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.MesTeamManagementIntroduce;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesTeamManagementIntroduceExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelImportResultExtModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesTeamManagementIntroduceExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.ConvertBeanExt;
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 java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/03 15:34
 * @Description: 团队管理介绍导入
 **/
@Service
public class MesTeamManagementIntroduceExcelService implements IExcelImportExtService {

    @Autowired
    private MesTeamManagementIntroduceExtRepository teamManagementIntroduceRepository;

    @Autowired
    private MesWorkCenterExtRepository workCenterExtRepository;

    /**
     * 团队管理介绍导入
     *
     * @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<MesTeamManagementIntroduce> list = new ArrayList<>();

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

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

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

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

                //产线代码
                String workCenterCode = null;
                try {
                    row.getCell(0).setCellType(CellType.STRING);
                    workCenterCode = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //班组长照片
                String shiftUrl = null;
                try {
                    row.getCell(1).setCellType(CellType.STRING);
                    shiftUrl = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //团队照片
                String teamUrl = null;
                try {
                    row.getCell(2).setCellType(CellType.STRING);
                    teamUrl = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //团队口号
                String teamSlogan = null;
                try {
                    row.getCell(3).setCellType(CellType.STRING);
                    teamSlogan = row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //团队年度目标
                String teamYearTarget = null;
                try {
                    row.getCell(4).setCellType(CellType.STRING);
                    teamYearTarget = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue().trim();
                } catch (Exception e) {
                }

                //校验产线
                DdlPackBean packBeanCenter = DdlPackBean.getDdlPackBean(organizeCode);
                DdlPreparedPack.getStringEqualPack(workCenterCode,"workCenterCode",packBeanCenter);
                MesWorkCenterExt center = workCenterExtRepository.getByProperty(packBeanCenter);

                if (null == center) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("不存在的产线【%s】请检查配置",workCenterCode)
                            .build();
                }

                //唯一性校验
                DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
                DdlPreparedPack.getStringEqualPack(workCenterCode,"workCenterCode",packBean);
                MesTeamManagementIntroduce teamManagementIntroduceCheck = teamManagementIntroduceRepository.getByProperty(packBean);

                if (null != teamManagementIntroduceCheck) {
                    throw ImppExceptionBuilder.newInstance()
                            .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                            .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                            .setErrorDetail("已存在相同团队管理介绍")
                            .build();
                }

                //入库
                MesTeamManagementIntroduceExt teamManagementIntroduce1 = new MesTeamManagementIntroduceExt();
                teamManagementIntroduce1.setWorkCenterCode(workCenterCode);
                teamManagementIntroduce1.setTeamSlogan(teamSlogan);
                teamManagementIntroduce1.setTeamYearTarget(teamYearTarget);
                teamManagementIntroduce1.setUrlShift(shiftUrl);
                teamManagementIntroduce1.setUrlTeam(teamUrl);
                //teamManagementIntroduce1.setPhotoType(MesExtEnumUtil.TEAM_MANAGEMENT_INTRODUCE_PHOTO_TYPE.SHIFT_PHOTO.getValue());
                teamManagementIntroduce1.setOrganizeCode(organizeCode);
                ConvertBean.serviceModelInitialize(teamManagementIntroduce1, userName);
                new ConvertBeanExt(teamManagementIntroduce1).convertBean(teamManagementIntroduce1);

                MesTeamManagementIntroduce insert1 = teamManagementIntroduceRepository.insert(teamManagementIntroduce1);
/*

                MesTeamManagementIntroduceExt teamManagementIntroduce2 = new MesTeamManagementIntroduceExt();
                teamManagementIntroduce2.setWorkCenterCode(workCenterCode);
                teamManagementIntroduce2.setTeamSlogan(teamSlogan);
                teamManagementIntroduce2.setTeamYearTarget(teamYearTarget);
                teamManagementIntroduce2.setUrl(teamUrl);
                teamManagementIntroduce2.setPhotoType(MesExtEnumUtil.TEAM_MANAGEMENT_INTRODUCE_PHOTO_TYPE.TEAM_PHOTO.getValue());
                teamManagementIntroduce2.setOrganizeCode(organizeCode);
                ConvertBean.serviceModelInitialize(teamManagementIntroduce2, userName);

                MesTeamManagementIntroduce insert2 = teamManagementIntroduceRepository.insert(teamManagementIntroduce2);
*/

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

    }

}
