package thyyxxk.webserver.service.zygl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.zygl.RegionDao;
import thyyxxk.webserver.entity.zygl.CodeNameEntity;

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

/**
 * @Description:
 * @Author:lihong
 * @Date: 2025/5/10
 */
@Service
@Slf4j
public class RegionService {

    @Resource
    private RegionDao regionMapper;
    public List<CodeNameEntity> queryProvince() {
        return regionMapper.selectProvince();
    }

    public CodeNameEntity queryByCode(String code) {
        return regionMapper.selectByCode(code);
    }

    public List<CodeNameEntity> queryByParentCode(String parentCode) {
        return regionMapper.selectByParentCode(parentCode);
    }

    public CodeNameEntity queryByName(String name,String parentCode) {
        return regionMapper.selectByName(name,parentCode);
    }

    public List<CodeNameEntity> queryAll() {
        return regionMapper.selectAll();
    }

    /**
     * @param result
     * @description: 根据中文地址 获取 省市区县编码 参数  province  city  district
     * @author: lihong
     * @date: 2025/2/12 15:46
     * @param: result
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    public Map<String, Object> getAddressCodes(Map<String, String> result) {
        Map<String, Object> codeMap = new HashMap<>();
        String province = result.get("province");
        String city = result.get("city");
        String district = result.get("district");
        try {
            if(StrUtil.isBlank(province)){
                putByCity(codeMap,city,district);
            }else {
                CodeNameEntity codeNameEntity = regionMapper.selectByName(province, null);
                if(codeNameEntity != null){
                    codeMap.put("provinceCode",codeNameEntity.getCode());
                    if(StrUtil.isBlank(city)){
                        putByDistrict(codeMap, district, codeNameEntity);
                    }else {
                        CodeNameEntity tempCity = regionMapper.selectByName(city, codeNameEntity.getCode());
                        if(tempCity != null){
                            codeMap.put("cityCode",tempCity.getCode());
                            if(StrUtil.isNotBlank(district)){
                                CodeNameEntity districtEntity = regionMapper.selectByName(district, tempCity.getCode());
                                codeMap.put("districtCode",districtEntity.getCode());
                            }
                        }else {
                            if(StrUtil.isBlank(district)){
                                List<CodeNameEntity> districtEntity = regionMapper.getByName(city);
                                if(CollUtil.isNotEmpty(districtEntity)){
                                    codeMap.put("cityCode",districtEntity.get(0).getParentCode());
                                    codeMap.put("districtCode",districtEntity.get(0).getCode());
                                }
                            }else {
                                putByDistrict(codeMap, district, codeNameEntity);
                            }
                        }
                    }
                }
                else {
                    putByCity(codeMap, city, district);
                }
            }
        }catch (Exception e){
            log.error("根据中文地址获取省市区县编码报错{}",e);
        }
        return codeMap;
    }

    private void putByDistrict(Map<String, Object> codeMap, String district, CodeNameEntity codeNameEntity) {
        if(StrUtil.isNotBlank(district)){
            List<CodeNameEntity> nameEntities = regionMapper.getByName(district);
            if(CollUtil.isNotEmpty(nameEntities)){
                if(nameEntities.size() == 1){
                    codeMap.put("cityCode", nameEntities.get(0).getParentCode());
                    codeMap.put("districtCode", nameEntities.get(0).getCode());
                }else {
                    for(CodeNameEntity entity : nameEntities){
                        CodeNameEntity parentEntity = regionMapper.selectByCode(entity.getParentCode());
                        if(parentEntity.getParentCode().equals(codeNameEntity.getCode())){
                            codeMap.put("cityCode", entity.getParentCode());
                            codeMap.put("districtCode", entity.getCode());
                            break;
                        }
                    }
                }
            }
        }
    }

    private void putByCity(Map<String, Object> codeMap, String city, String district) {
        if(StrUtil.isNotBlank(city)){
            List<CodeNameEntity> cityEntityList = regionMapper.getByName(city);
            if(CollUtil.isNotEmpty(cityEntityList)){
                codeMap.put("provinceCode",cityEntityList.get(0).getParentCode());
                codeMap.put("cityCode",cityEntityList.get(0).getCode());
                if(StrUtil.isNotBlank(district)){
                    CodeNameEntity districtEntity = regionMapper.selectByName(district, cityEntityList.get(0).getCode());
                    codeMap.put("districtCode",districtEntity.getCode());
                }
            }
        }
    }
}
