package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.job;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxOutPutStatisticsTravelService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxWorkOrderTravelService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IWorkOrderLogExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.job.ISxDeleteTravelDataService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.repository.MesConfigRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION: Pcn删除履历数据记录JOB
 * @USER: xinwang.yi
 * @DATE: 2022-11-11 9:45
 */
@Slf4j
@Service
public class SxDeleteTravelDataService implements ISxDeleteTravelDataService {

    @Autowired
    private IWorkOrderLogExtService workOrderLogExtService;

    @Autowired
    private ISxOutPutStatisticsTravelService outPutStatisticsTravelService;

    @Autowired
    private ISxWorkOrderTravelService workOrderTravelService;

    @Autowired
    private MesConfigRepository configRepository;

    //MES_CONFIG 表中配置时间点，删除从该时间节点往前推移两个月的数据（仅删除已完成和已删除的工单关联日志）
    public static final String DELETE_TIME_POINT = "DELETE_TIME_POINT";

    @Override
    public void doDeleteTravelData(String organizeCode) {

        String workOrderLogNowTime = getDeleteTime(organizeCode);
        //删除小于指定时间的数据
        String workOrderLogModifyDatetime = TimeTool.timeCalc(TimeTool.stringParseToDate(workOrderLogNowTime, MesPcnExtConstWords.DATETIME_FORMAT), Calendar.MONTH, -2, MesPcnExtConstWords.DATETIME_FORMAT);

        String nowTime = TimeTool.getNowTime(MesPcnExtConstWords.DATETIME_FORMAT);

        String modifyDatetime = TimeTool.timeCalc(TimeTool.stringParseToDate(nowTime, MesPcnExtConstWords.DATETIME_FORMAT), Calendar.MONTH, -1, MesPcnExtConstWords.DATETIME_FORMAT);

        long startTime = System.currentTimeMillis();
        //删除工单日志
        workOrderLogExtService.deleteWorkOrderLogExt(organizeCode, Stream.of(MesPcnExtEnumUtil.WORK_ORDER_STATUS.CLOSE.getValue(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue()).collect(Collectors.toList()), workOrderLogModifyDatetime);
        long endTime = System.currentTimeMillis();
        log.info(" doDeleteTravelData --- deleteWorkOrderLogExt --- 工单日志删除" + " :ALL:耗时: " + (endTime - startTime) + "ms");

        startTime = System.currentTimeMillis();
        //删除产量统计中间表数据
        outPutStatisticsTravelService.deleteOutPutStatisticsTravel(organizeCode, modifyDatetime);
        endTime = System.currentTimeMillis();
        log.info(" doDeleteTravelData --- deleteOutPutStatisticsTravel --- 产量统计中间表数据删除" + " :ALL:耗时: " + (endTime - startTime) + "ms");

        //删除工单表中间表数据
        startTime = System.currentTimeMillis();
        workOrderTravelService.deleteWorkOrderTravel(organizeCode, modifyDatetime);
        endTime = System.currentTimeMillis();
        log.info(" doDeleteTravelData --- deleteWorkOrderTravel --- 工单中间表数据删除" + " :ALL:耗时: " + (endTime - startTime) + "ms");
    }

    private String getDeleteTime(String organizeCode){
        MesConfig timeConfig = configRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.CFG_CODE ,MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED},
                new Object[]{organizeCode, DELETE_TIME_POINT, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()});

        if((timeConfig != null) && (!StringUtils.isEmpty(timeConfig.getCfgValue()))){
            log.info("getDeleteTime --- MesConfig ---{}" ,timeConfig);
            if(!TimeTool.checkDateFormat(timeConfig.getCfgValue(), MesPcnExtConstWords.DATETIME_FORMAT)) {
                log.info("删除履历数据记录 --- getDeleteTime --- 时间点格式无效");
                return "2020-01-01 00:00:00";
            }
            return timeConfig.getCfgValue();
        }
        return TimeTool.getNowTime(MesPcnExtConstWords.DATETIME_FORMAT);
    }

}
