package com.jeeplus.modules.sg.managementcenter.materialplan.util;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.sg.managementcenter.materialplan.entity.InventoryTable;
import com.jeeplus.modules.sg.managementcenter.materialplan.entity.MaterialPlan;
import com.jeeplus.modules.sg.managementcenter.materialplan.entity.Storage;
import groovy.util.IFileNameFinder;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Row;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MaterialPlanInfo {
    public static final Integer MATER_ONE = 1;
    public static final Integer MATER_TWO = 2;
    public static final Integer MATER_THREE = 3;
    public static final String MATER_CODE = "物资唯一码";
    public static final String MATER_CODETWO = "批次编码";
    public static final String MATER_NUMBER = "序号";
    public static final String MATER_FLAG = "入库";
    public static final String MATER_MATERIAL="物料";
    public static final String MATER_YES = "是";
    public static final String MATER_NO = "否";
//    public static final String MATER_STORAGE = "是否入库";
//    public static final String MATER_COUNT = "库存数量";
//    public static final String MATER_DONGCOUNT = "冻结数量";
//    public static final String MATER_ITEMNAME = "供应商名称";
    /**
     * 获取表格数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public static List<MaterialPlan> getMaterPlanUtil(ImportUtil importUtil)  {
        String requireCount = "";//需求数量
        String inventoryQuantity ="";//库存数量
        String numberFrozen = "";//冻结数量
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialPlan> list = new ArrayList<MaterialPlan>();
        MaterialPlan materialPlan = null;
        String uniqueCode="";//费用估算
        for(int i=1; i<lastRow;i++){
            materialPlan = new MaterialPlan();
            Row row = importUtil.getRow(i);
            uniqueCode = (String)importUtil.getCellValue(row,0);
            if (StringUtils.isNotBlank(uniqueCode)){
                materialPlan.setUniqueCode(uniqueCode);
                materialPlan.setRequireCoding((String)importUtil.getCellValue(row,1));
                materialPlan.setDescription((String)importUtil.getCellValue(row,2));
                materialPlan.setExpansionCode((String)importUtil.getCellValue(row,3));
                materialPlan.setExpansionDescription((String)importUtil.getCellValue(row,4));
                materialPlan.setBigDescribe((String)importUtil.getCellValue(row,5));
                materialPlan.setModerateDescribe((String)importUtil.getCellValue(row,6));
                materialPlan.setSmallDescribe((String)importUtil.getCellValue(row,7));
                requireCount =(String)importUtil.getCellValue(row,8);
                if (null!=requireCount&&!requireCount.equals("")){
                    materialPlan.setRequireNumber(new Double((String)importUtil.getCellValue(row,8)));
                }
                materialPlan.setUtil((String)importUtil.getCellValue(row,9));
                materialPlan.setPlanToBatch((String)importUtil.getCellValue(row,10));
//                materialPlan.setSupplierName((String)importUtil.getCellValue(row,15));
                list.add(materialPlan);
            }
        }
        return list;
    }

    /**
     * 导入基础数据 -  库存 数据 list集合
     * @param importUtil
     * @return
     */
    public static List<InventoryTable> getInventoryTableUtil(ImportUtil importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<InventoryTable> list = new ArrayList<InventoryTable>();
        InventoryTable inventoryTable = null;
        String uniqueCode="";//费用估算
        for(int i=1; i<lastRow;i++){
            inventoryTable = new InventoryTable();
            Row row = importUtil.getRow(i);
            uniqueCode = (String)importUtil.getCellValue(row,3);
            if (StringUtils.isNotBlank(uniqueCode)){
                inventoryTable.setUniqueCode(uniqueCode);
                inventoryTable.setInventoryQuantity(new Double((String)importUtil.getCellValue(row,7)));
                inventoryTable.setNumberFrozen(new Double((String)importUtil.getCellValue(row,9)));
                inventoryTable.setSupplierName((String)importUtil.getCellValue(row,15));
                list.add(inventoryTable);
            }
        }
        return list;
    }

    /**
     * 判断excel是哪张表
     * @return
     */
    public static Integer getJudgeExcel(ImportUtil importUtil){
        Row row = importUtil.getRow(0);
        String firstName = (String) importUtil.getCellValue(row,0);
        String basicTwoName = (String) importUtil.getCellValue(row,2);
        String twoName = (String)importUtil.getCellValue(row,1);
        String fourName = (String)importUtil.getCellValue(row,4);
        if (firstName.contains(MATER_CODE)||firstName.contains(MATER_CODETWO)){
           if (basicTwoName.contains(MATER_MATERIAL)){
               return MATER_ONE;
           }
        }
        if (firstName.contains(MATER_NUMBER)&&fourName.contains(MATER_MATERIAL)){
            return MATER_TWO;
        }
        return MATER_THREE;
    }

    /**
     * 获取是否入库数据
     * @param importUtil
     * @return
     */
    public static List<InventoryTable> getUpdateMaterPlan(ImportUtil importUtil){
        List<InventoryTable> inventoryTableList = MaterialPlanInfo.getInventoryTableUtil(importUtil);
        for (InventoryTable inventoryTable:inventoryTableList){
            if (inventoryTable.getInventoryQuantity()+inventoryTable.getNumberFrozen()>0){
                inventoryTable.setFlagStorage("是");
            }else {
                inventoryTable.setFlagStorage("否");
            }
        }
        return inventoryTableList;
    }
    /**
     * 生成随机数
     * @return
     */
    public static String getStatus(){
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV<0){
            hashCodeV=-hashCodeV;
        }
        return  (machineId+String.format("%015d",hashCodeV));
    }
}
