Region.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\facade\Cache;
  15. use app\common\library\helper;
  16. /**
  17. * 地区模型
  18. * Class Region
  19. * @package app\common\model
  20. */
  21. class Region extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'region';
  25. // 定义主键
  26. protected $pk = 'id';
  27. protected $createTime = false;
  28. protected $updateTime = false;
  29. /**
  30. * 类型自动转换
  31. * @var array
  32. */
  33. protected $type = [
  34. 'id' => 'integer',
  35. 'pid' => 'integer',
  36. 'level' => 'integer',
  37. ];
  38. // 当前数据版本号
  39. private static string $version = '1.0.3';
  40. /**
  41. * 根据ID获取地区名称
  42. * @param int $id
  43. * @return mixed|string
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function getNameById(int $id = 0)
  49. {
  50. if ($id == 0) {
  51. return '其他';
  52. }
  53. $data = self::getCacheAll();
  54. return isset($data[$id]) ? $data[$id]['name'] : '其他';
  55. }
  56. /**
  57. * 根据名称获取地区ID
  58. * @param string $name
  59. * @param int $level
  60. * @param int $pid
  61. * @return int|mixed
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function getIdByName(string $name, int $level = 0, int $pid = 0)
  67. {
  68. $data = self::getCacheAll();
  69. foreach ($data as $item) {
  70. if ($item['name'] == $name && $item['level'] == $level && $item['pid'] == $pid) {
  71. return $item['id'];
  72. }
  73. }
  74. return 0;
  75. }
  76. /**
  77. * 获取所有地区(树状结构)
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public static function getCacheTree(): array
  84. {
  85. return static::getCacheData('tree');
  86. }
  87. /**
  88. * 获取所有地区列表
  89. * @return array
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public static function getCacheAll(): array
  95. {
  96. return static::getCacheData('all');
  97. }
  98. /**
  99. * 获取所有地区的总数
  100. * @return array
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. */
  105. public static function getCacheCounts(): array
  106. {
  107. return static::getCacheData('counts');
  108. }
  109. /**
  110. * 获取缓存中的数据(存入静态变量)
  111. * @param null $item
  112. * @return array|mixed
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. private static function getCacheData($item = null)
  118. {
  119. static $cacheData = [];
  120. if (empty($cacheData)) {
  121. $static = new static;
  122. $cacheData = $static->regionCache();
  123. }
  124. if (is_null($item)) {
  125. return $cacheData;
  126. }
  127. return $cacheData[$item];
  128. }
  129. /**
  130. * 获取地区缓存
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. private function regionCache(): array
  137. {
  138. // 缓存的数据
  139. $complete = Cache::get('region');
  140. // 如果存在缓存则返回缓存的数据,否则从数据库中查询
  141. // 条件1: 获取缓存数据
  142. // 条件2: 数据版本号要与当前一致
  143. if (
  144. !empty($complete)
  145. && isset($complete['version'])
  146. && $complete['version'] == self::$version
  147. ) {
  148. return $complete;
  149. }
  150. // 所有地区
  151. $allList = $tempList = $this->getAllList();
  152. // 已完成的数据
  153. $complete = [
  154. 'all' => $allList,
  155. 'tree' => $this->getTreeList($allList),
  156. 'counts' => $this->getCount($allList),
  157. 'version' => self::$version,
  158. ];
  159. // 写入缓存
  160. Cache::tag('cache')->set('region', $complete);
  161. return $complete;
  162. }
  163. /**
  164. * 地区总数
  165. * @param $allList
  166. * @return array
  167. */
  168. private static function getCount($allList): array
  169. {
  170. $counts = [
  171. 'total' => count($allList),
  172. 'province' => 0,
  173. 'city' => 0,
  174. 'region' => 0,
  175. ];
  176. $level = [1 => 'province', 2 => 'city', 3 => 'region'];
  177. foreach ($allList as $item) {
  178. $counts[$level[$item['level']]]++;
  179. }
  180. return $counts;
  181. }
  182. /**
  183. * 格式化为树状格式
  184. * @param $allList
  185. * @return array
  186. */
  187. private function getTreeList($allList): array
  188. {
  189. $treeList = [];
  190. foreach ($allList as $pKey => $province) {
  191. if ($province['level'] == 1) { // 省份
  192. $treeList[$province['id']] = $province;
  193. unset($allList[$pKey]);
  194. foreach ($allList as $cKey => $city) {
  195. if ($city['level'] == 2 && $city['pid'] == $province['id']) { // 城市
  196. $treeList[$province['id']]['city'][$city['id']] = $city;
  197. unset($allList[$cKey]);
  198. foreach ($allList as $rKey => $region) {
  199. if ($region['level'] == 3 && $region['pid'] == $city['id']) { // 地区
  200. $treeList[$province['id']]['city'][$city['id']]['region'][$region['id']] = $region;
  201. unset($allList[$rKey]);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. return $treeList;
  209. }
  210. /**
  211. * 从数据库中获取所有地区
  212. * @return array
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\DbException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. */
  217. private function getAllList(): array
  218. {
  219. $list = self::withoutGlobalScope()
  220. ->field('id, pid, name, level')
  221. ->select()
  222. ->toArray();
  223. return helper::arrayColumn2Key($list, 'id');
  224. }
  225. }