SystemCityServices.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  57. /**
  58. * 添加城市数据表单
  59. * @param int $parentId
  60. * @return array
  61. * @throws \FormBuilder\Exception\FormBuilderException
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function createCityForm(int $parentId)
  67. {
  68. if ($parentId) {
  69. $info = $this->dao->getOne(['city_id' => $parentId], 'level,city_id,name');
  70. } else {
  71. $info = ["level" => 0, "city_id" => 0, "name" => '中国'];
  72. }
  73. $field[] = Form::hidden('level', $info['level']);
  74. $field[] = Form::hidden('parent_id', $info['city_id']);
  75. $field[] = Form::input('parent_name', '上级名称', $info['name'])->readonly(true);
  76. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  77. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  78. }
  79. /**
  80. * 添加城市数据创建
  81. * @param int $id
  82. * @return array
  83. * @throws \FormBuilder\Exception\FormBuilderException
  84. */
  85. public function updateCityForm(int $id)
  86. {
  87. $info = $this->dao->get($id);
  88. if (!$info) {
  89. throw new AdminException('需改的数据不存在');
  90. }
  91. $info = $info->toArray();
  92. $info['parent_name'] = $this->dao->value(['city_id' => $info['parent_id']], 'name') ?: '中国';
  93. $field[] = Form::hidden('id', $info['id']);
  94. $field[] = Form::hidden('level', $info['level']);
  95. $field[] = Form::hidden('parent_id', $info['parent_id']);
  96. $field[] = Form::input('parent_name', '上级名称', $info['parent_name'])->readonly(true);
  97. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  98. $field[] = Form::input('merger_name', '合并名称', $info['merger_name'])->placeholder('格式:陕西,西安,雁塔')->required('请填写合并名称');
  99. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  100. }
  101. /**
  102. * 获取城市数据
  103. * @return mixed
  104. */
  105. public function cityList()
  106. {
  107. return CacheService::get('CITY_LIST', function () {
  108. $allCity = $this->dao->getCityList([], 'city_id as v,name as n,parent_id');
  109. return sort_city_tier($allCity, 0);
  110. }, 0);
  111. }
  112. }