package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.excel.bu;

import cn.estsh.i3plus.ext.mes.api.base.IExcelImportExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSkillsManagementBenchmark;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelImportResultExtModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesSkillsManagementBenchmarkRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/03 15:34
 * @Description: 技能管理基准导入
 **/
@Service
public class MesSkillsManagementBenchmarkExcelService implements IExcelImportExtService {

    @Autowired
    private MesSkillsManagementBenchmarkRepository skillsManagementBenchmarkRepository;

    /**
     * 技能管理基准导入
     *
     * @param workbook
     * @param organizeCode
     * @param userName
     * @return
     */
    @Override
    public ExcelImportResultExtModel insertDataByExcel(Workbook workbook, String organizeCode, String userName) {
        Sheet sheetAt = workbook.getSheetAt(0);
        //从0行开始读取
        int totalNumberOfRows = sheetAt.getLastRowNum() + 1;

        List<MesSkillsManagementBenchmark> list = new ArrayList<>();

        //成功数量
        Integer successRowNum = 0;
        //失败数量
        Integer failRowNum = 0;
        //错误的行号
        String errorRows = "";

        //添加完的数据
        ExcelImportResultExtModel excelImportResultModels = new ExcelImportResultExtModel();

        //从excel表的第2行数据开始导入，getFirstRowNum是从0行开始读取
        for (int i = (sheetAt.getFirstRowNum() + 1); i < totalNumberOfRows; i++) {
            Row row = sheetAt.getRow(i);
            //空行跳过
            if (null == row) {
                continue;
            }
            //获取总列数
            Short lastCellNum = row.getLastCellNum();
            if (lastCellNum > 0) {

                int rowNum = i + 1;    //当前行号

                //技能水平
                String skillLevel = null;
                try {
                    row.getCell(0).setCellType(CellType.STRING);
                    skillLevel = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //基准
                String standard = null;
                try {
                    row.getCell(1).setCellType(CellType.STRING);
                    standard = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //唯一性校验
                DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
                DdlPreparedPack.getStringEqualPack(skillLevel,"skillLevel",packBean);
                MesSkillsManagementBenchmark skillsManagementBenchmarkCheck = skillsManagementBenchmarkRepository.getByProperty(packBean);

                if (null != skillsManagementBenchmarkCheck) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("已存在相同技能管理基准")
                            .build();
                }

                //技能管理基准枚举值技能水平校验
                if (StringUtils.isEmpty(MesExtEnumUtil.SKILLS_MANAGEMENT_BENCHMARK_SKTLLS_LEVEL.valueOfDescription(skillLevel))){
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("请校验技能水平【%s】-【%s】是否正确",skillLevel,standard)
                            .build();
                }

                //入库
                MesSkillsManagementBenchmark skillsManagementBenchmark = new MesSkillsManagementBenchmark();
                skillsManagementBenchmark.setSkillLevel(skillLevel);
                skillsManagementBenchmark.setStandard(standard);
                skillsManagementBenchmark.setOrganizeCode(organizeCode);
                ConvertBean.serviceModelInitialize(skillsManagementBenchmark, userName);

                MesSkillsManagementBenchmark insert = skillsManagementBenchmarkRepository.insert(skillsManagementBenchmark);

                list.add(insert);
            }
        }
        if (list.size() != 0) {
            excelImportResultModels.setExcelList(list);
            excelImportResultModels.setResult(true);
        } else {
            excelImportResultModels.setResult(false);
        }
        return excelImportResultModels;

    }

}
