<?php


namespace App\Http\Services;


//后台用户相关信息服务
use App\Model\RedisModel;
use App\Model\RegionModel;
use Illuminate\Support\Facades\DB;

//用于判断是否已经查看的服务
class RegionService
{
    public function getCityRegionData()
    {
        $regionModel = new RegionModel();
        $data = $regionModel->whereIn('region_type', [1,2])->get()->toArray();
        $topRegions = [];
        foreach ($data as $region) {
            if ($region['parent_id'] == 1) {
                $topRegions[] = [
                    'id' => $region['region_id'],
                    'name' => $region['region_name'],
                    'children'=>[],
                ];
            }
        }
        foreach ($topRegions as &$topRegion) {
            foreach ($data as $region) {
                if ($region['parent_id'] == 1) {
                    continue;
                }
                if ($region['parent_id'] == $topRegion['id']) {
                    $topRegion['children'][] = [
                        'id' => $region['region_id'],
                        'name' => $region['region_name'],
                    ];
                }
            }
        }
        unset($topRegion);
        $regionData = $topRegions;
        return $regionData;
    }

    public function getRegionNameByIds($ids)
    {
        $ids = array_filter($ids, function ($id) {
            return !empty($id);
        });
        if (empty($ids)) {
            return [];
        }
        $model = new RegionModel();
        return $model->whereIn('region_id', $ids)->pluck('region_name')->toArray();
    }
}