<?php

/**
 *
 *
 * @category   xxx
 * @package    PSR
 * @subpackage Documentation\API
 * @author     xxx  <xxx@xxx.com>
 * @license    GPL https://xxx.com
 * @link       https://xxx.com
 * @ctime:     2020/4/7 9:40
 */


namespace App\Repositories;


use App\Models\Coupon;
use App\Models\CouponsUserBag;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class CouponUserBagsRepository extends BaseRepository
{

    public $couponRepository;
    public $userRepository;

    public function __construct(CouponsUserBag $couponsUserBag, CouponRepository $couponRepository, UserRepository $userRepository)
    {
        $this->model = $couponsUserBag;
        $this->couponRepository = $couponRepository;
        $this->userRepository = $userRepository;
    }

    public function getCouponsByUserIdAndStatus($user_id, $status, $order_type = 3)
    {
        // 查询前先更新一下失效的
        $update = $this->model->where('user_id', $user_id)->where('status', Coupon::STATUS_OK)->get();
        $this->updateCouponsStatus($update);

        if ($order_type == $this->model::ORDER_TYPE_NORMAL) {
            $order_type_arr = [
                $this->model::ORDER_TYPE_NORMAL,
                $this->model::ORDER_TYPE_ALL
            ];
        } elseif ($order_type == $this->model::ORDER_TYPE_RENT) {
            $order_type_arr = [
                $this->model::ORDER_TYPE_RENT,
                $this->model::ORDER_TYPE_ALL
            ];
        } elseif ($order_type == $this->model::ORDER_TYPE_ALL) {
            $order_type_arr = [
                $this->model::ORDER_TYPE_RENT,
                $this->model::ORDER_TYPE_NORMAL,
                $this->model::ORDER_TYPE_ALL
            ];
        }

        return $this->model->query()
            ->where('user_id', $user_id)
            ->where('status', $status)
            ->whereIn('order_type', $order_type_arr)
            ->where('created_at', '>', Carbon::now()->subMonths(3))
            ->get();
    }

    public function getCouponByUserIdAndID($user_id, $id)
    {
        return $this->model->query()
            ->where('id', $id)
            ->where('user_id', $user_id)
            ->where('status', CouponsUserBag::STATUS_OK)
            ->first();
    }


    /**
     * updateCouponsStatus  更新优惠券失效
     *
     * @param CouponsUserBag $coupon
     * @return void
     * @author Fx
     *
     */
    public function updateCouponsStatus($coupon)
    {
        if (count($coupon) !== 0) {
            foreach ($coupon as $v) {
                if ($v->valid_type == Coupon::VALID_TYPE_ABSOLUTELY) {
                    // 绝对时效
                    $valid_start_time_diff = Carbon::now()->diffInSeconds(Carbon::make($v->valid_start_time), false); //小于等于0有效  大于0无效
                    $valid_end_time_diff = Carbon::now()->diffInSeconds(Carbon::make($v->valid_end_time), false); //大于等于0有效  小于0无效
                    if ($valid_start_time_diff > 0 || $valid_end_time_diff < 0) {
                        $v->status = Coupon::STATUS_NO;
                    }

                } else {
                    $overdue_time = Carbon::parse($v->created_at)->addDays($v->valid_days);
                    $overdue_time_diff = Carbon::now()->diffInSeconds($overdue_time, false); // 大等于0有效
                    if ($overdue_time_diff < 0) {
                        $v->status = Coupon::STATUS_NO;
                    }
                }
                $v->save();
            }
        }
    }

    /**
     * 邀请新用户赠送优惠券 inviteNewRewardCoupon
     *
     * @param $coupon_id
     * @param $user_id
     * @return void
     * @author Fx
     *
     */
    public function inviteNewRewardCoupon($coupon_id, $user_id, $coupon_num)
    {
        $users = $this->userRepository->byIdGetModel($user_id);
        $coupon = $this->couponRepository->byIdGetModel($coupon_id);
        $data = [
            'coupon_id' => $coupon_id,
            'type' => CouponsUserBag::TYPE_INVITE_NEW_GIVE,
            'user_id' => $users->id,
            'coupons_data' => $coupon->toArray(),
            'status' => CouponsUserBag::STATUS_OK,
            'valid_type' => $coupon->valid_type,
            'valid_start_time' => $coupon->valid_start_time,
            'valid_end_time' => $coupon->valid_end_time,
            'valid_days' => $coupon->valid_days,
            'grant_start_at' => $coupon->grant_start_at,
            'grant_end_at' => $coupon->grant_end_at,
            'give_admin_id' => 0,
            'order_type' => $coupon->order_type
        ];

        for ($coupon_num; $coupon_num > 0; $coupon_num--) {
            $this->model->create($data);
            $coupon->increment('take_count');
        }
    }

}