package cn.pluss.platform.region.impl;

import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.dto.RegionReflectDTO;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.*;
import cn.pluss.platform.region.RegionReflectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 不同通道的地区表映射表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-07-24
 */
@Slf4j
@Service
public class RegionReflectServiceImpl extends ServiceImpl<RegionReflectMapper, RegionReflect> implements RegionReflectService {

    @Resource
    private RegionCodeLsMapper regionCodeLsMapper;

    @Resource
    private RegionCodeSxfMapper regionCodeSxfMapper;

    @Resource
    private RegionCodeRyxMapper regionCodeRyxMapper;

    @Resource
    private RegionCodeYsMapper regionCodeYsMapper;

    @Override
    public RegionCodeLs getLsRegionCode(String province, String city, String area) {
        try {
            RegionCodeLs regionCodeLs = regionCodeLsMapper.selectRegionCode(province, city, area);
            if (regionCodeLs == null) {
                throw new MsgException("获取地区信息异常");
            }
            return regionCodeLs;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("===获取地区信息异常===");
            throw new MsgException("获取地区信息异常");
        }
    }

    @Override
    public RegionCodeSxf getSxfRegionCode(String province, String city, String area) {
        try {
            RegionCodeSxf regionCodeYs = regionCodeSxfMapper.selectRegionCode(province, city, area);
            if (regionCodeYs == null) {
                throw new MsgException("随行付获取地区信息异常");
            }
            return regionCodeYs;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("===随行付获取地区信息异常===");
            throw new MsgException("随行付获取地区信息异常");
        }
    }

    @Override
    public RegionCodeRyx getRyxRegionCode(String province, String city, String area) {
        try {
            RegionCodeRyx regionCodeRyx = regionCodeRyxMapper.selectRegionCode(province, city, area);
            if (regionCodeRyx == null) {
                throw new MsgException("瑞银信获取地区信息异常");
            }
            return regionCodeRyx;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("===瑞银信获取地区信息异常===");
            throw new MsgException("瑞银信获取地区信息异常");
        }
    }

    @Override
    public RegionCodeYs getYsRegionCode(String province, String city, String area) {
        try {
            RegionCodeYs regionCodeYs = regionCodeYsMapper.selectRegionCode(province, city, area);
            if (regionCodeYs == null) {
                throw new MsgException("银盛获取地区信息异常");
            }

            return regionCodeYs;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("===银盛获取地区信息异常===");
            throw new MsgException("银盛获取地区信息异常");
        }
    }

    @Override
    public List<String> listProvince(String type) {

        switch (type) {
            case "sxf":
                return regionCodeSxfMapper.selectProvince();
            case "ryx":
                return regionCodeRyxMapper.selectProvince();
            default:
                return regionCodeYsMapper.selectProvince();
        }
    }

    @Override
    public List<String> listCity(String type, String province) {
        switch (type) {
            case "sxf":
                return regionCodeSxfMapper.selectCity(province);
            case "ryx":
                return regionCodeRyxMapper.selectCity(province);
            default:
                return regionCodeYsMapper.selectCity(province);
        }
    }

    @Override
    public List<String> listArea(String type, String city, String province) {
        switch (type) {
            case "sxf":
                return regionCodeSxfMapper.selectArea(province, city);
            case "ryx":
                return regionCodeRyxMapper.selectArea(province, city);
            default:
                return regionCodeYsMapper.selectArea(province, city);
        }
    }

    @Override
    public void saveReflect(RegionReflectDTO reflectDTO) {

        switch (reflectDTO.getType()) {
            case "sxf":
                val qWrapperSxf = new LambdaQueryWrapper<RegionCodeSxf>().eq(RegionCodeSxf::getProvince, reflectDTO.getProvince())
                        .eq(RegionCodeSxf::getCity, reflectDTO.getCity())
                        .eq(RegionCodeSxf::getArea, reflectDTO.getArea());
                val regionCodeSxf = regionCodeSxfMapper.selectOne(qWrapperSxf);
                reflectDTO.setRegionDes(regionCodeSxf.getId());
                reflectDTO.setChannel(1);
                break;
            case "ryx":
                val qWrapperRyx = new LambdaQueryWrapper<RegionCodeRyx>().eq(RegionCodeRyx::getProvince, reflectDTO.getProvince())
                        .eq(RegionCodeRyx::getCity, reflectDTO.getCity())
                        .eq(RegionCodeRyx::getArea, reflectDTO.getArea());
                val regionCodeRyx = regionCodeRyxMapper.selectOne(qWrapperRyx);
                reflectDTO.setRegionDes(regionCodeRyx.getId());
                reflectDTO.setChannel(3);
                break;
            default:
                val qWrapperYs = new LambdaQueryWrapper<RegionCodeYs>().eq(RegionCodeYs::getProvince, reflectDTO.getProvince())
                        .eq(RegionCodeYs::getCity, reflectDTO.getCity())
                        .eq(RegionCodeYs::getArea, reflectDTO.getArea());
                val regionCodeYs = regionCodeYsMapper.selectOne(qWrapperYs);
                reflectDTO.setRegionDes(regionCodeYs.getId());
                reflectDTO.setChannel(4);
                break;
        }

        val regionReflect = Converter.INSTANCE.toEntity(reflectDTO);
        val uWrapper = new LambdaUpdateWrapper<RegionReflect>();
        uWrapper.eq(RegionReflect::getRegionSrc, regionReflect.getRegionSrc())
                .set(RegionReflect::getRegionDes, regionReflect.getRegionDes());

        switch (reflectDTO.getType()) {
            case "sxf":
                uWrapper.eq(RegionReflect::getChannel, 1);
                break;
            case "ryx":
                uWrapper.eq(RegionReflect::getChannel, 3);
                break;
            default:
                uWrapper.eq(RegionReflect::getChannel, 4);
                break;
        }


        val updateCount = baseMapper.update(null, uWrapper);
        if (updateCount == 0) {
            baseMapper.insert(regionReflect);
        }
    }
}
