<?php

namespace app\lib\service;


use think\Db;

class AddressService
{

    /**同步请求
     * @param int $ID
     * @param int $type
     */
    public static function getAddrInfo($ID=0,$type=0){
        if($type==1){//知道村委会ID
            $addrVillage=Db::name("addr_village")->where(['village_code'=>$ID])->field('village_code as value,village as name,town_code as ParentId')->find();
            $addrTown=Db::name("addr_village")->where(['town_code'=>$addrVillage['ParentId']])->field('town_code as value,town as name,county_code as ParentId')->find();
            $addrArea=Db::name("addr")->where(['ID'=>$addrTown['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrCity=Db::name("addr")->where(['ID'=>$addrArea['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrProvince=Db::name("addr")->where(['ID'=>$addrCity['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            return ["village"=>$addrVillage,"town"=>$addrTown,'area'=>$addrArea,'city'=>$addrCity,'province'=>$addrProvince];
        }else if($type==2){//知道街道id
            $addrTown=Db::name("addr_village")->where(['town_code'=>$ID])->field('town_code as value,town as name,county_code as ParentId')->find();
            $addrArea=Db::name("addr")->where(['ID'=>$addrTown['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrCity=Db::name("addr")->where(['ID'=>$addrArea['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrProvince=Db::name("addr")->where(['ID'=>$addrCity['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            return ["town"=>$addrTown,'area'=>$addrArea,'city'=>$addrCity,'province'=>$addrProvince];
        }else if ($type==3){//知道区县id
            $addrArea=Db::name("addr")->where(['ID'=>$ID])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrCity=Db::name("addr")->where(['ID'=>$addrArea['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            $addrProvince=Db::name("addr")->where(['ID'=>$addrCity['ParentId']])->field('ID as value,Name as name, ParentId,MergerName')->find();
            return ['area'=>$addrArea,'city'=>$addrCity,'province'=>$addrProvince];
        }
        return ;
    }


    /**
     * 异步请求 读取省市区数据,联动列表
     */
    public static function getAddress($params)
    {
        $where = ['LevelType' => 1];
        
        if(isset($params['province_id'])){
            $where['ParentId'] = $params['province_id'];
            $where['LevelType'] = 2;
        }
        if(isset($params['city_id'])){
            $where['ParentId'] = $params['city_id'];
            $where['LevelType'] = 3;
        }

        $addrList = Db::name("addr")->where($where)->field('ID as value,Name as name')->select();
        //街道、村委会
        if(isset($params['area_id'])){
            $map['county_code'] = $params['area_id'];
            $addrList = Db::name("addr_village")->where($map)->field('town_code as value,town as name')->group("town_code")->select();
        }

        if(isset($params['town_id'])){
            $map['town_code'] = $params['town_id'];
            $addrList = Db::name("addr_village")->where($map)->field('village_code as value,village as name')->select();
        }

        return $addrList;
    }
}