| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | namespace app\repository; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\model\Business; | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  | use app\model\Coupon; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use app\model\CouponMain; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use app\model\CouponType; | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | use app\model\UsingRule; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use app\service\Repository; | 
					
						
							| 
									
										
										
										
											2021-12-17 14:44:05 +08:00
										 |  |  | use app\traits\CouponBillTrait; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use app\traits\CouponMainTrait; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  | use think\Collection; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | use think\Db; | 
					
						
							| 
									
										
										
										
											2021-11-25 18:56:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use think\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |  * 优惠券 相关操作 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Class CouponRepository | 
					
						
							|  |  |  |  * @package app\repository | 
					
						
							|  |  |  |  * @method self getInstance(Model $model = null) static | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CouponRepository extends Repository | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     use CouponMainTrait; | 
					
						
							| 
									
										
										
										
											2021-12-17 14:44:05 +08:00
										 |  |  |     use CouponBillTrait; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 优惠券持有信息列表 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |      * @param $id | 
					
						
							|  |  |  |      * @param $keyword | 
					
						
							|  |  |  |      * @param $page | 
					
						
							|  |  |  |      * @param $size | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |      * @throws \Exception | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |     public function couponMainHasList($id, $keyword, $page, $size) | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-22 16:19:36 +08:00
										 |  |  |         return Coupon::findList(["coupon_id" => $id], ["*"], $page, $size, function ($q) use ($keyword) { | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |             if (!empty($keyword)) { | 
					
						
							|  |  |  |                 return $q::hasWhere('account', function ($q) use ($keyword) { | 
					
						
							| 
									
										
										
										
											2021-12-22 16:19:36 +08:00
										 |  |  |                     $q->where('nick_name', 'like', "%" . $keyword . "%")->field("nick_name,avatar_url,user_code"); | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return $q->with(["account" => function ($query) { | 
					
						
							| 
									
										
										
										
											2021-12-22 16:19:36 +08:00
										 |  |  |                     $query->field("nick_name,avatar_url,user_code"); | 
					
						
							| 
									
										
										
										
											2021-11-24 16:34:56 +08:00
										 |  |  |                 }]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, ["id" => "desc"]); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  |      * 获取优惠卷类型 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  |      * @param array $fields | 
					
						
							|  |  |  |      * @param array $order | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  |     public function getCouponTypeAll(array $fields = [], array $order = ["id" => "desc"]) | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             return CouponType::order($order)->field($fields)->select(); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return new Collection(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 发布优惠券  扣除商家余额 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * @param $data | 
					
						
							|  |  |  |      * @param $totalMoney | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |      * @param $usingRule 使用规则 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |     public function releaseCouponMain($data) | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |         //创建优惠券根表
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |         $couponMain = CouponMain::create($data); | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |         //创建优惠券使用规则
 | 
					
						
							| 
									
										
										
										
											2022-01-19 13:40:46 +08:00
										 |  |  |         //$usingRule["coupon_id"] = $couponMain->id;
 | 
					
						
							|  |  |  |         //UsingRule::create($usingRule);
 | 
					
						
							| 
									
										
										
										
											2021-12-14 17:34:27 +08:00
										 |  |  |         //Business::where("code", $data["business_code"])->dec("balance", $totalMoney)->update();
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 查询商家发布的优惠卷列表 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $where | 
					
						
							|  |  |  |      * @param array $fields | 
					
						
							|  |  |  |      * @param int $page | 
					
						
							|  |  |  |      * @param int $size | 
					
						
							|  |  |  |      * @param callable|null $call | 
					
						
							|  |  |  |      * @param array $sortOrder | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |     public function findCouponMainList(array $where, array $fields = [], int $page = 1, int $size = 0, callable $call = null, array $sortOrder = []): array | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return CouponMain::findList($where, $fields, $page, $size, $call, $sortOrder); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-20 13:56:21 +08:00
										 |  |  |      * 写入一个领取优惠券 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |      * @param array $data | 
					
						
							|  |  |  |      * @return Coupon|Model | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function receiveCoupon(array $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Coupon::create($data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | } |