package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base.jx;

import cn.estsh.i3plus.ext.mes.api.base.IShiftProdCenterService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IShiftProdCenterDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProd;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenter;
import cn.estsh.i3plus.ext.mes.pojo.model.ShiftProdCenterModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftProdCenterRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftProdRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class JxShiftProdCenterService implements IShiftProdCenterService {

    @Autowired
    private IShiftProdCenterDao shiftProdCenterDao;

    @Autowired
    private MesShiftExtRepository shiftExtRepository;

    @Autowired
    private MesShiftProdRepository shiftProdRepository;

    @Autowired
    private MesShiftProdCenterRepository shiftProdCenterRepository;

    @Override
    public ListPager<ShiftProdCenterModel> queryShiftProdCenterByPager(ShiftProdCenterModel shiftProdCenterModel, Pager pager) {
        pager = PagerHelper.getPager(pager, shiftProdCenterDao.queryShiftProdCenterByPagerCount(shiftProdCenterModel));
        List<ShiftProdCenterModel> shiftProdCenterModelList = shiftProdCenterDao.queryShiftProdCenterByPager(shiftProdCenterModel , pager);
        if (!CollectionUtils.isEmpty(shiftProdCenterModelList)) {
            for (ShiftProdCenterModel prodCenterModel : shiftProdCenterModelList) {
                if (null == prodCenterModel) {
                    continue;
                }
                prodCenterModel.setProdCategoryName(StringUtils.isEmpty(prodCenterModel.getProdCategory()) ?
                        null : MesExtEnumUtil.SHIFT_PROD_CATEGORY.valueOfDescription(prodCenterModel.getProdCategory()));
                prodCenterModel.setProdTypeName(StringUtils.isEmpty(prodCenterModel.getProdType()) ?
                        null : MesExtEnumUtil.SHIFT_PROD_TYPE.valueOfDescription(prodCenterModel.getProdType()));
                prodCenterModel.setIsProdTimeName(StringUtils.isEmpty(prodCenterModel.getIsProdTime()) ?
                        null : MesExtEnumUtil.SHIFT_PROD_IS_PROD_TIME.valueOfDescription(prodCenterModel.getIsProdTime()));
            }
        }

        return new ListPager(shiftProdCenterModelList, pager);
    }

    @Override
    public MesShiftProdCenter insertShiftProdCenter(ShiftProdCenterModel shiftProdCenterModel) {
        //获取当前选择的班次信息
        MesShiftExt shiftExt = getShiftByCenterAndShiftCode(shiftProdCenterModel);

        //校验[开始时间+结束时间] 在班次时间范围内
        checkTimeIsInShiftTime(shiftProdCenterModel, shiftExt);

        //[生产线代码+班次代码+作业类型(停机跟生产两个类型)] 校验 [开始时间+结束时间] 重叠
        checkTimeIsOverlap(shiftProdCenterModel, shiftExt);

        MesShiftProdCenter shiftProdCenter = new MesShiftProdCenter();
        BeanUtils.copyProperties(shiftProdCenterModel, shiftProdCenter, MesExtConstWords.ID);
        ConvertBean.serviceModelInitialize(shiftProdCenter, shiftProdCenterModel.getCreateUser());
        return shiftProdCenterRepository.insert(shiftProdCenter);
    }

    private MesShiftExt getShiftByCenterAndShiftCode(ShiftProdCenterModel shiftProdCenterModel) {
        MesShiftExt shiftExt = shiftExtRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID,
                        MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.SHIFT_CODE},
                new Object[]{shiftProdCenterModel.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        shiftProdCenterModel.getWorkCenterCode(), shiftProdCenterModel.getShiftCode()});
        if (null == shiftExt) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("生产线代码[%s]班次代码[%s]记录不存在",
                            shiftProdCenterModel.getShiftCode(), shiftProdCenterModel.getWorkCenterCode())
                    .build();
        }
        return shiftExt;
    }

    private void checkTimeIsOverlap(ShiftProdCenterModel shiftProdCenterModel, MesShiftExt shiftExt) {
        //获取[生产线代码+班次代码+作业类型(停机跟生产两个类型)] 维度的数据
        List<MesShiftProdCenter> shiftProdCenterList = getShiftProdCenterList(shiftProdCenterModel);

        //遍历并判断重叠的情况
        String msg = checkTimeIsOverlapMethod(shiftProdCenterList, shiftProdCenterModel, shiftExt);

        if (!StringUtils.isEmpty(msg)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail(msg)
                    .build();
        }
    }

    public String checkTimeIsOverlapMethod(List<MesShiftProdCenter> shiftProdCenterList, ShiftProdCenterModel shiftProdCenterModel, MesShiftExt shiftExt) {
        if (CollectionUtils.isEmpty(shiftProdCenterList)) {
            return null;
        }
        for (MesShiftProdCenter shiftProdCenter : shiftProdCenterList) {
            if (null == shiftProdCenter ||
                    (!StringUtils.isEmpty(shiftProdCenterModel.getId()) && shiftProdCenterModel.getId().compareTo(shiftProdCenter.getId()) == 0)) {
                continue;
            }

            //db实际开始跟结束时间(YYYY-MM-dd HH:mm:ss)
            ShiftProdCenterModel dbDateTimeModel = new ShiftProdCenterModel();
            dbDateTimeModel.setStartTime(shiftProdCenter.getStartTime());
            dbDateTimeModel.setEndTime(shiftProdCenter.getEndTime());
            checkTimeIsInShiftTime(dbDateTimeModel, shiftExt);

            //存在三种时间重叠的情况
            //if 当前 实际开始时间 大于等于db数据的 实际开始时间 并小于db数据的 实际结束时间
            //if 当前 实际结束时间 大于db数据的 实际开始时间 并小于等于db数据的 实际结束时间
            //if 当前 实际开始时间 小于等于db数据的 实际开始时间 并 当前 实际结束时间 大于等于db数据的 实际结束时间
            if ((TimeTool.stringParseToDate(shiftProdCenterModel.getStartDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getStartDateTime())) >= 0 &&
                    TimeTool.stringParseToDate(shiftProdCenterModel.getStartDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getEndDateTime())) < 0) ||
                    (TimeTool.stringParseToDate(shiftProdCenterModel.getEndDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getStartDateTime())) > 0 &&
                            TimeTool.stringParseToDate(shiftProdCenterModel.getEndDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getEndDateTime())) <= 0) ||
                    (TimeTool.stringParseToDate(shiftProdCenterModel.getStartDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getStartDateTime())) <= 0 &&
                            TimeTool.stringParseToDate(shiftProdCenterModel.getEndDateTime()).compareTo(TimeTool.stringParseToDate(dbDateTimeModel.getEndDateTime())) >= 0)) {
                return String.format("选择的时间[%s - %s]与已存在的数据存在时间重叠", shiftProdCenterModel.getStartTime(), shiftProdCenterModel.getEndTime());
            }
        }
        return null;
    }

    private List<MesShiftProdCenter> getShiftProdCenterList(ShiftProdCenterModel shiftProdCenterModel) {
        //获取作业类型的大类，停机或生产
        List<Integer> prodTypeList = getProdTypeList(shiftProdCenterModel.getProdType());
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(shiftProdCenterModel.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(shiftProdCenterModel.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(shiftProdCenterModel.getShiftCode(), MesExtConstWords.SHIFT_CODE, packBean);
        List<MesShiftProdCenter> shiftProdCenterList = shiftProdCenterRepository.findByHqlWhere(packBean);
        if (!CollectionUtils.isEmpty(shiftProdCenterList)) {
            List<String> prodCodeList = (shiftProdCenterList.stream().filter(o -> null != o)
                    .map(MesShiftProdCenter::getProdCode).collect(Collectors.toList())).stream().distinct().collect(Collectors.toList());
            List<MesShiftProd> shiftProdList = CollectionUtils.isEmpty(prodCodeList) ? null :
                    getShiftProdList(shiftProdCenterModel.getOrganizeCode(), prodCodeList, prodTypeList);
            if (!CollectionUtils.isEmpty(shiftProdList)) {
                prodCodeList = (shiftProdList.stream().filter(o -> null != o)
                        .map(MesShiftProd::getProdCode).collect(Collectors.toList())).stream().distinct().collect(Collectors.toList());
            }
            List<String> finalProdCodeList = prodCodeList;
            shiftProdCenterList = shiftProdCenterList.stream()
                    .filter(o -> (null != o && !CollectionUtils.isEmpty(finalProdCodeList) && finalProdCodeList.contains(o.getProdCode()))).collect(Collectors.toList());
        }
        return shiftProdCenterList;
    }

    private List<MesShiftProd> getShiftProdList(String organizeCode, List<String> prodCodeList, List<Integer> prodTypeList) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(prodCodeList, MesExtConstWords.PROD_CODE, ddlPackBean);
        DdlPreparedPack.getInPackList(prodTypeList, MesExtConstWords.PROD_TYPE, ddlPackBean);
        return shiftProdRepository.findByHqlWhere(ddlPackBean);
    }

    private List<Integer> getProdTypeList(Integer prodType) {
        List<Integer> prodTypeList = new ArrayList<>();
        if (MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_STOP_CENTER.getValue() == prodType ||
                MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_STOP_CENTER.getValue() == prodType) {
            prodTypeList.add(MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_STOP_CENTER.getValue());
            prodTypeList.add(MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_STOP_CENTER.getValue());
        } else {
            prodTypeList.add(MesExtEnumUtil.SHIFT_PROD_TYPE.NORMAL_PROD.getValue());
            prodTypeList.add(MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_PROD.getValue());
        }
        return prodTypeList;
    }

    private void checkTimeIsInShiftTime(ShiftProdCenterModel shiftProdCenterModel, MesShiftExt shiftExt) {
        String msg = checkTimeIsInShiftTimeComm(shiftProdCenterModel, shiftExt);
        if (!StringUtils.isEmpty(msg)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail(msg)
                    .build();
        }
    }

    public String checkTimeIsInShiftTimeComm(ShiftProdCenterModel shiftProdCenterModel, MesShiftExt shiftExt) {
        //当前时间(YYYY-MM-dd)
        String currentTimeYmd = TimeTool.getToday();
        //判断当前班次时间是否跨天,结束时间小于等于开始时间即跨天,否则当天
        Boolean isNextDayFlagShift = compareHms(shiftExt.getStartTime(), shiftExt.getEndTime());
        //判断当前选择的时间是否跨天,结束时间小于等于开始时间即跨天,否则当天
        Boolean isNextDayFlagCurr = compareHms(shiftProdCenterModel.getStartTime(), shiftProdCenterModel.getEndTime());

        //班次实际开始时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 班次开始时间]
        String shiftStartYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftExt.getStartTime();
        //班次实际结束时间(YYYY-MM-dd HH:mm:ss) [if 当天: 当前年月日 + 班次结束时间][else : 明天年月 + 班次结束时间]
        String shiftEndYmdHms = getDateTimeByFlag(isNextDayFlagShift, currentTimeYmd, shiftExt.getEndTime());
        String currStartYmdHms;
        String currEndYmdHms;

        //当前班次时间不跨天
        if (!isNextDayFlagShift) {
            if (isNextDayFlagCurr) { //当前选择的时间跨天
                return String.format("选择的时间[%s - %s]不在班次时间[%s - %s]的范围内",
                        shiftProdCenterModel.getStartTime(), shiftProdCenterModel.getEndTime(), shiftExt.getStartTime(), shiftExt.getEndTime());
            } else { //当前选择的时间不跨天
                //当前选择的实际开始时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 当前开始时间]
                currStartYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftProdCenterModel.getStartTime();
                //当前选择的实际结束时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 当前结束时间]
                currEndYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftProdCenterModel.getEndTime();
            }
        } else { //当前班次时间跨天
            if (isNextDayFlagCurr) { //当前选择的时间跨天
                //当前选择的实际开始时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 当前开始时间]
                currStartYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftProdCenterModel.getStartTime();
                //当前选择的实际结束时间(YYYY-MM-dd HH:mm:ss) [明天年月 + 当前结束时间]
                currEndYmdHms = getDateTimeByFlag(isNextDayFlagCurr, currentTimeYmd, shiftProdCenterModel.getEndTime());
            } else { //当前选择的时间不跨天
                //当前选择的开始时间小于当前班次的开始时间则当前选择的开始跟结束时间均跨天
                if (TimeTool.stringParseToDate(shiftProdCenterModel.getStartTime()).compareTo(TimeTool.stringParseToDate(shiftExt.getStartTime())) < 0) {
                    //当前选择的实际开始时间(YYYY-MM-dd HH:mm:ss) [明天年月 + 当前开始时间]
                    currStartYmdHms = getDateTimeByFlag(true, currentTimeYmd, shiftProdCenterModel.getStartTime());
                    //当前选择的实际结束时间(YYYY-MM-dd HH:mm:ss) [明天年月 + 当前结束时间]
                    currEndYmdHms = getDateTimeByFlag(true, currentTimeYmd, shiftProdCenterModel.getEndTime());
                } else { //当前选择的开始时间大于等于当前班次的开始时间则当前选择的开始跟结束时间均为当天
                    //当前选择的实际开始时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 当前开始时间]
                    currStartYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftProdCenterModel.getStartTime();
                    //当前选择的实际结束时间(YYYY-MM-dd HH:mm:ss) [当前年月日 + 当前结束时间]
                    currEndYmdHms = currentTimeYmd + MesExtConstWords.ONE_SPACE + shiftProdCenterModel.getEndTime();
                }
            }
        }

        //判断是否在班次时间范围内 [当前 实际开始时间 小于 班次的 实际开始时间 或者 当前 实际结束时间 大于 班次的 实际结束时间]
        if (TimeTool.stringParseToDate(currStartYmdHms).compareTo(TimeTool.stringParseToDate(shiftStartYmdHms)) < 0 ||
                TimeTool.stringParseToDate(currEndYmdHms).compareTo(TimeTool.stringParseToDate(shiftEndYmdHms)) > 0) {
            return String.format("选择的时间[%s - %s]不在班次时间[%s - %s]的范围内",
                    shiftProdCenterModel.getStartTime(), shiftProdCenterModel.getEndTime(), shiftExt.getStartTime(), shiftExt.getEndTime());
        }
        shiftProdCenterModel.setStartDateTime(currStartYmdHms);
        shiftProdCenterModel.setEndDateTime(currEndYmdHms);
        return null;
    }

    private Boolean compareHms(String startDate, String endDate) {
        Boolean flag;
        DateFormat sdf = new SimpleDateFormat(MesExtConstWords.TIME_FORMAT);
        try {
            if (sdf.parse(startDate).compareTo(sdf.parse(endDate)) >= 0) {
                flag = true;
            } else {
                flag = false;
            }
        } catch (Exception e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("时间比较出错")
                    .build();
        }
        return flag;
    }

    private String getDateTimeByFlag(Boolean isNextDayFlag, String currentTimeYmd, String time) {
        return !isNextDayFlag ? (currentTimeYmd + MesExtConstWords.ONE_SPACE + time) :
                (calcDay(currentTimeYmd, 1) + MesExtConstWords.ONE_SPACE + time);
    }

    private String calcDay(String day, int amount) {
        try {
            DateFormat sdf = new SimpleDateFormat(MesExtConstWords.DATE_FORMAT);
            return TimeTool.timeCalc(sdf.parse(day), Calendar.DATE, amount, MesExtConstWords.DATE_FORMAT);
        } catch (ParseException e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("时间计算失败")
                    .build();
        }
    }

    @Override
    public MesShiftProdCenter updateShiftProdCenter(ShiftProdCenterModel shiftProdCenterModel) {
        MesShiftProdCenter shiftProdCenterDb = getShiftProdCenterById(shiftProdCenterModel.getId());
        if (CommonEnumUtil.IS_VAILD.INVAILD.getValue() == shiftProdCenterDb.getIsValid()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录已被禁用,不能修改", shiftProdCenterModel.getId())
                    .build();
        }
        if (shiftProdCenterDb.getStartTime().equals(shiftProdCenterModel.getStartTime()) &&
                shiftProdCenterDb.getEndTime().equals(shiftProdCenterModel.getEndTime())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("ID[%s]记录未进行任何修改", shiftProdCenterModel.getId())
                    .build();
        }

        //获取当前选择的班次信息
        MesShiftExt shiftExt = getShiftByCenterAndShiftCode(shiftProdCenterModel);

        //校验[开始时间+结束时间] 在班次时间范围内
        checkTimeIsInShiftTime(shiftProdCenterModel, shiftExt);

        //[生产线代码+班次代码+作业类型(停机跟生产两个类型)] 校验 [开始时间+结束时间] 重叠
        checkTimeIsOverlap(shiftProdCenterModel, shiftExt);

        shiftProdCenterDb.setStartTime(shiftProdCenterModel.getStartTime());
        shiftProdCenterDb.setEndTime(shiftProdCenterModel.getEndTime());
        ConvertBean.serviceModelUpdate(shiftProdCenterDb, shiftProdCenterModel.getModifyUser());
        return shiftProdCenterRepository.save(shiftProdCenterDb);
    }

    private MesShiftProdCenter getShiftProdCenterById(Long id) {
        MesShiftProdCenter shiftProdCenterDb = shiftProdCenterRepository.getById(id);
        if (null == shiftProdCenterDb || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == shiftProdCenterDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", id)
                    .build();
        }
        return shiftProdCenterDb;
    }

    @Override
    public int updateShiftProdCenterValidById(Long id, Integer status, String organizeCode, String userName) {
        MesShiftProdCenter shiftProdCenterDb = getShiftProdCenterById(id);
        if (CommonEnumUtil.IS_VAILD.VAILD.getValue() == status) {
            //封装校验需要的入参
            ShiftProdCenterModel shiftProdCenterModel = packShiftProdCenterModel(id, organizeCode, shiftProdCenterDb);
            //获取当前选择的班次信息
            MesShiftExt shiftExt = getShiftByCenterAndShiftCode(shiftProdCenterModel);

            //校验[开始时间+结束时间] 在班次时间范围内
            checkTimeIsInShiftTime(shiftProdCenterModel, shiftExt);

            //[生产线代码+班次代码+作业类型(停机跟生产两个类型)] 校验 [开始时间+结束时间] 重叠
            checkTimeIsOverlap(shiftProdCenterModel, shiftExt);
        }

        return shiftProdCenterRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, userName, TimeTool.getNowTime(true)});
    }

    private ShiftProdCenterModel packShiftProdCenterModel(Long id, String organizeCode, MesShiftProdCenter shiftProdCenterDb) {
        ShiftProdCenterModel shiftProdCenterModel = new ShiftProdCenterModel();
        shiftProdCenterModel.setWorkCenterCode(shiftProdCenterDb.getWorkCenterCode());
        shiftProdCenterModel.setShiftCode(shiftProdCenterDb.getShiftCode());
        shiftProdCenterModel.setStartTime(shiftProdCenterDb.getStartTime());
        shiftProdCenterModel.setEndTime(shiftProdCenterDb.getEndTime());
        MesShiftProd shiftProd = shiftProdRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.PROD_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), shiftProdCenterDb.getProdCode()});
        if (null == shiftProd) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录关联的班次作业内容信息已不存在", id)
                    .build();
        }
        shiftProdCenterModel.setProdType(shiftProd.getProdType());
        return shiftProdCenterModel;
    }


    @Override
    public int deleteShiftProdCenterById(Long id, String organizeCode, String userName) {
        return shiftProdCenterRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_DELETED, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), userName, TimeTool.getNowTime(true)});
    }

}
