SystemCityServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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();
  58. }, 86400);
  59. }
  60. /**
  61. * tree形城市列表
  62. * @param int $pid
  63. * @param string $parent_name
  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, $parent_name = '中国')
  70. {
  71. $list = $this->dao->getCityList(['parent_id' => $pid], 'id,city_id,level,name');
  72. $arr = [];
  73. if ($list) {
  74. foreach ($list as $item) {
  75. $item['parent_id'] = $parent_name;
  76. $item['label'] = $item['name'];
  77. $item['value'] = $item['city_id'];
  78. $item['children'] = $this->getSonCityList($item['city_id'], $item['name']);
  79. $arr [] = $item;
  80. }
  81. }
  82. return $arr;
  83. }
  84. /**
  85. * 添加城市数据表单
  86. * @param int $parentId
  87. * @return array
  88. * @throws \FormBuilder\Exception\FormBuilderException
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function createCityForm(int $parentId)
  94. {
  95. if ($parentId) {
  96. $info = $this->dao->getOne(['city_id' => $parentId], 'level,city_id,name');
  97. } else {
  98. $info = ["level" => 0, "city_id" => 0, "name" => '中国'];
  99. }
  100. $field[] = Form::hidden('level', $info['level']);
  101. $field[] = Form::hidden('parent_id', $info['city_id']);
  102. $field[] = Form::input('parent_name', '上级名称', $info['name'])->readonly(true);
  103. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  104. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  105. }
  106. /**
  107. * 添加城市数据创建
  108. * @param int $id
  109. * @return array
  110. * @throws \FormBuilder\Exception\FormBuilderException
  111. */
  112. public function updateCityForm(int $id)
  113. {
  114. $info = $this->dao->get($id);
  115. if (!$info) {
  116. throw new AdminException('需改的数据不存在');
  117. }
  118. $info = $info->toArray();
  119. $info['parent_name'] = $this->dao->value(['city_id' => $info['parent_id']], 'name') ?: '中国';
  120. $field[] = Form::hidden('id', $info['id']);
  121. $field[] = Form::hidden('level', $info['level']);
  122. $field[] = Form::hidden('parent_id', $info['parent_id']);
  123. $field[] = Form::input('parent_name', '上级名称', $info['parent_name'])->readonly(true);
  124. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  125. $field[] = Form::input('merger_name', '合并名称', $info['merger_name'])->placeholder('格式:陕西,西安,雁塔')->required('请填写合并名称');
  126. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  127. }
  128. /**
  129. * 获取城市数据
  130. * @return mixed
  131. */
  132. public function cityList()
  133. {
  134. return CacheService::get('CITY_LIST', function () {
  135. $allCity = $this->dao->getCityList([], 'city_id as v,name as n,parent_id');
  136. return sort_city_tier($allCity, 0);
  137. }, 0);
  138. }
  139. }