SystemCityServices.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * @method fullList(?$field = '') 获取城市数据完整列表
  28. */
  29. class SystemCityServices extends BaseServices
  30. {
  31. /**
  32. * 构造方法
  33. * SystemCityServices constructor.
  34. * @param SystemCityDao $dao
  35. */
  36. public function __construct(SystemCityDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取城市数据
  42. * @param array $where
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function getCityList(array $where)
  49. {
  50. // $list = $this->dao->getCityList($where);
  51. // $cityIds = array_column($list, 'parent_id');
  52. // $cityNames = $this->dao->getCityArray(['city_id' => $cityIds], 'name', 'city_id');
  53. // foreach ($list as &$item) {
  54. // $item['parent_id'] = $cityNames[$item['parent_id']] ?? '中国';
  55. // }
  56. // return $list;
  57. // return CacheService::get('tree_city_list', function () {
  58. return $this->getSonCityList($where['parent_id']);
  59. // }, 86400);
  60. }
  61. /**
  62. * tree形城市列表
  63. * @param int $pid
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function getSonCityList($pid = 0)
  70. {
  71. $list = $this->dao->getCityList(['parent_id' => $pid], 'id,city_id,level,name');
  72. $parent_name = $pid ? $this->dao->value(['city_id' => $pid], 'name') : '中国';
  73. $is_add = $pid == 0 || $this->dao->value(['city_id' => $pid], 'parent_id') == 0 ? 1 : 0;
  74. $arr = [];
  75. if ($list) {
  76. foreach ($list as $item) {
  77. $data = [];
  78. $data['id'] = $item['id'];
  79. $data['city_id'] = $item['city_id'];
  80. $data['label'] = $item['name'];
  81. $data['parent_name'] = $parent_name;
  82. if ($is_add) {
  83. $data['children'] = [];
  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. }