package com.energy.manage.service.service.area.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.po.area.AreaPO;
import com.energy.manage.common.po.windfield.WindFieldPO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.service.domain.vo.area.ProvinceVo;
import com.energy.manage.service.domain.vo.excel.AreaCodeExcelVo;
import com.energy.manage.service.domain.vo.excel.MinTestDataVo;
import com.energy.manage.service.mappers.area.AreaMapper;
import com.energy.manage.service.service.area.AreaApiService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.List;

@Service
public class AreaApiServiceImpl extends BaseServiceImpl<AreaPO> implements AreaApiService {

    @Value("${spring.datasource.druid.url}")
    private String druid;



    @Autowired
    private AreaMapper areaMapper;

    /**
     * 获取所有的省份信息
     *
     * @return
     */
    @Override
    public List<AreaPO> findAllProvince() {
        return areaMapper.findAllProvince();
    }

    @Override
    public List<AreaPO> getAllProvinceByName(Integer type,List<String> names) {
        return areaMapper.selectAreaByNames(type,names);
    }


    /**
     * 根据省份信息获取对应的城市信息
     *
     * @param provinceId
     * @return
     */
    @Override
    public List<AreaPO> selectCityByPId(int provinceId) {
        return areaMapper.selectCityByPId(provinceId);
    }

    /**
     * 根据城市信息获取对应的区县信息
     *
     * @param cityId
     * @return
     */
    @Override
    public List<AreaPO> selectAreaByCId(int cityId) {
        return areaMapper.selectAreaByCId(cityId);
    }

    /**
     * 根据区县Id修改区县信息
     *
     * @param areaId
     * @param area
     * @return
     */
    @Override
    public boolean updateAreaByAId(int areaId, String area, int areaType) {
        return areaMapper.updateAreaByAId(areaId, area, areaType) > 0;
    }

    /**
     * 根据区县Id删除区县信息
     *
     * @param areaId
     * @return
     */
    @Override
    public JSONObject deleteAreaByAId(int areaId, int areaType) {
        if (2 == areaType) {
            List<AreaPO> list = areaMapper.selectAreaByCId(areaId);
            if (!CollectionUtils.isEmpty(list)) {
                return RespMsg.FAIL("当前城市下有区县，不允许删除");
            }
        } else {
            return RespMsg.INVALIDPARAM("type");
        }
        boolean flag = areaMapper.deleteAreaById(areaId) > 0;
        if (flag) {
            return RespMsg.SUCCESS();
        }
        return RespMsg.FAIL();
    }

    /**
     * 获取未被占用的省份
     *
     * @param provinceName
     * @return
     */
    @Override
    public List<AreaPO> getEnableProvince(String provinceName) {
        return areaMapper.getEnableProvince(provinceName);
    }

    /**
     * 获取未被占用的地区信息
     *
     * @return
     */
    @Override
    public JSONObject getEnableArea() {
        List<ProvinceVo> list = areaMapper.getEnableArea();
        return RespMsg.SUCCESS(list);
    }

    @Override
    public boolean addArea(int provinceId, String province, Integer cityId, String city, String area, int areaType) {
        if (2 == areaType) {
            return areaMapper.insertArea(province, city, "", 2, provinceId) > 0;
        } else if (3 == areaType) {
            return areaMapper.insertArea(province, city, area, 3, cityId) > 0;
        } else {
            return false;
        }
    }

    @Override
    public boolean syncAreaCode() {
        List<AreaPO> list = areaMapper.findAll();
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        File file = FileUtil.file("/Users/shiyue/Downloads/area_code.xlsx");
        List<AreaCodeExcelVo> areaCodeExcelVos = EasyExcel.read(file).head(AreaCodeExcelVo.class).sheet().doReadSync();

        AreaPO areaPO = null;
        List<AreaPO> areaPOList = Lists.newArrayList();

        for (AreaPO po : list) {
            areaPO = new AreaPO();
            for (AreaCodeExcelVo vo : areaCodeExcelVos) {
                if (po.getCity() != null && vo.getCityName() != null) {
                    if (po.getCity().equals(vo.getCityName())) {
                        areaPO.setAreaId(po.getAreaId());
                        areaPO.setAreaCode(vo.getAreaCode());
                        areaPOList.add(areaPO);
                    }
                }
            }
        }
        for(AreaPO areaPO1 :areaPOList){
            System.out.println(areaPO1.getAreaId() + "====>" + areaPO1.getAreaCode());
        }
        return areaMapper.updateAreaCodes(areaPOList) > 0;
    }

}
