SystemCityServices.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\shipping;
  12. use app\dao\shipping\SystemCityDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\FormBuilder as Form;
  17. /**
  18. * 城市数据
  19. * Class SystemCityServices
  20. * @package app\services\shipping
  21. * @method deleteCity(int $cityId) 删除cityId下的数据
  22. * @method getCityIdMax() 获取最大的cityId
  23. * @method save(array $data) 保存数据
  24. * @method update($id, array $data, ?string $key = null) 修改数据
  25. * @method value(array $where, ?string $field = '') 获取一条数据
  26. * @method getShippingCity() 获取运费模板城市数据
  27. */
  28. class SystemCityServices extends BaseServices
  29. {
  30. /**
  31. * 构造方法
  32. * SystemCityServices constructor.
  33. * @param SystemCityDao $dao
  34. */
  35. public function __construct(SystemCityDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 获取城市数据
  41. * @param array $where
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getCityList(array $where)
  48. {
  49. // $list = $this->dao->getCityList($where);
  50. // $cityIds = array_column($list, 'parent_id');
  51. // $cityNames = $this->dao->getCityArray(['city_id' => $cityIds], 'name', 'city_id');
  52. // foreach ($list as &$item) {
  53. // $item['parent_id'] = $cityNames[$item['parent_id']] ?? '中国';
  54. // }
  55. // return $list;
  56. // return CacheService::get('tree_city_list', function () {
  57. return $this->getSonCityList($where['parent_id']);
  58. // }, 86400);
  59. }
  60. /**
  61. * tree形城市列表
  62. * @param int $pid
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getSonCityList($pid = 0)
  69. {
  70. $list = $this->dao->getCityList(['parent_id' => $pid], 'id,city_id,level,name');
  71. $parent_name = $pid ? $this->dao->value(['city_id' => $pid], 'name') : '中国';
  72. $is_add = $pid == 0 || $this->dao->value(['city_id' => $pid], 'parent_id') == 0 ? 1 : 0;
  73. $arr = [];
  74. if ($list) {
  75. foreach ($list as $item) {
  76. $data = [];
  77. $data['id'] = $item['id'];
  78. $data['city_id'] = $item['city_id'];
  79. $data['label'] = $item['name'];
  80. $data['parent_name'] = $parent_name;
  81. if ($is_add) {
  82. $data['children'] = [];
  83. $data['hasChildren'] = [];
  84. $data['_loading'] = false;
  85. }
  86. $arr [] = $data;
  87. }
  88. }
  89. return $arr;
  90. }
  91. /**
  92. * 添加城市数据表单
  93. * @param int $parentId
  94. * @return array
  95. * @throws \FormBuilder\Exception\FormBuilderException
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function createCityForm(int $parentId)
  101. {
  102. if ($parentId) {
  103. $info = $this->dao->getOne(['city_id' => $parentId], 'level,city_id,name');
  104. } else {
  105. $info = ['level' => 0, 'city_id' => 0, 'name' => '中国'];
  106. }
  107. $field[] = Form::hidden('level', $info['level']);
  108. $field[] = Form::hidden('parent_id', $info['city_id']);
  109. $field[] = Form::input('parent_name', '上级名称', $info['name'])->disabled(true)->readonly(true);
  110. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  111. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  112. }
  113. /**
  114. * 添加城市数据创建
  115. * @param int $id
  116. * @return array
  117. * @throws \FormBuilder\Exception\FormBuilderException
  118. */
  119. public function updateCityForm(int $id)
  120. {
  121. $info = $this->dao->get($id);
  122. if (!$info) {
  123. throw new AdminException(100026);
  124. }
  125. $info = $info->toArray();
  126. $info['parent_name'] = $this->dao->value(['city_id' => $info['parent_id']], 'name') ?: '中国';
  127. $field[] = Form::hidden('id', $info['id']);
  128. $field[] = Form::hidden('level', $info['level']);
  129. $field[] = Form::hidden('parent_id', $info['parent_id']);
  130. $field[] = Form::input('parent_name', '上级名称', $info['parent_name'])->readonly(true);
  131. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  132. $field[] = Form::input('merger_name', '合并名称', $info['merger_name'])->placeholder('格式:陕西,西安,雁塔')->required('请填写合并名称');
  133. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  134. }
  135. /**
  136. * 获取城市数据
  137. * @return mixed
  138. */
  139. public function cityList()
  140. {
  141. return CacheService::remember('CITY_LIST', function () {
  142. $allCity = $this->dao->getCityList([], 'city_id as v,name as n,parent_id');
  143. return sort_city_tier($allCity, 0);
  144. }, 0);
  145. }
  146. /**
  147. * 获取城市数据完整列表
  148. * @return mixed
  149. */
  150. public function fullList($field = '*')
  151. {
  152. return CacheService::remember('CITY_FULL_LIST', function () use ($field) {
  153. return $this->fullListTree($this->dao->fullList($field));
  154. }, 0);
  155. }
  156. /**
  157. * 格式化获取城市数据完整列表
  158. * @param $data
  159. * @param int $pid
  160. * @param array $navList
  161. * @return array|mixed
  162. * @author 吴汐
  163. * @email 442384644@qq.com
  164. * @date 2023/04/10
  165. */
  166. function fullListTree($data, $pid = 0, $navList = [])
  167. {
  168. foreach ($data as $k => $menu) {
  169. if ($menu['parent_id'] == $pid) {
  170. unset($menu['parent_id']);
  171. unset($data[$k]);
  172. $menu['children'] = $this->fullListTree($data, $menu['value']);
  173. if(!count($menu['children'])) unset($menu['children']);
  174. $navList[] = $menu;
  175. }
  176. }
  177. return $navList;
  178. }
  179. }