123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- declare (strict_types = 1);
- namespace app\common\model\chef;
- use app\common\library\helper;
- use app\common\model\BaseModel;
- use think\facade\Cache;
- /**
- * 地区
- * Class ChefAreas
- * @package app\common\model
- */
- class ChefAreas extends BaseModel
- {
- protected $name = 'areas';
- // protected $connection = 'chef';
- // 是否允许全局查询store_id
- protected $isGlobalScopeStoreId = false;
- /**
- * 类型自动转换
- * @var array
- */
- protected $type = [
- 'id' => 'integer',
- 'parent_id' => 'integer',
- 'level' => 'integer',
- ];
- // 当前数据版本号
- private static $version = '1.0.2';
- /**
- * 根据id获取地区名称
- * @param int $id
- * @return mixed|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getNameById(int $id = 0)
- {
- if ($id == 0) {
- return '其他';
- }
- $data = self::getCacheAll();
- return isset($data[$id]) ? $data[$id]['name'] : '其他';
- }
- /**
- * 获取所有地区(树状结构)
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getCacheTree()
- {
- return static::getCacheData('tree');
- }
- /**
- * 获取所有地区列表
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getCacheAll()
- {
- return static::getCacheData('all');
- }
- /**
- * 获取所有地区的总数
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getCacheCounts()
- {
- return static::getCacheData('counts');
- }
- /**
- * 获取缓存中的数据(存入静态变量)
- * @param null $item
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private static function getCacheData($item = null)
- {
- static $cacheData = [];
- if (empty($cacheData)) {
- $static = new static;
- $cacheData = $static->regionCache();
- }
- if (is_null($item)) {
- return $cacheData;
- }
- return $cacheData[$item];
- }
- /**
- * 获取地区缓存
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function regionCache()
- {
- // 缓存的数据
- $complete = Cache::get('region');
- // 如果存在缓存则返回缓存的数据,否则从数据库中查询
- // 条件1: 获取缓存数据
- // 条件2: 数据版本号要与当前一致
- if (
- !empty($complete)
- && isset($complete['version'])
- && $complete['version'] == self::$version
- ) {
- return $complete;
- }
- // 所有地区
- $allList = $tempList = $this->getAllList();
- // 已完成的数据
- $complete = [
- 'all' => $allList,
- 'tree' => $this->getTreeList($allList),
- 'counts' => $this->getCount($allList),
- 'version' => self::$version,
- ];
- // 写入缓存
- Cache::tag('cache')->set('region', $complete);
- return $complete;
- }
- /**
- * 地区总数
- * @param $allList
- * @return array
- */
- private static function getCount($allList)
- {
- $counts = [
- 'total' => count($allList),
- 'province' => 0,
- 'city' => 0,
- 'region' => 0,
- ];
- $level = [1 => 'province', 2 => 'city', 3 => 'region'];
- foreach ($allList as $item) {
- $counts[$level[$item['level']]]++;
- }
- return $counts;
- }
- /**
- * 格式化为树状格式
- * @param $allList
- * @return array
- */
- private function getTreeList($allList)
- {
- $treeList = [];
- $p = $c = $r = 0;
- foreach ($allList as $pKey => $province) {
- if ($province['level'] == 1) { // 省份
- $treeList[$p] = $province;
- unset($allList[$pKey]);
- $c = 0;
- foreach ($allList as $cKey => $city) {
- if ($city['level'] == 2 && $city['parent_id'] == $province['id']) { // 城市
- $treeList[$p]['city'][$c] = $city;
- unset($allList[$cKey]);
- $r = 0;
- foreach ($allList as $rKey => $region) {
- if ($region['level'] == 3 && $region['parent_id'] == $city['id']) { // 地区
- $treeList[$p]['city'][$c]['region'][$r] = $region;
- unset($allList[$rKey]);
- $r++;
- }
- }
- $c++;
- }
- }
- $p++;
- }
- }
- return $treeList;
- }
- /**
- * 从数据库中获取所有地区
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function getAllList()
- {
- $list = self::withoutGlobalScope()
- ->field('id, parent_id, name, level')
- ->select()
- ->toArray();
- return helper::arrayColumn2Key($list, 'id');
- }
- }
|