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.MesProcessLabelMarking;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelImportResultExtModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcessLabelMarkingRepository;
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 MesProcessLabelMarkingExcelService implements IExcelImportExtService {

    @Autowired
    private MesProcessLabelMarkingRepository processLabelMarkingRepository;

    /**
     * 工序标签标示导入
     *
     * @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<MesProcessLabelMarking> 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 processLabelCode = null;
                try {
                    row.getCell(0).setCellType(CellType.STRING);
                    processLabelCode = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //工序标签标示名称
                String processLabelName = null;
                try {
                    row.getCell(1).setCellType(CellType.STRING);
                    processLabelName = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //颜色
                String color = null;
                try {
                    row.getCell(2).setCellType(CellType.STRING);
                    color = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //位置
                String location = null;
                try {
                    row.getCell(3).setCellType(CellType.STRING);
                    location = row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //唯一性校验
                DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
                DdlPreparedPack.getStringEqualPack(processLabelCode,"processLabelCode",packBean);
                MesProcessLabelMarking processLabelMarkingCheck1 = processLabelMarkingRepository.getByProperty(packBean);

                DdlPackBean packBean1 = DdlPackBean.getDdlPackBean(organizeCode);
                DdlPreparedPack.getStringEqualPack(location,"location",packBean1);
                MesProcessLabelMarking processLabelMarkingCheck2 = processLabelMarkingRepository.getByProperty(packBean1);

                if (null != processLabelMarkingCheck1 || null != processLabelMarkingCheck2) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("已存在相同员工技能管理配置")
                            .build();
                }

                //工序标签标示枚举值颜色校验
                if (StringUtils.isEmpty(MesExtEnumUtil.PROCESS_LABEL_MARKING_COLOR.valueOfDescription(color))){
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("请校验【%s】-【%s】颜色【%s】是否正确",processLabelCode,processLabelName,color)
                            .build();
                }

                //工序标签标示枚举值位置校验
                if (StringUtils.isEmpty(MesExtEnumUtil.PROCESS_LABEL_MARKING_LOCATION.valueOfDescription(location))){
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("请校验【%s】-【%s】位置【%s】是否正确",processLabelCode,processLabelName,location)
                            .build();
                }

                //入库
                MesProcessLabelMarking processLabelMarking = new MesProcessLabelMarking();
                processLabelMarking.setProcessLabelCode(processLabelCode);
                processLabelMarking.setProcessLabelName(processLabelName);
                processLabelMarking.setColor(color);
                processLabelMarking.setLocation(location);
                processLabelMarking.setOrganizeCode(organizeCode);
                ConvertBean.serviceModelInitialize(processLabelMarking, userName);

                MesProcessLabelMarking insert = processLabelMarkingRepository.insert(processLabelMarking);

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

    }

}
