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