| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | namespace app\repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use app\exception\RepositoryException; | 
					
						
							| 
									
										
										
										
											2021-12-22 15:09:53 +08:00
										 |  |  | use app\model\Account; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use app\model\Business; | 
					
						
							|  |  |  | use app\model\CouponMain; | 
					
						
							|  |  |  | use app\model\Deduction; | 
					
						
							|  |  |  | use app\model\Recharge; | 
					
						
							|  |  |  | use app\service\Repository; | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  | use app\traits\account\ApplyStaffTrait; | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  | use app\traits\CouponBillTrait; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use app\traits\CouponMainTrait; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | use think\Collection; | 
					
						
							|  |  |  | use think\Model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 商家域 相关操作 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class BusinessRepository | 
					
						
							|  |  |  |  * @package app\repository | 
					
						
							|  |  |  |  * @method self getInstance(Model $model = null) static | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class BusinessRepository extends Repository | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     use CouponBillTrait; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |     use CouponMainTrait; | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |     use ApplyStaffTrait; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 根据条件查询列表 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $where 查询条件 | 
					
						
							|  |  |  |      * @param int $page 默认第一页 0不限制 | 
					
						
							|  |  |  |      * @param int $limit 限制条数 0不限制 | 
					
						
							|  |  |  |      * @param array $order | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws RepositoryException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function businessList(array $where = [], int $page = 1, int $limit = 0, array $order = []) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-29 13:56:26 +08:00
										 |  |  |         $q = $this->model->alias("a") | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |             ->join("account b", "a.code = b.business_code") | 
					
						
							| 
									
										
										
										
											2021-12-01 16:53:44 +08:00
										 |  |  |             ->join("category c", "a.type = c.id","left") | 
					
						
							| 
									
										
										
										
											2021-12-23 09:49:15 +08:00
										 |  |  |             ->where("b.type","=",Account::type_business) | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |             ->field(["a.code as business_code", | 
					
						
							|  |  |  |                 "b.avatar_url", | 
					
						
							|  |  |  |                 "b.nick_name as account_nick_name", | 
					
						
							|  |  |  |                 "a.business_name", | 
					
						
							|  |  |  |                 "a.total_recharge", | 
					
						
							|  |  |  |                 "a.id as basiness_id", | 
					
						
							| 
									
										
										
										
											2021-12-01 16:53:44 +08:00
										 |  |  |                 "a.balance", | 
					
						
							|  |  |  |                 "a.contact_name", | 
					
						
							|  |  |  |                 "a.contact_phone", | 
					
						
							|  |  |  |                 "a.business_address", | 
					
						
							|  |  |  |                 "a.business_license", | 
					
						
							|  |  |  |                 "a.state", | 
					
						
							|  |  |  |                 "c.name as type_name", | 
					
						
							|  |  |  |             ]) | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |             ->group("a.id"); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         $data = [ | 
					
						
							|  |  |  |             'total' => 0, | 
					
						
							|  |  |  |             'current' => $page, | 
					
						
							|  |  |  |             'size' => $limit, | 
					
						
							|  |  |  |             'list' => new Collection(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (count($where)) { | 
					
						
							|  |  |  |             $q = $q->where($where); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (count($order)) { | 
					
						
							|  |  |  |             $q = $q->order($order); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $data['total'] = $q->count(); | 
					
						
							|  |  |  |         if ($data['total']) { | 
					
						
							|  |  |  |             if ($limit) { | 
					
						
							|  |  |  |                 if ($page) { | 
					
						
							|  |  |  |                     $q = $q->page($page); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $q = $q->limit($limit); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $data['list'] = $q->select(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 优惠券列表 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $businessCode 商家code | 
					
						
							|  |  |  |      * @param int $page 默认第一页 0不限制 | 
					
						
							|  |  |  |      * @param int $limit 限制条数 0不限制 | 
					
						
							|  |  |  |      * @param array $order | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws RepositoryException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function businessCouponList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"]) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return CouponMain::findList($where, [], $page, $limit, null, $order); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 扣费记录 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $businessCode 商家code | 
					
						
							|  |  |  |      * @param int $page 默认第一页 0不限制 | 
					
						
							|  |  |  |      * @param int $limit 限制条数 0不限制 | 
					
						
							|  |  |  |      * @param array $order | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws RepositoryException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function businessDeductionList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"]) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |         return Deduction::findList($where, [], $page, $limit, function ($q) { | 
					
						
							| 
									
										
										
										
											2021-12-20 17:53:03 +08:00
										 |  |  |             return $q->with(["couponMain","bill","account"]); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |         }, $order); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 充值记录 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $businessCode 商家code | 
					
						
							|  |  |  |      * @param int $page 默认第一页 0不限制 | 
					
						
							|  |  |  |      * @param int $limit 限制条数 0不限制 | 
					
						
							|  |  |  |      * @param array $order | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * @throws RepositoryException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function businessRechargeList($where, int $page = 1, int $limit = 0, array $order = ["create_time" => "desc", "id" => "desc"]) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |         return Recharge::findList($where, [], $page, $limit, function ($q) { | 
					
						
							|  |  |  |             return $q->withJoin(["business" => ["business_name"]]); | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |         }, $order); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* 获取所有的商家*/ | 
					
						
							|  |  |  |     public function getBusinessAll() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |         return Business::alias("a") | 
					
						
							|  |  |  |             ->join("account b", "a.code = b.business_code") | 
					
						
							| 
									
										
										
										
											2021-12-23 11:02:00 +08:00
										 |  |  |             ->field("a.code , a.business_name , a.contact_name") | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |             ->order("a.id desc") | 
					
						
							|  |  |  |             ->group("a.id") | 
					
						
							|  |  |  |             ->select(); | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取单个商家详情 | 
					
						
							|  |  |  |      * @param $businessCode | 
					
						
							|  |  |  |      * @param bool $lock | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |      * @return array|Model|null | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |      * @throws \think\db\exception\DataNotFoundException | 
					
						
							|  |  |  |      * @throws \think\db\exception\DbException | 
					
						
							|  |  |  |      * @throws \think\db\exception\ModelNotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |     public function getBusinessAccount($businessCode, bool $lock = false) | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-17 11:41:27 +08:00
										 |  |  |         $business = Business::where("code", $businessCode)->when($lock, function ($q) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             $q->lock(true); | 
					
						
							|  |  |  |         })->find(); | 
					
						
							| 
									
										
										
										
											2021-12-17 11:41:27 +08:00
										 |  |  |         if (empty($business)) { | 
					
						
							| 
									
										
										
										
											2021-11-25 18:11:50 +08:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-17 11:41:27 +08:00
										 |  |  |         return $business; | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-29 13:59:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 格式化前端输出商户信息 | 
					
						
							|  |  |  |      * @param array $data | 
					
						
							|  |  |  |      * @param array $formats | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function formatFrontBusinessInfo(array $data, array $formats=[]): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $fields = ['id', 'code', 'business_name', 'business_subtitle', 'business_license', | 
					
						
							|  |  |  |             'lat', 'lng', 'business_address', 'contact_name', 'contact_phone', | 
					
						
							|  |  |  |             'create_time', 'type', 'type_name', 'characteristic', 'intro', | 
					
						
							|  |  |  |             'business_circle', 'business_circle_id', 'background', 'score', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (in_array(1, $formats)) { | 
					
						
							|  |  |  |             // 返回审批相关字段
 | 
					
						
							|  |  |  |             $fields = array_merge($fields, ['state', 'reason']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (in_array(2, $formats)) { | 
					
						
							|  |  |  |             // 返回关联的平台商相关字段
 | 
					
						
							|  |  |  |             $fields = array_merge($fields, [ 'is_agency', 'agency_code']); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return arrayKeysFilter($data, $fields); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 查看指定商圈下的商家 | 
					
						
							|  |  |  |      * @param $businessCircleId | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |      * @return Business[]|array|Collection | 
					
						
							|  |  |  |      * @throws \think\db\exception\DataNotFoundException | 
					
						
							|  |  |  |      * @throws \think\db\exception\DbException | 
					
						
							|  |  |  |      * @throws \think\db\exception\ModelNotFoundException | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getByCircleId($businessCircleId) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-11-30 18:31:58 +08:00
										 |  |  |         return Business::where("business_circle_id", $businessCircleId)->select(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 平台商下有多少商家 | 
					
						
							|  |  |  |      * @param $agencyCode | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function agencyHasBusinessCount($agencyCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Business::where("agency_code", $agencyCode)->count(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 平台商下商家 所持有的优惠券数量 | 
					
						
							|  |  |  |      * @param $agencyCode | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function agencyHasCouponCount($agencyCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Business::alias("a") | 
					
						
							|  |  |  |             ->join("coupon_main b", "a.code =b.business_code") | 
					
						
							|  |  |  |             ->where("a.agency_code", $agencyCode) | 
					
						
							|  |  |  |             ->sum("b.count"); | 
					
						
							| 
									
										
										
										
											2021-11-30 15:26:38 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-18 17:57:04 +08:00
										 |  |  | } |