package cn.pluss.platform.areaCity.impl;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.areaCity.AreaCityService;
import cn.pluss.platform.entity.AreaCity;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.AreaCityMapper;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 * @description:
 * @author: Administrator
 * @DATE: 2021/7/16 11:03
 *
 */
@Service
@Slf4j
public class AreaCityServiceImpl implements AreaCityService {

    @Resource
    private AreaCityMapper areaCityMapper;

    /**
     *  String provinceName, String cityName, String areaName,String type
     * @date: 2021/7/23 15:03
     * @return cn.pluss.platform.api.Result<com.alibaba.fastjson.JSONObject>
     */
    @Override
    public Result<JSONObject> getAreaCodeByName(JSONObject params) {
        MsgException.checkBlank(params.getString("provinceName"),"缺失参数provinceName");
        MsgException.checkBlank(params.getString("cityName"),"缺失参数cityName");
        String type = StringUtil.isEmpty(params.getString("type")) ? "1" : params.getString("type");
        Map<String, String> areaCodeMap = areaCityMapper.getAreaCodeByName(params.getString("provinceName"),
                params.getString("cityName"), params.getString("areaName"),type);
        if(areaCodeMap != null){
            JSONObject result = new JSONObject(3);
            result.putAll(areaCodeMap);
            return ResultGenerator.genSuccessResult("获取成功",result);
        }
        return ResultGenerator.genSuccessResult("获取成功",null);
    }

    /**
     * 递归查询所有省市区数据
     * @date: 2022/2/25 10:33
     * @return java.util.List<cn.pluss.platform.entity.AreaCity>
     */
    @Override
    public List<AreaCity> queryProvince() {
        return areaCityMapper.queryProvince();
    }
    
    /**
     * 查询所省份
     * @date: 2022/2/25 10:33
     * @return java.util.List<cn.pluss.platform.entity.AreaCity>
     */
    @Override
    public List<AreaCity> queryOnlyProvince() {
        return areaCityMapper.queryOnlyProvince();
    }

    /**
     * queryParentCode
     * @date: 2022/2/25 11:04
     * @param areaCode:
     * @return java.util.List<cn.pluss.platform.entity.AreaCity>
     */
    @Override
    public List<AreaCity> queryParentCode(String areaCode) {
        return areaCityMapper.queryParentCode(areaCode);
    }
}
