| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\traits; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use app\exception\RepositoryException; | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | use app\model\Account; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use app\model\CouponMain; | 
					
						
							|  |  |  | use app\model\UsingRule; | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  | use think\Model; | 
					
						
							|  |  |  | use app\model\Coupon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | trait CouponTrait | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |     /**消费者持有优惠券总数 | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |      * @param string $userCode | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function consumerTotalCoupon($userCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::where("consumer_code", $userCode)->count("id"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |     /**消费者已使用优惠券总数 | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |      * @param string $userCode | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function consumerUsedTotalCoupon($userCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_on)->count("id"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 消费者已使用优惠券总数 | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |      * @param string $userCode | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function consumerNotUsedTotalCoupon($userCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_off)->count("id"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 消费者已使用优惠券总数 | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |      * @param string $userCode | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws \Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-23 17:13:55 +08:00
										 |  |  |     public function consumerCouponList($where, $page, $size) | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-23 17:13:55 +08:00
										 |  |  |         return Coupon::findList($where, [], $page, $size, function ($q){ | 
					
						
							|  |  |  |             return $q->with("couponBill"); | 
					
						
							|  |  |  |         }, ["id" => "desc"]); | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 查看某个优惠券的是否可以领取  0 可以领取 1 已领取不能再领取 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |      * @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 ) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 09:34:31 +08:00
										 |  |  |         //如果 领取数量==总数
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |         if($couponMain['received_count']  == $couponMain['count'] ){ | 
					
						
							|  |  |  |                 return  CouponMain::receive_status_over; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         //没有领取记录就可以领取
 | 
					
						
							|  |  |  |         $ReceiveCount = Coupon::where("coupon_id",$couponMain->id) | 
					
						
							|  |  |  |             ->where("consumer_code",$accountCode) | 
					
						
							|  |  |  |             ->count(); | 
					
						
							|  |  |  |         if($ReceiveCount <= 0){ | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |             return  CouponMain::receive_status_default; | 
					
						
							|  |  |  |         }else{ | 
					
						
							|  |  |  |             return  CouponMain::receive_status_received; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //确定使用规则
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |         //$usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
 | 
					
						
							|  |  |  |         //if(empty($usingRule)){
 | 
					
						
							|  |  |  |         //    return CouponMain::COMMON_OFF;
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //单人日限量
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |         //$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();
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |         //if($todayReceivesCount >= $usingRule->person_day_total  ){
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |             //return CouponMain::COMMON_ON;
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //单人总限量
 | 
					
						
							| 
									
										
										
										
											2022-02-25 18:17:23 +08:00
										 |  |  |         //if($ReceiveCount >= $usingRule->person_total  ){
 | 
					
						
							|  |  |  |         //    return CouponMain::COMMON_ON;
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 查看某个优惠券的是否可以领取  0 可以领取 1 已领取不能再领取 | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |      * @param CouponMain $couponMain | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      * @throws RepositoryException | 
					
						
							|  |  |  |      * @throws \think\db\exception\DataNotFoundException | 
					
						
							|  |  |  |      * @throws \think\db\exception\DbException | 
					
						
							|  |  |  |      * @throws \think\db\exception\ModelNotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |     public function getCouponReceiveStatusText(Account $account,CouponMain $couponMain ) | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         //没有领取记录就可以领取
 | 
					
						
							|  |  |  |         $ReceiveCount = Coupon::where("coupon_id",$couponMain->id) | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |             ->where("consumer_code",$account->user_code)->count(); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         if(!empty($ReceiveCount)){ | 
					
						
							|  |  |  |             throw  new RepositoryException("您已经领取过了"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //确定使用规则
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //$usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
 | 
					
						
							|  |  |  |         //if(empty($usingRule)){
 | 
					
						
							|  |  |  |         //   throw  new RepositoryException("使用规则错误,不能领取");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //一天的总限量
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //$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("单日领取总限量达到上限");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //单人日限量
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //$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("单人日限量达到上限");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //单人总限量
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //if($ReceiveCount >= $usingRule->person_total  ){
 | 
					
						
							|  |  |  |         //    throw  new RepositoryException("单人总限量达到上限");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |         //白名单
 | 
					
						
							|  |  |  |         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)) { | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |                 throw  new RepositoryException("您没有在该优惠券白名单内"); | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         return ; | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |         //领取不做限制  使用才做限制
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //$time = time();
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         //一天的开始时间
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){
 | 
					
						
							|  |  |  |         //    throw  new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}领取");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         //一天的结束时间
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){
 | 
					
						
							|  |  |  |         //   throw  new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}前领取");
 | 
					
						
							|  |  |  |         //}
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  | } |