package com.jeeplus.modules.sg.managementcenter.materialInventory.util;
import com.jeeplus.modules.sg.financial.erpcredit.util.DoubleUtil;
import com.jeeplus.modules.sg.managementcenter.materialInventory.entity.MaterialInventory;
import com.jeeplus.modules.sg.managementcenter.materialInventory.entity.MaterialRequisition;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;

import java.util.*;

public class ImportPro {
    /**
     * 获取库存物料数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public static List<MaterialInventory> getInventoryData(ImportUtil importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialInventory> list = new ArrayList<MaterialInventory>();
        MaterialInventory materialInventory = null;
        String materialCode="";//物料编码
        for(int i=1; i<lastRow;i++){
            materialInventory = new MaterialInventory();
            Row row = importUtil.getRow(i);
            materialCode = (String)importUtil.getCellValue(row,3);
            if (StringUtils.isNotBlank(materialCode)){
                materialInventory.setMaterialCode(materialCode);
                materialInventory.setMaterialDetail((String)importUtil.getCellValue(row,4));//物料描述
                materialInventory.setStockQuantity(new Double((String)importUtil.getCellValue(row,6)));//非限制库存数量
                materialInventory.setFrozenQuantity(new Double((String)importUtil.getCellValue(row,8)));//已冻结库存数量
                materialInventory.setAvailableStockQuantity(new Double((String)importUtil.getCellValue(row,6)));//可用库存，初始化使用非限制库存
                list.add(materialInventory);
            }
        }
        return list;
    }

    /**
     * 获取领料单表格数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public static List<MaterialRequisition> getRequisitionData(ImportUtil importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialRequisition> list = new ArrayList<MaterialRequisition>();
        MaterialRequisition materialRequisition = null;
        String materialCode="";//物料编码
//        for(int i=1; i<lastRow;i++){
        for(int i=4; i<lastRow;i++){
            materialRequisition = new MaterialRequisition();
            Row row = importUtil.getRow(i);
            materialCode = (String)importUtil.getCellValue(row,12);
            if (StringUtils.isNotBlank(materialCode)){
               if (!materialCode.contains("物料")){
                   materialRequisition.setMaterialCode(materialCode);//物料编码
                   materialRequisition.setRequireCount(new Double((String)importUtil.getCellValue(row,15)));//需求数量
                   materialRequisition.setOutCount(new Double((String)importUtil.getCellValue(row,16)));//已出库数量
                   list.add(materialRequisition);
               }
            }
        }
        return list;
    }

    /**
     * 将领料对象与库存对象合并，同时计算得到可用库存数量。这一步操作会丢弃无基础库存数据的领料数据
     * @param list
     * @param map
     * @return
     */
    public static List<MaterialInventory> getMaterial(List<MaterialInventory> list,Map<String,MaterialRequisition> map){
        if(list!=null&&map!=null){
            for(MaterialInventory inventory:list){
                MaterialRequisition requisition = map.get(inventory.getMaterialCode());
                if(requisition!=null){
                    inventory.setRequireCount(requisition.getRequireCount());
                    inventory.setOutCount(requisition.getOutCount());
                    inventory.setAvailableStockQuantity(DoubleUtil.add(inventory.getStockQuantity(),-requisition.getRequireCount(),
                            requisition.getOutCount()));//可用库存=非限制库存-（需求数量-出库数量）
                }
            }
        }
        return list;
    }

    /**
     * 库存物料相同物料编码汇总
     * @param materialInventories
     * @return
     */
    public static List<MaterialInventory> getInventorySet(List<MaterialInventory> materialInventories){
        Map<String,MaterialInventory> map = new HashMap<>();
        if(materialInventories!=null && materialInventories.size()>0){
            for(MaterialInventory material:materialInventories){
                String code = material.getMaterialCode();
                MaterialInventory mapMaterial = map.get(code);
                if(mapMaterial == null){
                    map.put(code,material);
                }else{
                    mapMaterial.setStockQuantity(DoubleUtil.add(mapMaterial.getStockQuantity(),material.getStockQuantity()));//非限制库存数量
                    mapMaterial.setFrozenQuantity(DoubleUtil.add(mapMaterial.getFrozenQuantity(),material.getFrozenQuantity()));//已冻结库存数量
                    mapMaterial.setAvailableStockQuantity(DoubleUtil.add(mapMaterial.getAvailableStockQuantity(),material.getAvailableStockQuantity()));//可用库存数量
                }
            }
        }
        List<MaterialInventory> list = new ArrayList<>(map.values());
        return list;
    }

    /**
     * 领料单数据 相同物料编码汇总
     * @param requisitions
     * @return
     */
    public static Map<String,MaterialRequisition> getRequisitionMap(List<MaterialRequisition> requisitions){
        Map<String,MaterialRequisition> map = new HashMap<>();
        if(requisitions!=null && requisitions.size()>0){
            for(MaterialRequisition requisition:requisitions){
                String code = requisition.getMaterialCode();
                MaterialRequisition mapRequisition = map.get(code);
                if(mapRequisition == null){
                    map.put(code,requisition);
                }else{
                    mapRequisition.setRequireCount(DoubleUtil.add(mapRequisition.getRequireCount(),requisition.getRequireCount()));//需求数量
                    mapRequisition.setOutCount(DoubleUtil.add(mapRequisition.getOutCount(),requisition.getOutCount()));//出库数量
                }
            }
        }
        return map;
    }

    /**
     * 验证导入的物料库存表
     * @param importUtil
     * @return
     */
    public static boolean getJudgeInventory(ImportUtil importUtil){
        boolean flag = false;
        Row row = importUtil.getRow(0);
        String materialCode = (String) importUtil.getCellValue(row,3);//物理编码
        String materialDetails = (String) importUtil.getCellValue(row,4);//物料描述
        if (!materialCode.contains("编码")||!materialDetails.contains("描述")){
            return true;
        }
        return false;
    }

    /**
     * 验证导入的领料表
     * @param importUtil
     * @return
     */
    public static boolean getJudgeRequisition(ImportUtil importUtil){
        boolean flag = false;
        Row row = importUtil.getRow(3);
        String requireCount = (String) importUtil.getCellValue(row,15);//需求数量
        String outCount = (String) importUtil.getCellValue(row,16);//已出库
        if (!requireCount.contains("需求")||!outCount.contains("出库")){
            return true;
        }
        return false;
    }
}