| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app\traits; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  | use app\exception\RepositoryException; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use app\model\CouponMain; | 
					
						
							|  |  |  | use app\model\UsingRule; | 
					
						
							|  |  |  | use app\model\Coupon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | trait CouponMainTrait | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 检查优惠券状态是否可以领取 | 
					
						
							|  |  |  |      * @param CouponMain $couponMain | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function checkCouponMainReceiveStatus(CouponMain $couponMain){ | 
					
						
							|  |  |  |         if (empty($couponMain)) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券不存在"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ($couponMain->status != CouponMain::status_on) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券已停用"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ($couponMain->on_shelf != CouponMain::on_shelf_on) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券已下架"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         $time = time(); | 
					
						
							|  |  |  |         if (strtotime($couponMain->start_time) > $time) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券还未到开始使用时间"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (strtotime($couponMain->end_time) < $time) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券已结束使用"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($couponMain->using_count <= 0) { | 
					
						
							| 
									
										
										
										
											2021-12-16 14:03:35 +08:00
										 |  |  |             throw  new  RepositoryException("优惠券已经被领完了"); | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-09 18:26:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 统计这个优惠券已经使用了多少张了 | 
					
						
							|  |  |  |      * @param $couponMainId | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCouponMainUsingCount($couponMainId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::where("coupon_id",$couponMainId) | 
					
						
							|  |  |  |             ->where("is_verificated",Coupon::is_verificated_on) | 
					
						
							|  |  |  |             ->count(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取商户没有被领取的优惠券  需要扣除多少钱 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBusinessNotClaimedCoupon($businessCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $date = date("Y-m-d"); | 
					
						
							|  |  |  |         $totalDeductionMoney = 0; | 
					
						
							|  |  |  |         $couponMain = CouponMain::where("business_code", $businessCode) | 
					
						
							|  |  |  |             ->where("status", CouponMain::status_on) | 
					
						
							|  |  |  |             ->where("on_shelf", CouponMain::on_shelf_on) | 
					
						
							|  |  |  |             ->whereTime("start_time", "<=", $date) | 
					
						
							|  |  |  |             ->whereTime("end_time", ">=", $date) | 
					
						
							|  |  |  |             ->whereRaw(" verification_count <  count ") | 
					
						
							|  |  |  |             ->field("id,(deduction_money * (count - verification_count)) as total_deduction_money") | 
					
						
							|  |  |  |             ->select() | 
					
						
							|  |  |  |             ->toArray(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($couponMain as $item) { | 
					
						
							|  |  |  |             $totalDeductionMoney += $item["total_deduction_money"]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $totalDeductionMoney; | 
					
						
							| 
									
										
										
										
											2021-12-15 15:04:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取 发布优惠券的统计 | 
					
						
							|  |  |  |      * 每条数据都走一遍 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public  function getReleaseStatistics($startTime,$endTime,$agencyCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return CouponMain::alias("a") | 
					
						
							|  |  |  |             ->join("business b","a.business_code =b.code") | 
					
						
							|  |  |  |             ->whereTime("a.start_time", ">=", $startTime) | 
					
						
							|  |  |  |             ->whereTime("a.end_time", "<=", $endTime) | 
					
						
							|  |  |  |             ->when(!empty($agencyCode), function ($q) use ($agencyCode) { | 
					
						
							|  |  |  |                 $q->where("b.agency_code", $agencyCode); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->field("a.id,a.create_time") | 
					
						
							|  |  |  |             ->withAttr("create_time",function ($value){ | 
					
						
							|  |  |  |                 return date("Y-m-d",strtotime($value)); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->order(["a.id" => "desc"]) | 
					
						
							|  |  |  |             ->select() | 
					
						
							|  |  |  |             ->toArray() | 
					
						
							|  |  |  |             ; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-15 15:04:20 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取 验证优惠券的统计 | 
					
						
							|  |  |  |      * 每条数据都走一遍 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getSignStatistics($startTime,$endTime,$agencyCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::alias("a") | 
					
						
							|  |  |  |             ->join("business b","a.business_code =b.code") | 
					
						
							|  |  |  |             ->whereTime("a.verificate_time", ">=", $startTime) | 
					
						
							|  |  |  |             ->whereTime("a.verificate_time", "<=", $endTime) | 
					
						
							|  |  |  |             ->where("a.is_verificated", "=", Coupon::is_verificated_on) | 
					
						
							|  |  |  |             ->when(!empty($agencyCode), function ($q) use ($agencyCode) { | 
					
						
							|  |  |  |                 $q->where("b.agency_code", $agencyCode); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->field("a.id,a.verificate_time") | 
					
						
							|  |  |  |             ->withAttr("verificate_time",function ($value){ | 
					
						
							|  |  |  |                 return date("Y-m-d",strtotime($value)); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->order(["a.id" => "desc"]) | 
					
						
							|  |  |  |             ->select() | 
					
						
							|  |  |  |             ->toArray() | 
					
						
							|  |  |  |             ; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取 验证优惠券的统计 | 
					
						
							|  |  |  |      * 每条数据都走一遍 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getReceiveStatistics($startTime,$endTime,$agencyCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::alias("a") | 
					
						
							|  |  |  |             ->join("business b","a.business_code =b.code") | 
					
						
							|  |  |  |             ->whereTime("a.received_time", ">=", $startTime) | 
					
						
							|  |  |  |             ->whereTime("a.received_time", "<=", $endTime) | 
					
						
							|  |  |  |             ->when(!empty($agencyCode), function ($q) use ($agencyCode) { | 
					
						
							|  |  |  |                 $q->where("b.agency_code", $agencyCode); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->field("a.id,a.received_time") | 
					
						
							|  |  |  |             ->withAttr("received_time",function ($value){ | 
					
						
							|  |  |  |                 return date("Y-m-d",strtotime($value)); | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             ->order(["a.id" => "desc"]) | 
					
						
							|  |  |  |             ->select() | 
					
						
							|  |  |  |             ->toArray() | 
					
						
							|  |  |  |             ; | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-16 10:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家商家的优惠券 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBusinessOnShelfOnCount($businessCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return CouponMain::where("business_code",$businessCode) | 
					
						
							|  |  |  |             ->where("on_shelf",CouponMain::on_shelf_on) | 
					
						
							|  |  |  |             ->count(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | } |