package com.sifang.modules.insurance.handler;

import com.google.common.collect.Maps;
import com.sifang.modules.insurance.entity.CrmCustomerEntryInsurance;
import com.sifang.modules.insurance.entity.CrmCustomerNoSystemEntryInsurance;
import com.sifang.modules.order.utils.excelExt.handler.ExcelAbstractHandler;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class InsuranceNoSystemExcelImportHandler extends ExcelAbstractHandler {

    private List<CrmCustomerNoSystemEntryInsurance> retList = new ArrayList<>();

    private List<String> insuranceCodeList = new ArrayList<>();

    boolean checkFlag;

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

    @Override
    public Boolean handler(Map<String, Object> rowData, int rowIndex) {
        CrmCustomerNoSystemEntryInsurance addTrader = transToModel(rowData);
        retList.add(addTrader);
        return true;
    }

    private CrmCustomerNoSystemEntryInsurance transToModel(Map<String, Object> rowData){
        CrmCustomerNoSystemEntryInsurance insurance = new CrmCustomerNoSystemEntryInsurance();
        insurance.setItemNo(formatCode(rowData.get("1")));
        insurance.setInsuranceMoney(new BigDecimal(rowData.get("2").toString()));
        insurance.setTenantName(formatCode(rowData.get("3")));
        insurance.setProvince(formatCode(rowData.get("4")));
        insurance.setCity(formatCode(rowData.get("5")));
        insurance.setArea(formatCode(rowData.get("6")));
        insurance.setAddress(formatCode(rowData.get("7")));
        insurance.setTraderName(formatCode(rowData.get("8")));
        insurance.setProjectName(formatCode(rowData.get("9")));
        insurance.setProjectCompanyName(formatCode(rowData.get("10")));
        insurance.setPartnerName(formatCode(rowData.get("11")));
        insurance.setInsuranceCompanyName(formatCode(rowData.get("12")));
        insurance.setPolicyholder(formatCode(rowData.get("13")));
        insurance.setInsurant(formatCode(rowData.get("14")));
        try {
            insurance.setStartTime(DateUtils.convertExcelDateDayToJavaDate(Double.parseDouble(String.valueOf(rowData.get("15")))));
            insurance.setEndTime(DateUtils.convertExcelDateDayToJavaDate(Double.parseDouble(String.valueOf(rowData.get("16")))));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return insurance;
    }

    //匹配模板
    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("代理商");
        titles.add("产品名称");
        titles.add("项目公司名称");
        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;
    }

    /**
     * 如果返回false，则表示终止继续遍历Excel,截至到当前的行数
     */
    //验证Excel模板
    @SuppressWarnings("unchecked")
    @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);
        }
        //如果是title列，则直接检查是否同一个Excel模板,包括顺序和列名匹配
        if (rowIndex == (getStartRow() - 1)) {
            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(String.valueOf(rowData.get("1")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：电站编码为空" + "</p>");
                return true;
            }

            if (rowData.get("2") == null || StringUtils.isBlank(String.valueOf(rowData.get("2")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：投保金额为空" + "</p>");
                return true;
            }

            if (rowData.get("3") == null || StringUtils.isBlank(String.valueOf(rowData.get("3")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：业主姓名为空" + "</p>");
                return true;
            }

            if (rowData.get("4") == null || StringUtils.isBlank(String.valueOf(rowData.get("4")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：项目所属省为空" + "</p>");
                return true;
            }
            if (rowData.get("5") == null || StringUtils.isBlank(String.valueOf(rowData.get("5")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：项目所属市为空" + "</p>");
                return true;
            }
            if (rowData.get("6") == null || StringUtils.isBlank(String.valueOf(rowData.get("6")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：项目所属区为空" + "</p>");
                return true;
            }

            if (rowData.get("7") == null || StringUtils.isBlank(String.valueOf(rowData.get("7")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：项目详细地址为空" + "</p>");
                return true;
            }

            if (rowData.get("8") == null || StringUtils.isBlank(String.valueOf(rowData.get("8")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：代理商为空" + "</p>");
                return true;
            }

            if (rowData.get("9") == null || StringUtils.isBlank(String.valueOf(rowData.get("9")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：产品名称为空" + "</p>");
                return true;
            }

            if (rowData.get("10") == null || StringUtils.isBlank(String.valueOf(rowData.get("10")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：项目公司名称为空" + "</p>");
                return true;
            }

            if (rowData.get("11") == null || StringUtils.isBlank(String.valueOf(rowData.get("11")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：资方为空" + "</p>");
                return true;
            }


            if (rowData.get("12") == null || StringUtils.isBlank(String.valueOf(rowData.get("12")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：投保公司为空" + "</p>");
                return true;
            }

            if (rowData.get("13") == null || StringUtils.isBlank(String.valueOf(rowData.get("13")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：投保人为空" + "</p>");
                return true;
            }

            if (rowData.get("14") == null || StringUtils.isBlank(String.valueOf(rowData.get("14")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：被保人为空" + "</p>");
                return true;
            }

            if (rowData.get("15") == null || StringUtils.isBlank(String.valueOf(rowData.get("15")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：保险开始时间为空" + "</p>");
                return true;
            }

            if (rowData.get("16") == null || StringUtils.isBlank(String.valueOf(rowData.get("16")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：保险结束时间为空" + "</p>");
                return true;
            }

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

        return true;
    }


    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);
    }

    public List<CrmCustomerNoSystemEntryInsurance> getRetList() {
        return retList;
    }

    public void setRetList(List<CrmCustomerNoSystemEntryInsurance> retList) {
        this.retList = retList;
    }

    public boolean isCheckFlag() {
        return checkFlag;
    }

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