package com.jeeplus.modules.sg.managementcenter.materialproject.util;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;

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 MaterialProUtil {
    /**
     * 获取表格数据
     * @param importUtil
     * @return List<MaterialProjectMapper> 导入表格数据
     */
    public static List<MaterialProject> getProjectUtil(ImportUtil importUtil) throws Exception {
        int lastRow = importUtil.getLastDataRowNum();
        List<MaterialProject> list = new ArrayList<MaterialProject>();
        MaterialProject materialProject = null;
        for(int i=1; i<=lastRow;i++){
            materialProject = new MaterialProject();
            Row row = importUtil.getRow(i);
            String reserveProjectName = (String) importUtil.getCellValue(row,0);
            try {
                if(StringUtils.isNotBlank(reserveProjectName)) {
                    materialProject.setReserveProjectName(reserveProjectName);
                    materialProject.setReserveCode((String)importUtil.getCellValue(row,1));//项目名称
                    materialProject.setProjectType((String)importUtil.getCellValue(row,2));
                    materialProject.setBatchPlan((String)importUtil.getCellValue(row,3));
                    materialProject.setIssuePlan((String)importUtil.getCellValue(row,4));
                    materialProject.setMonthPlan((String)importUtil.getCellValue(row,5));
                    materialProject.setHasPlan((String)importUtil.getCellValue(row,6));
                    materialProject.setHalfPlan((String)importUtil.getCellValue(row,7));
                    materialProject.setProjectId((String)importUtil.getCellValue(row,8));
                    materialProject.setProjectName((String)importUtil.getCellValue(row,9));
                    materialProject.setAttributionYear((String)importUtil.getCellValue(row,10));
                    list.add(materialProject);
                }
            } catch (Exception e) {
                throw new Exception("第 "+i+" 行数据读取错误！");
            }
        }
        return list;
    }
    //验证表格数据是否正确
    public static Boolean getMaterialProjectFlag(ImportUtil importUtil){
        Boolean flag = false;
        Row row = importUtil.getRow(0);
        String index =  (String)importUtil.getCellValue(row,0);
        String two = (String)importUtil.getCellValue(row,1);
        if (!index.contains("储备")){
            flag = true;
        }
        if (!two.contains("项目")){
            flag = true;
        }
        return flag;
    }
    /**
     * 生成随机数
     * @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));
    }
}
