| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace app\repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  | use app\model\Area; | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  | use app\model\BusinessCircle; | 
					
						
							|  |  |  | use app\model\Category; | 
					
						
							|  |  |  | use app\model\Model; | 
					
						
							|  |  |  | use app\service\Repository; | 
					
						
							|  |  |  | use think\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 数据字典域 相关 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class DictionaryRepository | 
					
						
							|  |  |  |  * @package app\repository | 
					
						
							|  |  |  |  * @method self getInstance(Model $model = null) static | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class DictionaryRepository extends Repository | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 上下级按children进行递归分组 | 
					
						
							|  |  |  |      * @param $items | 
					
						
							|  |  |  |      * @param int $pid | 
					
						
							|  |  |  |      * @param string $pidField 上下级关联字段 | 
					
						
							|  |  |  |      * @param string $primaryField 关联关系字段 | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function recursionChildrenList($items, int $pid=0, string $pidField = 'pid', string $primaryField='id'): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $list = []; | 
					
						
							|  |  |  |         foreach ($items as $ki => $item) { | 
					
						
							|  |  |  |             if ($item[$pidField] == $pid) { | 
					
						
							|  |  |  |                 unset($items[$ki]); | 
					
						
							|  |  |  |                 $item['children']   = $this->recursionChildrenList($items, $item[$primaryField], $pidField, $primaryField); | 
					
						
							|  |  |  |                 $list[] = $item; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $list; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取所有的商圈数据 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAllBusinessCircleList(array $fields=[]) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return BusinessCircle::field($fields) | 
					
						
							|  |  |  |                 ->order(['sort'=>'desc', 'id'=>'asc']) | 
					
						
							|  |  |  |                 ->select(); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return new Collection(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取商圈详情 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param int $id | 
					
						
							|  |  |  |      * @param array $fields | 
					
						
							|  |  |  |      * @param callable|null $call | 
					
						
							|  |  |  |      * @return array|\think\Model|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findBusinessCircleById(int $id, array $fields = [], callable $call = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return BusinessCircle::findById($id, $fields, $call); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家分类数据 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBusinessTypeList(array $where=[], array $fields = [], callable $call=null, array $order = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $res = Category::findList($where, $fields, 1, 0, $call, $order); | 
					
						
							|  |  |  |             return $res['list']; | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return new Collection(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家分类详情 | 
					
						
							|  |  |  |      * @param int $id | 
					
						
							|  |  |  |      * @param array $fields | 
					
						
							|  |  |  |      * @param callable|null $call | 
					
						
							|  |  |  |      * @return array|\think\Model|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findBusinessTypeById(int $id, array $fields = [], callable $call = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return Category::findById($id, $fields, $call); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 省市区地址配置数据 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findAreaList(array $where = []) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return Area::where($where)->select(); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return new Collection(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 通过地址编号查询地址信息 | 
					
						
							|  |  |  |      * @param string $areaCode | 
					
						
							|  |  |  |      * @return Area|array|\think\Model|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function findAreaInfo(string $areaCode) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return Area::where('code', $areaCode)->find(); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 解析地址编码 | 
					
						
							|  |  |  |      * [province, city, county] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function parseAreaText(array $list, array $areaList = []): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (empty($list)) { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($areaList)) { | 
					
						
							|  |  |  |             $areaList = $this->findAreaList()->column('name', 'code'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $list['province_text']  = (!isset($list['province']) || empty($list['province'])) ? '' : ($areaList[$list['province']] ?? ''); | 
					
						
							|  |  |  |         $list['city_text']      = (!isset($list['city']) || empty($list['city'])) ? '' : ($areaList[$list['city']] ?? ''); | 
					
						
							|  |  |  |         $list['county_text']    = (!isset($list['county']) || empty($list['county'])) ? '' : ($areaList[$list['county']] ?? ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $list; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 16:34:25 +08:00
										 |  |  | } |