Region.php 6.6 KB

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