package com.jeeplus.modules.sg.managementcenter.materialinfo.util;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.sg.audit.material.entity.Material;
import com.jeeplus.modules.sg.financial.erpcredit.util.DoubleUtil;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialInfo;
import org.apache.poi.ss.usermodel.Row;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MaterialInfoUtil {

    /**
     * 获取‘领料清单数据’
     * @param importExcel
     * @param projectName
     * @return
     */
    public static List<MaterialInfo> getData(ImportExcel importExcel, String projectName)  {
        int lastRow = importExcel.getLastDataRowNum();
        List<MaterialInfo> list = new ArrayList<>();
        MaterialInfo maintainData = null;
        for(int i=2; i<lastRow;i++){
            maintainData = new MaterialInfo();
            Row row = importExcel.getRow(i);
            String materialCode = (String) importExcel.getCellValue(row,1);//物料编码
            if(StringUtils.isNotBlank(materialCode)) {
                maintainData.setProjectName(projectName);//项目名称
                maintainData.setMaterialCode(materialCode);
                maintainData.setMaterialName((String) importExcel.getCellValue(row,2));//物料名称
                maintainData.setExtendDescription((String) importExcel.getCellValue(row,3));//扩展描述
                maintainData.setUnit((String) importExcel.getCellValue(row,4));//单位
                maintainData.setTotalCount(getDouble(importExcel,row,5));//总量
                maintainData.setWeightKg(getDouble(importExcel,row,6));//单重/kg
                maintainData.setWeightTon(getDouble(importExcel,row,7));//总重/吨
                maintainData.setSupplyType((String) importExcel.getCellValue(row,8));//甲乙供
                maintainData.setNote((String) importExcel.getCellValue(row,9));//备注
                list.add(maintainData);
            }
        }
        return list;
    }

    /**
     * 合并‘领料清单’数据
     * @param infos
     * @return
     */
    public static List<MaterialInfo> combineData(List<MaterialInfo> infos){
        Map<Integer,MaterialInfo> map = new HashMap<>();
        for(int i=0;i<infos.size();i++){
            MaterialInfo info = infos.get(i);
            MaterialInfo mapInfo = map.get(info.hashCode());
            if(mapInfo==null){
                map.put(info.hashCode(),info);
            }else{
                mapInfo.setTotalCount(DoubleUtil.add(mapInfo.getTotalCount(),info.getTotalCount()));
                mapInfo.setWeightTon(DoubleUtil.add(mapInfo.getWeightTon(),info.getWeightTon()));
            }
        }
        return new ArrayList<>(map.values());
    }


    /**
     * 获取‘领料清单数据’
     * @param importExcel
     * @param projectName
     * @return
     */
    public static List<MaterialInfo> getCbData(ImportExcel importExcel, String projectName)  {
        int lastRow = importExcel.getLastDataRowNum();
        List<MaterialInfo> list = new ArrayList<>();
        MaterialInfo maintainData = null;
        for(int i=6; i<lastRow;i++){
            maintainData = new MaterialInfo();
            Row row = importExcel.getRow(i);
            String materialCode = (String) importExcel.getCellValue(row,1);//物料编码
            if(StringUtils.isNotBlank(materialCode)) {
                maintainData.setProjectName(projectName);//项目名称
                maintainData.setMaterialCode(materialCode);
                maintainData.setMaterialName((String) importExcel.getCellValue(row,2));//物料名称
                maintainData.setExtendDescription((String) importExcel.getCellValue(row,3));//扩展描述
                maintainData.setUnit((String) importExcel.getCellValue(row,4));//单位
                maintainData.setTotalCount(getDouble(importExcel,row,5));//总量
                maintainData.setWeightKg(getDouble(importExcel,row,6));//单重/kg
                maintainData.setWeightTon(getDouble(importExcel,row,7));//总重/吨
                maintainData.setSupplyType((String) importExcel.getCellValue(row,8));//甲乙供
                maintainData.setNote((String) importExcel.getCellValue(row,9));//备注
                list.add(maintainData);
            }
        }
        return list;
    }

//    /**
//     * 合并数据
//     * @param materialInfos
//     * @return
//     */
//    public List<MaterialInfo> getNewData(List<MaterialInfo> materialInfos) {
//        double totalCount = 0.00;
//        double weightTon = 0.00;
//        List<MaterialInfo> list = new ArrayList<>();
//        for(int i=0;i<materialInfos.size();i++){
//            MaterialInfo materialInfo = materialInfos.get(i);
//            String projectName = materialInfo.getProjectName();
//            String code = materialInfo.getMaterialCode();
//            String name = materialInfo.getMaterialName();
//            String des = materialInfo.getExtendDescription();
//            totalCount = materialInfo.getTotalCount();
//            weightTon = materialInfo.getWeightTon();
//            String note = materialInfo.getNote();
//            if(projectName != null){
//                for(int j=i+1;j<materialInfos.size();j++){
//                    MaterialInfo materialInfo1 = materialInfos.get(j);
//                    String code1 = materialInfo1.getMaterialCode();
//                    String name1 = materialInfo1.getMaterialName();
//                    String des1 = materialInfo1.getExtendDescription();
//                    String note1 = materialInfo1.getNote();
//                    double totalCount1 = materialInfo1.getTotalCount();
//                    double weightTon1 = materialInfo1.getWeightTon();
//                    if(code.equals(code1) && des.equals(des1) && name.equals(name1) && note.equals(note1)){
//                        totalCount = totalCount + totalCount1;
//                        weightTon = weightTon + weightTon1;
//                        materialInfo1.setProjectName(null);
//                    }
//                }
//                materialInfo.setTotalCount(totalCount);
//                materialInfo.setWeightTon(weightTon);
//                list.add(materialInfo);
//            }
//
//        }
//        return list;
//    }

    private static double getDouble(ImportExcel importExcel,Row row,int col){
        double d = 0.00;
        Object obj = importExcel.getCellValue(row,col);
        if(obj!=null&&!obj.equals("")){
            d = Double.parseDouble(obj.toString());
        }
        return d;
    }

}
