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.AddProjectCompany;
import com.sifang.modules.sunshine.SystemSetting.utils.ProJectCompanyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.PictureData;

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

public class ExcelImportProjectCompanyNumberHandler extends ExcelAbstractHandler {

    private List<AddProjectCompany> addCompanyList = new ArrayList<>();

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

    @Override
    public Boolean handler(Map<String, Object> rowData, int rowIndex) {
        AddProjectCompany addProjectCompany = transToModel(rowData);
        addCompanyList.add(addProjectCompany);
        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("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>");
            }else {
                if(!ProJectCompanyUtils.checkTelphone(rowData.get("6").toString())){
                    appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：法人授权电话格式不正确" + "</p>");
                }
            }
            if (rowData.get("8")== null || StringUtils.isBlank(rowData.get("8").toString())){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：公司代码为空" + "</p>");
            }
            if (rowData.get("9")== null || StringUtils.isBlank(rowData.get("9").toString())){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：所属资方为空" + "</p>");
            }
            if (rowData.get("10") == null || StringUtils.isBlank(rowData.get("10").toString())){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：省为空！" + "</p>");
            }
            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>");
            }
            if (rowData.get("13")== null || StringUtils.isBlank(rowData.get("13").toString())){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：详细地址为空" + "</p>");
            }
            if (rowData.get("14")== null || StringUtils.isBlank(rowData.get("14").toString())){
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：公司营业执照不能为空" + "</p>");
            }else {
                PictureData pictureData = null;
                try {
                    pictureData = (PictureData) rowData.get("14");
                    if(pictureData!=null){
                        String ext = pictureData.suggestFileExtension();
                        if(!"jpg|png|gif|bmp|jpeg".contains(ext)){
                            appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：包含不支持的格式" + "</p>");
                        }
                    }else {
                        appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：公司营业执照不能为空" + "</p>");
                    }
                }catch (Exception e){
                    //  转换异常说明当前单元格不是图片
                    appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：图片异常,请检查是否嵌入单元格!"+ "</p>");
                }

            }
            addCompanyList.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("法人授权电话");
        titles.add("联系电话");
        titles.add("公司代码");
        titles.add("所属资方");
        titles.add("省");
        titles.add("市");
        titles.add("区");
        titles.add("详细地址");
        titles.add("公司营业执照");
        return checkTemplate(titles, rowData);
    }

    private 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 PictureData formatCode(Object code,int type) {
        PictureData pictureData = null;
        if (code == null) {
            return null;
        }
        Object codeVal = code;
        if (codeVal instanceof Double) {
            codeVal = new DecimalFormat("#").format((Double) codeVal);
        } else {
            codeVal = code;
        }
        try {
            pictureData = (PictureData)codeVal;
        }catch (Exception e){
        }
        return pictureData;
    }

    private AddProjectCompany transToModel(Map<String, Object> rowData) {
        AddProjectCompany addProjectCompany = new AddProjectCompany();
        addProjectCompany.setName(formatCode(rowData.get("2")));
        addProjectCompany.setEnterpriseQualificationNumber(formatCode(rowData.get("3")));
        addProjectCompany.setChargePerson(formatCode(rowData.get("4")));
        addProjectCompany.setChargePersonIdCard(formatCode(rowData.get("5")));
        addProjectCompany.setPsnAccount(formatCode(rowData.get("6")));
        addProjectCompany.setTelephone(formatCode(rowData.get("7")));
        addProjectCompany.setCompanyCode(formatCode(rowData.get("8")));
        addProjectCompany.setPartnerName(formatCode(rowData.get("9")));
        addProjectCompany.setProvince(formatCode(rowData.get("10")));
        addProjectCompany.setCity(formatCode(rowData.get("11")));
        addProjectCompany.setArea(formatCode(rowData.get("12")));
        addProjectCompany.setAddress(formatCode(rowData.get("13")));
        addProjectCompany.setBusinessLicense(formatCode(rowData.get("14"),1));
        return addProjectCompany;
    }


    public List<AddProjectCompany> getAddCompanyList() {
        return addCompanyList;
    }

    public void setAddCompanyList(List<AddProjectCompany> addCompanyList) {
        this.addCompanyList = addCompanyList;
    }

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