package com.sifang.modules.sunshine.SystemSetting.handler;

import com.google.common.collect.Maps;
import com.sifang.modules.order.utils.excelExt.handler.ExcelAbstractHandler;
import com.sifang.modules.sunshine.SystemSetting.form.AddProjectInfo;
import com.sifang.modules.supply.stock.form.AddMaterial;
import org.apache.commons.lang.StringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelImportProjectInfoNumberHandler extends ExcelAbstractHandler {


    private  List<AddProjectInfo> addProjectList = new ArrayList<>();

    boolean checkFlag;
    private int maxRows=5000;   //导入的最大条数

    /**
     * 如果返回false，则表示终止继续遍历Excel,截至到当前的行数
     */
    @SuppressWarnings("unchecked")
    @Override
    public Boolean handler(Map<String, Object> rowData, int rowIndex) {
        AddProjectInfo addProjectInfo = transToModel(rowData);
        addProjectList.add(addProjectInfo);
        return true;
    }


    @Override
    public Boolean checkExcel(Map<String, Object> rowData, int rowIndex) {
        Map<String, Object> result = (Map<String, Object>) this.getHandlerResult();
        if (result == null) {
            result = Maps.newHashMap();
            this.setHandlerResult(result);
        }
        // check excel title match
        if (rowIndex == (getStartRow() - 1)) {//如果是title列，则直接检查是否同一个Excel模板,包括顺序和列名匹配
            boolean match = matchTemplate(rowData);
            boolean flag = true;
            if(!match){
                result.put("TemplateError", "<p>模板不匹配</p>");
                flag = false;
            }
            return flag;
        }
    if (rowIndex > (getStartRow() -1)){
        checkFlag = true;
        if (rowData.get("1")== null || StringUtils.isBlank(rowData.get("1").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：序号为空" + "</p>");
        }
        if (rowData.get("2")== null || StringUtils.isBlank(String.valueOf(rowData.get("2")))){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：产品编号为空" + "</p>");
        }
        if (rowData.get("3")== null || StringUtils.isBlank(rowData.get("3").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：产品名称为空" + "</p>");
        }
        if (rowData.get("4")== null || StringUtils.isBlank(rowData.get("4").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：所属产品线为空" + "</p>");
        }
        if (rowData.get("5")== null || StringUtils.isBlank(rowData.get("5").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：备案方式为空" + "</p>");
        }
        if (rowData.get("6")== null || StringUtils.isBlank(rowData.get("6").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：省为空" + "</p>");
        }
        if (rowData.get("7")== null || StringUtils.isBlank(rowData.get("7").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：市为空" + "</p>");
        }
        if (rowData.get("8")== null || StringUtils.isBlank(rowData.get("8").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：EPC公司为空" + "</p>");
        }
//        if (rowData.get("9")== null || StringUtils.isBlank(rowData.get("9").toString())){
//            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：所属项目公司为空" + "</p>");
//        }
//        if (rowData.get("10")== null || StringUtils.isBlank(String.valueOf(rowData.get("10")))){
//            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：EPC单价为空" + "</p>");
//        }else
        if (rowData.get("10") != null && rowData.get("10") != ""){
            if (!formatCode(rowData.get("10")).matches("(^[1-9]([0-9]+)?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)")) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：EPC单价格式不正确！" + "</p>");
                return true;
            }
        }
        if (rowData.get("11")== null || StringUtils.isBlank(rowData.get("11").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：租金支付周期为空" + "</p>");
        }
        if (rowData.get("12")== null || StringUtils.isBlank(rowData.get("12").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：组件规格为空" + "</p>");
        }else if (!formatCode(rowData.get("12")).matches("^\\d+(\\.\\d{1,2})?$")){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：组件规格必须是纯数字！" + "</p>");
            return true;
        }
        if (rowData.get("13")== null || StringUtils.isBlank(rowData.get("13").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：是否国网授权为空" + "</p>");
        }else {
            if (!(rowData.get("13").toString().equals("是") || rowData.get("13").toString().equals("否"))){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：是否国网授权内容不正确" + "</p>");
            }
        }
        if (rowData.get("14")== null || StringUtils.isBlank(rowData.get("14").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：资方名称为空" + "</p>");
        }
        if (rowData.get("15")== null || StringUtils.isBlank(rowData.get("15").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：是否购买保险为空" + "</p>");
        }else {
            if (!(rowData.get("15").toString().equals("是") || rowData.get("15").toString().equals("否"))){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：是否购买保险内容不正确" + "</p>");
            }
        }
        if (rowData.get("16")== null || StringUtils.isBlank(rowData.get("16").toString())){
            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：保险名称为空" + "</p>");
        }

        addProjectList.add(transToModel(rowData));
           /* if (isLastRow(rowIndex)) {
                checkMaterialCodeExists(retList);
            }*/
        if (rowIndex > maxRows) {
            result.put("overLimit", "overLimit");
            return false;
        }
    }

        return true;


}

    private boolean matchTemplate(Map<String, Object> rowData){
        List<String> titles = new ArrayList<>();
        titles.add("序号");
        titles.add("产品编号");
        titles.add("产品名称");
        titles.add("所属产品线");
        titles.add("备案方式");
        titles.add("省");
        titles.add("市");
        titles.add("EPC公司");
        titles.add("项目公司名称");
        titles.add("EPC单价");
        titles.add("租金支付周期");
        titles.add("组件规格");
        titles.add("是否国网授权");
        titles.add("资方名称");
        titles.add("是否购买保险");
        titles.add("保险名称");
        return checkTemplate(titles, rowData);
    }

    public boolean checkTemplate(List<String> titles, Map<String, Object> rowData){
        boolean ret = true;
        for (int i = 0; i < titles.size(); i++) {
            String title = String.valueOf(rowData.get(String.valueOf(i + 1)));
            if (!titles.get(i).equals(title.trim())) {
                //模板不匹配
                ret = false;
                break;
            }
        }
        return ret;
    }

    private String formatCode(Object code) {
        if (code == null) {
            return "";
        }
        Object codeVal = code;
        if (codeVal instanceof Double) {
            codeVal = new DecimalFormat("#").format((Double) codeVal);
        } else {
            codeVal = code;
        }
        return String.valueOf(codeVal);
    }


    private AddProjectInfo transToModel(Map<String, Object> rowData){
        AddProjectInfo addProjectInfo = new AddProjectInfo();
        addProjectInfo.setCode(formatCode(rowData.get("2")));
        addProjectInfo.setName(formatCode(rowData.get("3")));
        addProjectInfo.setProductLineId(formatCode(rowData.get("4")));
        addProjectInfo.setFilingType(formatCode(rowData.get("5")));
        addProjectInfo.setProvince(formatCode(rowData.get("6")));
        addProjectInfo.setCity(formatCode(rowData.get("7")));
        addProjectInfo.setEpcCompanyId(formatCode(rowData.get("8")));
        addProjectInfo.setProjectCompanyId(formatCode(rowData.get("9")));
        addProjectInfo.setEpcPrice(formatCode(rowData.get("10")));
        addProjectInfo.setPayCycle(formatCode(rowData.get("11")));
        addProjectInfo.setAssemblySpecs(formatCode(rowData.get("12")));
        addProjectInfo.setStateAuthorizedFlag(formatCode(rowData.get("13")));
        addProjectInfo.setManagementType(formatCode(rowData.get("14")));
        addProjectInfo.setBuyInsuranceFlag(formatCode(rowData.get("15")));
        addProjectInfo.setInsuranceName(formatCode(rowData.get("16")));
        return addProjectInfo;
    }

    public List<AddProjectInfo> getAddProjectList() {
        return addProjectList;
    }

    public void setAddProjectList(List<AddProjectInfo> addProjectList) {
        this.addProjectList = addProjectList;
    }

    public boolean isCheckFlag() {
        return checkFlag;
    }

    public void setCheckFlag(boolean checkFlag) {
        this.checkFlag = checkFlag;
    }


}
