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

import com.jeeplus.modules.sg.managementcenter.materialProvided.entity.MaterialProvided;
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<MaterialProvided> getMaterProUtil(ImportUtil importUtil)  {
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialProvided> list = new ArrayList<MaterialProvided>();
        MaterialProvided materialProvided = null;
        String materialCode="";//物料编码
        for(int i=1; i<lastRow;i++){
            materialProvided = new MaterialProvided();
            Row row = importUtil.getRow(i);
            materialCode = (String)importUtil.getCellValue(row,5);
            if (StringUtils.isNotBlank(materialCode)){
                materialProvided.setMaterialCode(materialCode);//物料编码
                //序号
                materialProvided.setSerialNumber(new Integer((String)importUtil.getCellValue(row,0)));
                //计划批次
                materialProvided.setPlanToBatch((String)importUtil.getCellValue(row,1));
                //计划批次名称
                materialProvided.setPlanToBatchName((String)importUtil.getCellValue(row,2));
                //分标信息
                materialProvided.setCaseInformation((String)importUtil.getCellValue(row,3));
//                物料编码
//                materialProvided.setMaterialCode((String)importUtil.getCellValue(row,4));
                //物料描述
                materialProvided.setMaterialDescription((String)importUtil.getCellValue(row,6));
                //扩展编码
                materialProvided.setExtendedCode((String)importUtil.getCellValue(row,7));
                //扩展编码描述
                materialProvided.setExtendedDescription((String)importUtil.getCellValue(row,8));
                //计量单位
                materialProvided.setMeasuringUnit((String)importUtil.getCellValue(row,9));
                //参考概算价格
                materialProvided.setReferencePrice(new Double((String)importUtil.getCellValue(row,10)));
                //物料加扩展
                materialProvided.setMaterialExtension(materialProvided.getMaterialCode()+materialProvided.getExtendedCode());
                list.add(materialProvided);
            }
        }
        return list;
    }
    public static List<MaterialProvided> getFlagMaterial(List<MaterialProvided> materProUtil){
        boolean flag = false;
        List<MaterialProvided> materialProvideds = new ArrayList<MaterialProvided>();
        for (MaterialProvided materialProvided:materialProvideds){
            flag = true;
            if (null!=materialProvideds && materialProvideds.size()>0){
                for (MaterialProvided material:materialProvideds){
                    if (material.getMaterialCode().equals(materialProvided.getMaterialCode())){
                        if (material.getMaterialDescription().equals(materialProvided.getExtendedDescription())){
                            flag = false;
                        }
                    }
                }
            }
            if (flag){
                materialProvideds.add(materialProvided);
            }
        }
        return materialProvideds;
    }

    /*
        验证
     */
    public static Boolean getJudgeProvided(ImportUtil importUtil){
        boolean flag = false;
        Row row = importUtil.getRow(0);
        String materialCode = (String) importUtil.getCellValue(row,5);//物理编码
        String materialDetail = (String) importUtil.getCellValue(row,6);//物料描述
        if (!materialCode.contains("编码")||!materialDetail.contains("描述")){
            return true;
        }
        return false;
    }
    /**
     * 生成随机数
     * @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));
    }
}
