ChefAreas.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\model\chef;
  4. use app\common\library\helper;
  5. use app\common\model\BaseModel;
  6. use think\facade\Cache;
  7. /**
  8. * 地区
  9. * Class ChefAreas
  10. * @package app\common\model
  11. */
  12. class ChefAreas extends BaseModel
  13. {
  14. protected $name = 'areas';
  15. // protected $connection = 'chef';
  16. // 是否允许全局查询store_id
  17. protected $isGlobalScopeStoreId = false;
  18. /**
  19. * 类型自动转换
  20. * @var array
  21. */
  22. protected $type = [
  23. 'id' => 'integer',
  24. 'parent_id' => 'integer',
  25. 'level' => 'integer',
  26. ];
  27. // 当前数据版本号
  28. private static $version = '1.0.2';
  29. /**
  30. * 根据id获取地区名称
  31. * @param int $id
  32. * @return mixed|string
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public static function getNameById(int $id = 0)
  38. {
  39. if ($id == 0) {
  40. return '其他';
  41. }
  42. $data = self::getCacheAll();
  43. return isset($data[$id]) ? $data[$id]['name'] : '其他';
  44. }
  45. /**
  46. * 获取所有地区(树状结构)
  47. * @return array|mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public static function getCacheTree()
  53. {
  54. return static::getCacheData('tree');
  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 getCacheAll()
  64. {
  65. return static::getCacheData('all');
  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 getCacheCounts()
  75. {
  76. return static::getCacheData('counts');
  77. }
  78. /**
  79. * 获取缓存中的数据(存入静态变量)
  80. * @param null $item
  81. * @return array|mixed
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. private static function getCacheData($item = null)
  87. {
  88. static $cacheData = [];
  89. if (empty($cacheData)) {
  90. $static = new static;
  91. $cacheData = $static->regionCache();
  92. }
  93. if (is_null($item)) {
  94. return $cacheData;
  95. }
  96. return $cacheData[$item];
  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. private function regionCache()
  106. {
  107. // 缓存的数据
  108. $complete = Cache::get('region');
  109. // 如果存在缓存则返回缓存的数据,否则从数据库中查询
  110. // 条件1: 获取缓存数据
  111. // 条件2: 数据版本号要与当前一致
  112. if (
  113. !empty($complete)
  114. && isset($complete['version'])
  115. && $complete['version'] == self::$version
  116. ) {
  117. return $complete;
  118. }
  119. // 所有地区
  120. $allList = $tempList = $this->getAllList();
  121. // 已完成的数据
  122. $complete = [
  123. 'all' => $allList,
  124. 'tree' => $this->getTreeList($allList),
  125. 'counts' => $this->getCount($allList),
  126. 'version' => self::$version,
  127. ];
  128. // 写入缓存
  129. Cache::tag('cache')->set('region', $complete);
  130. return $complete;
  131. }
  132. /**
  133. * 地区总数
  134. * @param $allList
  135. * @return array
  136. */
  137. private static function getCount($allList)
  138. {
  139. $counts = [
  140. 'total' => count($allList),
  141. 'province' => 0,
  142. 'city' => 0,
  143. 'region' => 0,
  144. ];
  145. $level = [1 => 'province', 2 => 'city', 3 => 'region'];
  146. foreach ($allList as $item) {
  147. $counts[$level[$item['level']]]++;
  148. }
  149. return $counts;
  150. }
  151. /**
  152. * 格式化为树状格式
  153. * @param $allList
  154. * @return array
  155. */
  156. private function getTreeList($allList)
  157. {
  158. $treeList = [];
  159. $p = $c = $r = 0;
  160. foreach ($allList as $pKey => $province) {
  161. if ($province['level'] == 1) { // 省份
  162. $treeList[$p] = $province;
  163. unset($allList[$pKey]);
  164. $c = 0;
  165. foreach ($allList as $cKey => $city) {
  166. if ($city['level'] == 2 && $city['parent_id'] == $province['id']) { // 城市
  167. $treeList[$p]['city'][$c] = $city;
  168. unset($allList[$cKey]);
  169. $r = 0;
  170. foreach ($allList as $rKey => $region) {
  171. if ($region['level'] == 3 && $region['parent_id'] == $city['id']) { // 地区
  172. $treeList[$p]['city'][$c]['region'][$r] = $region;
  173. unset($allList[$rKey]);
  174. $r++;
  175. }
  176. }
  177. $c++;
  178. }
  179. }
  180. $p++;
  181. }
  182. }
  183. return $treeList;
  184. }
  185. /**
  186. * 从数据库中获取所有地区
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. private function getAllList()
  193. {
  194. $list = self::withoutGlobalScope()
  195. ->field('id, parent_id, name, level')
  196. ->select()
  197. ->toArray();
  198. return helper::arrayColumn2Key($list, 'id');
  199. }
  200. }