<?php

namespace app\traits;

use app\exception\RepositoryException;
use app\model\Account;
use app\model\CouponMain;
use app\model\UsingRule;
use think\Model;
use app\model\Coupon;


trait CouponTrait
{
    /**消费者持有优惠券总数
     * @param string $userCode
     *
     *
     * @return int
     */
    public function consumerTotalCoupon($userCode)
    {
        return Coupon::where("consumer_code", $userCode)->count("id");
    }

    /**消费者已使用优惠券总数
     * @param string $userCode
     * @return int
     */
    public function consumerUsedTotalCoupon($userCode)
    {
        return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_on)->count("id");
    }

    /**
     * 消费者已使用优惠券总数
     * @param string $userCode
     * @return int
     */
    public function consumerNotUsedTotalCoupon($userCode)
    {
        return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_off)->count("id");
    }

    /**
     * 消费者已使用优惠券总数
     * @param string $userCode
     * @return array
     * @throws \Exception
     */
    public function consumerCouponList($where, $page, $size)
    {
        return Coupon::findList($where, [], $page, $size, function ($q){
            return $q->with("couponBill");
        }, ["id" => "desc"]);
    }

    /**
     * 查看某个优惠券的是否可以领取  0 可以领取 1 已领取不能再领取
     * @param $accountCode
     * @param CouponMain $couponMain
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCouponReceiveStatus($accountCode,CouponMain $couponMain )
    {
        //没有领取记录就可以领取
        $ReceiveCount = Coupon::where("coupon_id",$couponMain->id)
            ->where("consumer_code",$accountCode)
            ->count();
        if($ReceiveCount <= 0){
           return   CouponMain::COMMON_OFF;
        }

        //确定使用规则
        $usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
        if(empty($usingRule)){
            return CouponMain::COMMON_OFF;
        }

        //单人日限量
        $todayReceivesCount = Coupon::where("coupon_id",$couponMain->id)
            ->where("consumer_code",$accountCode)
            ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
            ->count();

        if($todayReceivesCount >= $usingRule->person_day_total  ){

            return CouponMain::COMMON_ON;
        }


        //单人总限量
        if($ReceiveCount >= $usingRule->person_total  ){
            return CouponMain::COMMON_ON;
        }

        return  CouponMain::COMMON_OFF;
    }


    /**
     * 查看某个优惠券的是否可以领取  0 可以领取 1 已领取不能再领取
     * @param Account $account
     * @param CouponMain $couponMain
     * @return void
     * @throws RepositoryException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCouponReceiveStatusText(Account $account,CouponMain $couponMain )
    {
        //没有领取记录就可以领取
        $ReceiveCount = Coupon::where("coupon_id",$couponMain->id)
            ->where("consumer_code",$account->user_code)->count();


        //确定使用规则
        $usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
        if(empty($usingRule)){
            throw  new RepositoryException("使用规则错误,不能领取");
        }


        //一天的总限量
        $dayTotalReceivesCount = Coupon::where("coupon_id",$couponMain->id)
            ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
            ->count();
        if($dayTotalReceivesCount >= $usingRule->day_total){
            throw  new RepositoryException("单日领取总限量达到上限");
        }


        //单人日限量
        $todayReceivesCount = Coupon::where("coupon_id",$couponMain->id)
            ->where("consumer_code",$account->user_code)
            ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
            ->count();
        if($todayReceivesCount >= $usingRule->person_day_total  ){
            throw  new RepositoryException("单人日限量达到上限");
        }

        //单人总限量
        if($ReceiveCount >= $usingRule->person_total  ){
            throw  new RepositoryException("单人总限量达到上限");
        }

        //白名单
        if(!empty($couponMain->white_list)){
            if (empty($account->real_name) || empty($account->mobile)) {
                throw  new RepositoryException("请先授权手机号和真实姓名");
            }
            if (false === strpos($couponMain->white_list, $account->mobile . "-" . $account->real_name)) {
                throw  new RepositoryException("您没有在该优惠券白名单内");
            }
        }



        //领取不做限制  使用才做限制
        return;
        $time = time();
        //一天的开始时间
        if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){
            throw  new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}领取");
        }
        //一天的结束时间
        if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){
            throw  new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}前领取");
        }

    }
}