package cn.pluss.platform.wxCityInfo.impl;

import cn.pluss.platform.entity.AreaCity;
import cn.pluss.platform.entity.WxCityInfo;
import cn.pluss.platform.mapper.AreaCityMapper;
import cn.pluss.platform.mapper.WxCityInfoMapper;
import cn.pluss.platform.wxCityInfo.WxCityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Transactional
@Service
public class WxCityInfoServiceImpl implements WxCityInfoService {

    @Resource
    private WxCityInfoMapper wxCityInfoMapper;

    @Resource
    private AreaCityMapper areaCityMapper;

    @Override
    public WxCityInfo queryWxCityInfo(WxCityInfo wxCityInfo) {
        String cityName = wxCityInfo.getCityName();
        String[] areaArr = cityName.split(",");

        AreaCity areaCityQuery = new AreaCity();
        AreaCity areaCity = new AreaCity();

        if (areaArr.length == 5 && "".equals(areaArr[1])) {
            areaArr[1] = areaArr[2];
            areaArr[2] = areaArr[3];
            areaArr[3] = areaArr[4];
        }

        if (!("".equals(areaArr[1]) || areaArr[1].equals(areaArr[2]))) {
            // 查省
            areaCityQuery.setType("1");
            areaCityQuery.setAreaName(areaArr[1]);
            areaCity = areaCityMapper.selectOne(new QueryWrapper<>(areaCityQuery));
            areaCityQuery.setType(null);
        }

        // 查市, 同一个省下面不可能存在两个同名市
        areaCityQuery.setParentAreaCode(areaCity.getAreaCode());
        areaCityQuery.setAreaName(areaArr[2]);
        areaCity = areaCityMapper.selectOne(new QueryWrapper<>(areaCityQuery));

        if (areaCity == null) {
            wxCityInfo.setCityCode("000000");
            return wxCityInfo;
        }
        // 查县/区/街道, type可能是3也可能是4
        areaCityQuery.setParentAreaCode(areaCity.getAreaCode());
        if (areaArr.length == 3) {
            // 解决一些不必要的问题
            wxCityInfo.setCityCode(areaCity.getAreaCode());
            return wxCityInfo;
        }
        areaCityQuery.setAreaName(areaArr[3]);
        areaCity = areaCityMapper.selectOne(new QueryWrapper<>(areaCityQuery));
        if (areaCity == null) {
            wxCityInfo.setCityCode("000000");
            return wxCityInfo;
        }

        wxCityInfo.setCityCode(areaCity.getAreaCode());

        return wxCityInfo;
    }

    @Override
    public List<WxCityInfo> queryWxCityInfoList(WxCityInfo wxCityInfo) {
        return wxCityInfoMapper.queryWxCityInfoList(wxCityInfo);
    }

    @Override
    public void saveWxCityInfo(WxCityInfo wxCityInfo) {
        wxCityInfoMapper.saveWxCityInfo(wxCityInfo);
    }

    @Override
    public void updateWxCityInfo(WxCityInfo wxCityInfo) {
        wxCityInfoMapper.updateWxCityInfo(wxCityInfo);
    }

    @Override
    public void deleteWxCityInfo(WxCityInfo wxCityInfo) {
        wxCityInfoMapper.deleteWxCityInfo(wxCityInfo);
    }

    @Override
    public List<WxCityInfo> queryWxCityInfoPage(Map map) {
        return wxCityInfoMapper.queryWxCityInfoPage(map);
    }

    @Override
    public Integer queryWxCityInfoPageCount(Map map) {
        return wxCityInfoMapper.queryWxCityInfoPageCount(map);
    }

    @Override
    public void saveWxCityInfoBatch(List<WxCityInfo> wxCityInfoList) {
        wxCityInfoMapper.saveWxCityInfoBatch(wxCityInfoList);
    }
}

