package com.jeeplus.modules.sg.managementcenter.materialInventory.util;
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.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImportPro {
    /**
     * 获取库存表格数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public static List<MaterialInventory> getMaterialInventoryUtil(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)));//已冻结库存数量
                list.add(materialInventory);
            }
        }
        return list;
    }

    /**
     * 获取领料单表格数据
     * @param importUtil
     * @return List<ErpAccount> 导入表格数据
     */
    public static List<MaterialRequisition> getMaterialRequisitionUtil(ImportUtil importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialRequisition> list = new ArrayList<MaterialRequisition>();
        MaterialRequisition materialRequisition = null;
        String materialCode="";//物料编码
        for(int i=1; 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;
    }
    /*
        得到可使用库存数量
     */
    public static List<MaterialInventory> getMaterial(List<MaterialInventory> materialInventories,List<MaterialRequisition> materialRequisitions){
        Double availableStockQuantity = 0.0;
        Double requireCount =0.0;
        Double outCount = 0.0;
        for (MaterialInventory materialInventory:materialInventories){
            for (MaterialRequisition materialRequisition:materialRequisitions){
                if (materialInventory.getMaterialCode().equals(materialRequisition.getMaterialCode())){
                    availableStockQuantity = materialRequisition.getRequireCount()-materialRequisition.getOutCount();
                    requireCount = materialRequisition.getRequireCount();
                    outCount = materialRequisition.getOutCount();
                    break;
                }
            }
            materialInventory.setRequireCount(requireCount);
            materialInventory.setOutCount(outCount);
            availableStockQuantity = materialInventory.getStockQuantity() - availableStockQuantity;
            materialInventory.setAvailableStockQuantity(availableStockQuantity);
        }
        return materialInventories;
    }

    /*
     * 去重库存表
     */
    public static List<MaterialInventory> getSetMaterialInvenntory(List<MaterialInventory> materialInventories){
        List<MaterialInventory> materialInventoryList = new ArrayList<MaterialInventory>();
        boolean flag = true;
        MaterialInventory material = null;
        double count = 0.0;
        for (MaterialInventory materialInventory:materialInventories){
            flag = true;
            for (MaterialInventory materialInventory1:materialInventoryList){
                if (materialInventory1.getMaterialCode().equals(materialInventory.getMaterialCode())){
                    count = materialInventory.getStockQuantity()+materialInventory1.getStockQuantity();
                    materialInventory1.setStockQuantity(count);//非限制库存数量
                    count = materialInventory.getFrozenQuantity()+materialInventory1.getFrozenQuantity();
                    materialInventory1.setFrozenQuantity(count);//已冻结库存数量
                    flag = false;
                    break;
                }
            }
            if (flag){
                material = new MaterialInventory();
                material.setMaterialCode(materialInventory.getMaterialCode());//物料编码
                material.setMaterialDetail(materialInventory.getMaterialDetail());//物料描述
                material.setStockQuantity(materialInventory.getStockQuantity());//非限制库存数量
                material.setFrozenQuantity(materialInventory.getFrozenQuantity());//已冻结库存数量
                materialInventoryList.add(material);
            }
        }
        for (MaterialInventory materialInventory:materialInventoryList){
            System.out.println(materialInventory);
        }
        return materialInventoryList;
    }

    /*
     * 去重 附件5
     */
    public static List<MaterialRequisition> getSetMaterialRequisition(List<MaterialRequisition> requisitions){
        List<MaterialRequisition> requisitionList = new ArrayList<MaterialRequisition>();
        boolean flag = true;
        MaterialRequisition requisition = null;
        double count = 0.0;
        for (MaterialRequisition materialRequisition:requisitions){
            flag = true;
            for (MaterialRequisition materialRequisition1:requisitionList){
                if (materialRequisition.getMaterialCode().equals(materialRequisition1.getMaterialCode())){
                    count = materialRequisition.getRequireCount()+materialRequisition1.getRequireCount();
                    materialRequisition1.setRequireCount(count);
                    count = materialRequisition.getOutCount()+materialRequisition1.getOutCount();
                    materialRequisition1.setOutCount(count);
                    flag = false;
                }
            }
            if (flag){
                requisition = new MaterialRequisition();
                requisition.setMaterialCode(materialRequisition.getMaterialCode());
                requisition.setRequireCount(materialRequisition.getRequireCount());
                requisition.setOutCount(materialRequisition.getOutCount());
                requisitionList.add(requisition);
            }
        }
        return requisitionList;
    }
    //验证库存表
    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;
    }
    //验证库存表
    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;
    }
}