<?php

namespace App\Services;

use App\Model\Area as AreaModel;

class AreaService extends BaseService
{
    /**
     * 检测省市区省市区
     * @param string $province_id
     * @param string $city_id
     * @param string $county_id
     * @return bool
     */
    public function checkAreaById(string $province_id, string $city_id, string $county_id): bool
    {
        // 检测省份
        if (!empty($province_id)){
            $province = $this->getAreaByAreaId($province_id);
            if (empty($province)){
                return false;
            }
        }

        // 检测城市
        if (!empty($city_id) && !empty($province_id)){
            $city = $this->getAreaByAreaId($city_id,$province_id);
            if (empty($city)){
                return false;
            }
        }

        // 检测区县
        if (!empty($county_id) && !empty($city_id)){
            $county = $this->getAreaByAreaId($county_id,$city_id);
            if (empty($county)){
                return false;
            }
        }

        if (!empty($province) || !empty($city) || !empty($county)){
            return true;
        }

        return false;
    }

    /**
     * 获取省市区数据
     * @param string $province_id
     * @param string $city_id
     * @param string $county_id
     * @return array
     */
    public function getAreaById(string $province_id, string $city_id, string $county_id): array
    {
        // 省份
        if (!empty($province_id)){
            $province = $this->getAreaByAreaId($province_id);
            if (empty($province)){
                return [];
            }
        }

        // 城市
        if (!empty($city_id) && !empty($province_id)){
            $city = $this->getAreaByAreaId($city_id,$province_id);
            if (empty($city)){
                return [];
            }
        }

        // 检测区县
        if (!empty($county_id) && !empty($city_id)){
            $county = $this->getAreaByAreaId($county_id,$city_id);
            if (empty($county)){
                return [];
            }
        }

        if (!empty($province) || !empty($city) || !empty($county)){
            $result = array();
            $result['province'] = $province ?? [];
            $result['city'] = $city ?? [];
            $result['county'] = $county ?? [];

            return $result;
        }

        return [];

    }

    /**
     * 获取省份列表
     * @return array
     */
    public function getProvince(): array
    {
        $params = array();
        $params['parent_id'] = 1;
        $params['area_type'] = 2;

        $province = AreaModel::getList($params);
        return empty($province) ? success() : success($province->toArray());
    }

    /**
     * 获取城市列表
     * @return array
     */
    public function getCity(): array
    {
        $area_id = $this->request->input('area_id');
        $city = $this->getAreaByParentId($area_id);
        return success($city);
    }

    /**
     * 获取区县信息
     * @return array
     */
    public function getCounty(): array
    {
        $area_id = $this->request->input('area_id');
        $county = $this->getAreaByParentId($area_id);
        return success($county);
    }

    /**
     * 查询省市区-地区id
     * @param string $area_id
     * @param string $parent_id
     * @return array
     */
    protected function getAreaByAreaId(string $area_id, string $parent_id = ""): array
    {
        $params = array();
        $params['area_id'] = $area_id;
        if (!empty($parent_id)){
            $params['parent_id'] = $parent_id;
        }
        $area = AreaModel::getOne($params);

        return empty($area) ? [] : $area->toArray();
    }

    /**
     * 查询省市区-父级id
     * @param string $parent_id
     * @return array
     */
    protected function getAreaByParentId(string $parent_id = ""): array
    {
        $params = array();
        $params['parent_id'] = $parent_id;
        $area = AreaModel::getList($params);

        return empty($area) ? [] : $area->toArray();
    }
}