Region.php 6.5 KB

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