package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu3;

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToMonthlyStatisticsDispatchService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToStatisticsTravelService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToStatisticsTravel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
public class SxProdDataToMonthlyStatisticsWatchService implements ISxProdDataToMonthlyStatisticsDispatchService {

    private ISxProdDataToMonthlyStatisticsDispatchService dispatchService;

    private ISxProdDataToStatisticsTravelService prodDataToStatisticsTravelService;

    public SxProdDataToMonthlyStatisticsWatchService(ISxProdDataToMonthlyStatisticsDispatchService dispatchService) {
        this.dispatchService = dispatchService;
        this.prodDataToStatisticsTravelService = (ISxProdDataToStatisticsTravelService) SpringContextsUtil.getBean("sxProdDataToStatisticsTravelService");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = {ImppBusiException.class, Exception.class})
    public SxProdDataToAutoStatisticsModel exec(SxProdDataToAutoStatisticsModel data) {

        log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- WATCH START --- [{}] --- THREAD:[{}]", data.getOrganizeCode(), data.getToday(), data.getStrategyClass(), Thread.currentThread().getName());

        dispatchService.restock(data);

        if (!check(data)) return data;

        log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- QUERY START --- [{}] --- THREAD:[{}]", data.getOrganizeCode(), data.getToday(), data.getStrategyClass(), Thread.currentThread().getName());

        dispatchService.query(data);

        log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- QUERY END THEN EXEC --- [{}] --- source:[{}]target:[{}] --- THREAD:[{}]",
                data.getOrganizeCode(), data.getToday(), data.getStrategyClass(), data.getSourceCount(), data.getTargetCount(), Thread.currentThread().getName());

        dispatchService.exec(doTravel(data));

        log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- WATCH END --- [{}] --- 执行记录:{} --- THREAD:[{}]", data.getOrganizeCode(), data.getToday(), data.getStrategyClass(), JSONObject.toJSONString(data.getTravel()), Thread.currentThread().getName());

        return data;

    }

    private boolean check(SxProdDataToAutoStatisticsModel data) {
        if (null == prodDataToStatisticsTravelService.getProdDataToStatisticsTravel(
                data.getOrganizeCode(), data.getModuleCode(), data.getStatisticsType(), data.getStatisticsDate(), data.getDataKey())) return true;

        log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- WATCH END --- [{}] --- 已经存在统计履历 --- THREAD:[{}]", data.getOrganizeCode(), data.getToday(), data.getStrategyClass(), Thread.currentThread().getName());
        return false;
    }

    @Override
    public SxProdDataToAutoStatisticsModel doTravel(SxProdDataToAutoStatisticsModel data) {
        MesProdDataToStatisticsTravel travel = new MesProdDataToStatisticsTravel();
        BeanUtils.copyProperties(data, travel);
        return data.travel(travel);
    }

}





