package cn.com.tenlion.systembase.util;

import cn.com.tenlion.systembase.dao.school.ISchoolDao;
import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO;
import cn.com.tenlion.systembase.pojo.model.school.SchoolModel;
import cn.com.tenlion.systembase.service.handleimportexcel.IHandleImportExcelService;
import ink.wgink.module.dictionary.pojo.dtos.AreaDTO;
import ink.wgink.module.dictionary.pojo.dtos.DataDTO;
import ink.wgink.mongo.module.dictionary.service.IMongoAreaService;
import ink.wgink.mongo.module.dictionary.service.IMongoDataService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导入excel数据的校验类
 * @author renpc
 */
@Component
public class ImportExcelHelper {

    @Autowired
    private IHandleImportExcelService handleImportExcelService;
    @Autowired
    private static IMongoDataService mongoDataService;
    @Autowired
    private static IMongoAreaService mongoAreaService;
    @Autowired
    private static ISchoolDao schoolDao;

    @Autowired
    public void init(ISchoolDao schoolDao) {
        ImportExcelHelper.schoolDao = schoolDao;
    }

    @Autowired
    public void init(IMongoDataService mongoDataService) {
        ImportExcelHelper.mongoDataService = mongoDataService;
    }

    @Autowired
    public void init(IMongoAreaService mongoAreaService) {
        ImportExcelHelper.mongoAreaService = mongoAreaService;
    }

    public static <T> String checkDataValid(T obj) {
        if(null == obj) {
            return null;
        }
        String returnStr = isMustInput(obj);
        return returnStr;
    }

    /**
     * 检查数据是否必填项缺失
     * @param obj
     * @param <T>
     * @return
     */
    private static <T> String isMustInput(T obj) {
        // 房屋信息
        if(obj instanceof SchoolModel) {
            Object temp = obj;
            SchoolModel schoolModel = (SchoolModel) temp;
            return checkSchoolData(schoolModel);
        }
        return "";
    }

    /**
     * 检查数据是否必填项缺失(房屋管理)
     * @param schoolModel
     * @return
     */
    private static String checkSchoolData(SchoolModel schoolModel) {
        if(StringUtils.isEmpty(schoolModel.getSchoolName())) {
            return "学校名称必填";
        }
        if(StringUtils.isEmpty(schoolModel.getSchoolAddress())) {
            return "学校地址必填";
        }
        if(StringUtils.isEmpty(schoolModel.getAreaName())) {
            return "行政区划";
        }
        return "";
    }

    /**
     * 从数据库比对数据，并转换为数据库数据
     * @param obj
     * @param <T>
     */
    public static <T> void checkDataFromDatabase(T obj) {
        if(obj instanceof SchoolModel) {
            Object temp = obj;
            SchoolModel schoolModel = (SchoolModel) temp;
            schoolCheck(schoolModel);
            temp = schoolModel;
            obj = (T) temp;
        }
    }

    /**
     * 从数据库比对数据，并转换为数据库数据(房屋管理)
     * @param schoolModel
     */
    private static void schoolCheck(SchoolModel schoolModel) {
        // 行政区划筛选
        if("市直".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("市辖区");
        }
        if("乌兰察布市察哈尔右翼前旗".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("察哈尔右翼前旗");
        }
        if("乌兰察布市教育局".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("乌兰察布市");
        }
        if("12000".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("乌兰察布市");
        }
        if("市直单位".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("乌兰察布市");
        }
        if("察右中旗".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("察哈尔右翼中旗");
        }
        if("察右后旗".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("察哈尔右翼后旗");
        }
        if("乌兰察布市兴和县".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("兴和县");
        }
        if("内蒙古自治区乌兰察布市兴和县".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("兴和县");
        }
        if("兴和县城关镇".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("城关镇");
        }
        if("兴和县赛乌素镇".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("赛乌素镇");
        }
        if("兴和县大库联".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("大库联乡");
        }
        if("兴和县团结乡".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("民族团结乡");
        }
        if("兴和县张皋镇".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("张皋镇");
        }
        if("兴和县大同夭".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("大同夭乡");
        }
        if("兴和县店子镇".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("店子镇");
        }
        if("内蒙古自治区乌兰察布市凉城县".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("凉城县");
        }
        if("察右前旗".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("察哈尔右翼前旗");
        }
        if("县市级".equals(schoolModel.getAreaName())) {
            schoolModel.setAreaName("丰镇市");
        }

        // 办学类型筛选
        if("中等职业技术学校".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("中等职业学校");
        }
        if("九年一贯制学校（初中）".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("九年一贯制学校");
        }
        if("九年一贯制".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("九年一贯制学校");
        }
        if("职高".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("职业高中");
        }
        if("民办".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("民办的其他高等教育机构");
        }
        if("九年一贯".equals(schoolModel.getRunningType())) {
            schoolModel.setAreaName("九年一贯制学校");
        }

        String remake = "源系统数据:\n";
        remake += "流水号:" + schoolModel.getSliceNumber() + "\n";
        remake += "批次号:" + schoolModel.getBatchNumber() + "\n";
        remake += "入库时间:" + schoolModel.getInsertTime() + "\n";
        remake += "MD5校验码:" + schoolModel.getMd5Check() + "\n";
        remake += "业务字段MD5:" + schoolModel.getBusinessMd5() + "\n";
        remake += "分片号:" + schoolModel.getSerialNumber() + "\n";
        remake += "记录状态:" + schoolModel.getRecordStatus() + "\n";
        remake += "处理状态:" + schoolModel.getHandleStatus() + "\n";
        remake += "建筑面积:" + schoolModel.getBuildArea() + "\n";
        remake += "类别:" + schoolModel.getSchoolType() + "\n";
        remake += "教职工人数:" + schoolModel.getWorkersCount() + "\n";
        remake += "面积:" + schoolModel.getArea() + "\n";
        schoolModel.setRemake(remake);

        Map<String, Object> params = new HashMap<>();
        params.put("keywords", schoolModel.getRunningType());
        params.put("dataParentId", "e5624543-4f8e-4550-a8e8-49235f23b287");
        ListPage page = new ListPage();
        page.setParams(params);
        SuccessResultList<List<DataDTO>> runningTypeDTO = mongoDataService.listPage(page);
        if(null != runningTypeDTO && null != runningTypeDTO.getRows() && runningTypeDTO.getRows().size() > 0) {
            DataDTO dataDTO = runningTypeDTO.getRows().get(0);
            schoolModel.setRunningType(dataDTO.getDataId());
            schoolModel.setRunningTypeName(dataDTO.getDataName());
            schoolModel.setRunningTypeCode(dataDTO.getDataCode());
        }
    }

}
