package com.jeeplus.modules.sg.managementcenter.moduleacquisition.util;
import com.jeeplus.common.utils.excel.ImportExcel;

import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleBlock;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleListing;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;

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

public  class ModuleUtil {
    /**
     * 获取模块表清单
     * @param importUtil
     * @return List<MaterialProjectMapper> 导入表格数据
     */
    public static List<ModuleBlock> getModuleBlockUtil(ImportExcel importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<ModuleBlock> list = new ArrayList<ModuleBlock>();
        ModuleBlock moduleBlock = null;
        for(int i=1; i<=lastRow;i++){
            moduleBlock = new ModuleBlock();
            Row row = importUtil.getRow(i);
            String blockName = (String) importUtil.getCellValue(row,0);
            if(StringUtils.isNotBlank(blockName)) {
                moduleBlock.setMdBigClass(blockName);//获取大类名称
                moduleBlock.setMdTheClass((String)importUtil.getCellValue(row,1));//获取模块中类
                moduleBlock.setMdSmallClass((String)importUtil.getCellValue(row,2));//获取模块小类(模块名称)
                moduleBlock.setMdUnit((String)importUtil.getCellValue(row,3));//获取单位
                moduleBlock.setMdBigSort(new Integer((String)importUtil.getCellValue(row,4)));//获取大类排序
                moduleBlock.setMdTheSort(new Integer((String)importUtil.getCellValue(row,5)));//获取中类排序
                moduleBlock.setMdSmallSort(new Integer((String)importUtil.getCellValue(row,6)));//获取小类排序
                list.add(moduleBlock);
            }
        }
        return list;
    }

    /**
     * 获取领料清单
     * @param importUtil
     * @return
     */
    public static List<ModuleListing> getModuleListingUtil(ImportExcel importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<ModuleListing> list = new ArrayList<ModuleListing>();
        ModuleListing moduleListing = null;
        for(int i=1; i<=lastRow;i++){
            moduleListing = new ModuleListing();
            Row row = importUtil.getRow(i);
            String listingName = (String) importUtil.getCellValue(row,0);
            if(StringUtils.isNotBlank(listingName)) {
                moduleListing.setMdBigClass(listingName);//获取模块大类
                moduleListing.setMdTheClass((String)importUtil.getCellValue(row,1));//获取模块中类
                moduleListing.setMdName((String)importUtil.getCellValue(row,2));//获取 模块名称
                moduleListing.setBigClassification((String)importUtil.getCellValue(row,3));//大分类
                moduleListing.setTheClassification((String)importUtil.getCellValue(row,4));//中分类
                moduleListing.setSmallClassification((String)importUtil.getCellValue(row,5));//小分类
                moduleListing.setMaterialCode((String)importUtil.getCellValue(row,6));//物料编码
                moduleListing.setMaterialDescription((String)importUtil.getCellValue(row,7));//物料描述
                moduleListing.setExtensionDescription((String)importUtil.getCellValue(row,8));//扩展描述
                moduleListing.setMdUnits((String)importUtil.getCellValue(row,9));//单位
                moduleListing.setMdCount(getDouble(importUtil,row,10));//数量
                moduleListing.setMdSingleWeight(getDouble(importUtil,row,11));//单重
                moduleListing.setMdTotalWeight(getDouble(importUtil,row,12));//总重
                moduleListing.setMdTheParty((String)importUtil.getCellValue(row,13));//甲乙供
                moduleListing.setMdText((String)importUtil.getCellValue(row,14));//备注
                moduleListing.setMdTotalAmount(getDouble(importUtil,row,15));//总量
                list.add(moduleListing);
            }
        }
        return list;
    }
    /**
     *获取excel double类型的值
     */
    private static double getDouble(ImportExcel importExcel,Row row,int col){
        double d = 0.00;
        Object o = importExcel.getCellValue(row,col);
        if(!o.equals("")){
            if (!o.equals("/")){
                d = Double.parseDouble(o.toString());
            }
        }
        return d;
    }
    /**
     * 模块清单 获取 与之关联的模块表id
     * @param moduleListings 导入时得到的模块清单集合
     * @param moduleBlocks 导入时得到的模块表
     * @return
     */
    public static List<ModuleListing> getModuleListing(List<ModuleListing> moduleListings,List<ModuleBlock> moduleBlocks){
        String moduleId = "";
        for (ModuleListing moduleListing:moduleListings){
            moduleId = "";
            for (ModuleBlock moduleBlock:moduleBlocks){
                boolean bigFlag = StringUtils.equals(moduleListing.getMdBigClass(), moduleBlock.getMdBigClass());
                boolean theFlag = StringUtils.equals(moduleListing.getMdTheClass(), moduleBlock.getMdTheClass());
                boolean smallFlag = StringUtils.equals(moduleListing.getMdName(), moduleBlock.getMdSmallClass());
                if (bigFlag&&theFlag&&smallFlag){
                    moduleId = moduleBlock.getId();
                    break;
                }
            }
            moduleListing.setBlockId(moduleId);
        }
        return moduleListings;
    }
}
