| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /**消费者持有优惠券总数 | 
					
						
							|  |  |  |      * @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"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +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
										 |  |  |     /** | 
					
						
							|  |  |  |      * 消费者已使用优惠券总数 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     /** | 
					
						
							|  |  |  |      * 查看某个优惠券的是否可以领取  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 已领取不能再领取 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //确定使用规则
 | 
					
						
							|  |  |  |         $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("单日领取总限量达到上限"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-19 19:06:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //单人日限量
 | 
					
						
							|  |  |  |         $todayReceivesCount = Coupon::where("coupon_id",$couponMain->id) | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |             ->where("consumer_code",$account->user_code) | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |             ->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("单人总限量达到上限"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							|  |  |  |                 throw  new RepositoryException("您没有在该优惠券白名单内"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |         //领取不做限制  使用才做限制
 | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         $time = time(); | 
					
						
							|  |  |  |         //一天的开始时间
 | 
					
						
							|  |  |  |         if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){ | 
					
						
							| 
									
										
										
										
											2021-12-13 19:09:16 +08:00
										 |  |  |             throw  new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}领取"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         //一天的结束时间
 | 
					
						
							|  |  |  |         if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){ | 
					
						
							| 
									
										
										
										
											2021-12-13 19:09:16 +08:00
										 |  |  |             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
										 |  |  | } |