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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataPushNotifyStrategyService;
import cn.estsh.i3plus.pojo.model.platform.PushDataNotifyModel;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import java.util.List;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
public class SxProdDataPushNotifyWatchService implements ISxProdDataPushNotifyStrategyService {

    private ISxProdDataPushNotifyStrategyService strategyService;

    public SxProdDataPushNotifyWatchService(ISxProdDataPushNotifyStrategyService strategyService) {
        this.strategyService = strategyService;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = {ImppBusiException.class, Exception.class})
    public List<PushDataNotifyModel> exec(PushDataNotifyModel data) {

        log.info("生产数据推送通知数据处理 --- WATCH START --- DATA:{} --- THREAD:{}", JSONObject.toJSONString(data), Thread.currentThread().getName());

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        List<PushDataNotifyModel> resultList = null;

        try {

            if (null != strategyService) resultList = strategyService.exec(data);

        } catch (Exception e) {

            log.info("生产数据推送通知数据处理 --- WATCH EXCEPTION --- {} --- THREAD:{}", e.getMessage(), Thread.currentThread().getName());

        } finally {

            stopWatch.stop();

            log.info("生产数据推送通知数据处理 --- WATCH END --- 耗时:{}ms --- DATA:{} --- THREAD:{}", stopWatch.getTotalTimeMillis(), JSONObject.toJSONString(resultList), Thread.currentThread().getName());

        }

        return resultList;

    }
}
