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.sunshine.SystemSetting.form.AddProjectInfoParameter;
import org.apache.commons.lang.StringUtils;

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

public class ExcelImportProjectInfoParameterNumberHandler extends ExcelAbstractHandler {

    private List<AddProjectInfoParameter> parameterList = new ArrayList<>();
    boolean checkFlag;
    private int maxRows=5000;   //导入的最大条数

    public List<AddProjectInfoParameter> getParameterList() {
        return parameterList;
    }

    public void setParameterList(List<AddProjectInfoParameter> parameterList) {
        this.parameterList = parameterList;
    }

    public boolean isCheckFlag() {
        return checkFlag;
    }

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

    @Override
    public int getMaxRows() {
        return maxRows;
    }

    @Override
    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    @Override
    public Boolean handler(Map<String, Object> rowData, int rowIndex) {
        AddProjectInfoParameter addProjectInfoParameter = transToModel(rowData);
        parameterList.add(addProjectInfoParameter);
        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>");
            }else if (!formatCode(rowData.get("5")).matches("^\\d+(\\.\\d{1,2})?$")){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：租凭年限从(含)必须是纯数字！" + "</p>");
                return true;
            }
            if (rowData.get("6") == null || StringUtils.isBlank(rowData.get("6").toString())) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：租凭年限到(含)为空" + "</p>");
            }else if (!formatCode(rowData.get("6")).matches("^\\d+(\\.\\d{1,2})?$")){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：租凭年限到(含)必须是纯数字！" + "</p>");
                return true;
            }
            parameterList.add(transToModel(rowData));
            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("租赁年限到(含)");
        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 AddProjectInfoParameter transToModel(Map<String, Object> rowData){
        AddProjectInfoParameter parameter = new AddProjectInfoParameter();
        parameter.setId(formatCode(rowData.get("1")));
        parameter.setCode(formatCode(rowData.get("2")));
        parameter.setParameterName(formatCode(rowData.get("3")));
        if (rowData.get("4") == null){
            parameter.setParameterValue(formatCode(rowData.get("4")));
        }else {
            parameter.setParameterValue(rowData.get("4").toString());
        }

        parameter.setLeaseTermFrom(formatCode(rowData.get("5")));
        parameter.setLeaseTermTo(formatCode(rowData.get("6")));
        return parameter;
    }


    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.toString());
    }

}
