Region.php 6.8 KB

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