<?php

namespace App\Http\Controllers\V1;

use App\Models\Help;
use App\Models\Region;
use Illuminate\Http\Request;
use App\Models\Advertise;
use App\Models\Setting;
use Hamcrest\Type\IsString;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class RegionController extends Controller
{
    //全国地区
    public function allRegion()
    {
        $regionList = Redis::get('all_region');
        if(!empty($regionList)){
            return $this->success(json_decode($regionList, true));
        }

        $list = Region::query()
            ->where('type', 1)
            ->select('code', 'full_name')
            ->get()
            ->toArray();

        foreach ($list as $key => $val) {
            $cityList = Region::query()->where('parent_code', $val['code'])->select('code', 'full_name')->get()->toArray();
            foreach ($cityList as $k => $v) {
                $areaList = Region::query()->where('parent_code', $v['code'])->select('code', 'full_name')->get()->toArray();
                $v['area_list'] = $areaList;
                $cityList[$k] = $v;
            }
            $val['city_list'] = $cityList;
            $list[$key] = $val;
        }

        Redis::setex('all_region', 24*3600, json_encode($list));

        return $this->success($list);
    }

}
