package com.sifang.modules.supply.warehouse.handler;

import com.google.common.collect.Maps;
import com.sifang.modules.order.utils.excelExt.handler.ExcelAbstractHandler;
import com.sifang.modules.supply.warehouse.vo.ImportFittingStockVo;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.SpringUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ExcelImportFittingStockHandler extends ExcelAbstractHandler {

    private List<ImportFittingStockVo> stockVos=new ArrayList<>();

    boolean checkFlag;

    @Override
    public Boolean handler(Map<String, Object> rowData, int rowIndex) {
        if((rowData.get("4") == null|| StringUtils.isBlank(String.valueOf(rowData.get("4")))) && (rowData.get("5") == null || StringUtils.isBlank(String.valueOf(rowData.get("5")))) && (rowData.get("6") == null || StringUtils.isBlank(String.valueOf(rowData.get("6"))))){
            return false;
        }
        ImportFittingStockVo vo=transToModel(rowData,rowIndex);
        stockVos.add(vo);
        return true;
    }

    private ImportFittingStockVo transToModel(Map<String,Object> rowData,int rowIndex){
        ImportFittingStockVo importFittingStockVo=new ImportFittingStockVo();
        if(rowIndex==2){
            importFittingStockVo.setInWarehouseName(String.valueOf(rowData.get("1")));
            importFittingStockVo.setCreateName(String.valueOf(rowData.get("2")));
            Date date= DateUtils.convertExcelDateToJavaDate(Double.valueOf(String.valueOf(rowData.get("3"))));
            importFittingStockVo.setCreateTime(date);
        }
        importFittingStockVo.setMaterialType(String.valueOf(rowData.get("4")));
        importFittingStockVo.setMaterialSpec(String.valueOf(rowData.get("5")));
        String materialSpec=importFittingStockVo.getMaterialSpec();
        if("组件".equals(importFittingStockVo.getMaterialType()) && materialSpec.endsWith("W")){
            importFittingStockVo.setMaterialSpec(materialSpec.substring(0,materialSpec.length()-1));
        }
        importFittingStockVo.setAmount(String.valueOf(rowData.get("6")));
        return importFittingStockVo;
    }

    //匹配模板
    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;
    }

    @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);
        }
        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() - 2)){
           checkFlag = true;
           if(rowIndex==2){
               if (rowData.get("1") == null || StringUtils.isBlank(String.valueOf(rowData.get("1")))) {
                   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(String.valueOf(rowData.get("3")))) {
                   appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：申请时间为空！" + "</p>");
               }else{
                   try {
                       Date date= DateUtils.convertExcelDateToJavaDate(Double.valueOf(String.valueOf(rowData.get("3"))));
                   }catch (Exception e){
                       appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：申请时间格式不正确！" + "</p>");
                   }
               }
           }
           if((rowData.get("4") == null|| StringUtils.isBlank(String.valueOf(rowData.get("4")))) && (rowData.get("5") == null || StringUtils.isBlank(String.valueOf(rowData.get("5")))) && (rowData.get("6") == null || StringUtils.isBlank(String.valueOf(rowData.get("6"))))){
               return false;
           }
            if (rowData.get("4") == null || StringUtils.isBlank(String.valueOf(rowData.get("4")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：物料类型为空！" + "</p>");
            }
            if (rowData.get("5") == null || StringUtils.isBlank(String.valueOf(rowData.get("5")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：物料规格为空！" + "</p>");
            }
            if (rowData.get("6") == null || StringUtils.isBlank(String.valueOf(rowData.get("6")))) {
                appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：备货数量为空！" + "</p>");
            }else{
                try {
                    new BigDecimal(String.valueOf(rowData.get("6")).trim());
                }catch (Exception e){
                    appendErrorDetail("<p>第" + (rowIndex + 1) + "行数据：备货数量格式不正确" + "</p>");
                }
            }
            stockVos.add(transToModel(rowData,rowIndex));
        }
        return true;
    }

    public List<ImportFittingStockVo> getStockVos() {
        return stockVos;
    }

    public void setStockVos(List<ImportFittingStockVo> stockVos) {
        this.stockVos = stockVos;
    }

    public boolean isCheckFlag() {
        return checkFlag;
    }

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