165 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			165 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| 
 | |
| namespace app\traits;
 | |
| 
 | |
| use app\exception\RepositoryException;
 | |
| 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 $accountCode
 | |
|      * @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($accountCode,CouponMain $couponMain )
 | |
|     {
 | |
|         //没有领取记录就可以领取
 | |
|         $ReceiveCount = Coupon::where("coupon_id",$couponMain->id)
 | |
|             ->where("consumer_code",$accountCode)->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",$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  ){
 | |
|             throw  new RepositoryException("单人日限量达到上限");
 | |
|         }
 | |
| 
 | |
|         //单人总限量
 | |
|         if($ReceiveCount >= $usingRule->person_total  ){
 | |
|             throw  new RepositoryException("单人总限量达到上限");
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|         $time = time();
 | |
|         //一天的开始时间
 | |
|         if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){
 | |
|             throw  new RepositoryException("请在当天{$usingRule->day_start_time}后来领取");
 | |
|         }
 | |
|         //一天的结束时间
 | |
|         if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){
 | |
|             throw  new RepositoryException("请在当天{$usingRule->day_start_time}前领取");
 | |
|         }
 | |
| 
 | |
|     }
 | |
| } |