package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.collect;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyDbCollectDataRecordService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyDbCollectDataStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesThirdPartyDbCollectDataRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxThirdPartyDbCollectDataModel;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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.util.StopWatch;
import org.springframework.util.StringUtils;

@Slf4j
public class SxThirdPartyDbCollectDataProxyWatchService implements ISxThirdPartyDbCollectDataStrategyService {

    private ISxThirdPartyDbCollectDataStrategyService proxyObject;

    private ISxThirdPartyDbCollectDataRecordService thirdPartyDbCollectDataRecordService;

    public SxThirdPartyDbCollectDataProxyWatchService(ISxThirdPartyDbCollectDataStrategyService proxyObject) {
        this.proxyObject = proxyObject;
        thirdPartyDbCollectDataRecordService = (ISxThirdPartyDbCollectDataRecordService) SpringContextsUtil.getBean("gzThirdPartyDbCollectDataRecordService");
    }

    @Override
    public void exec(SxThirdPartyDbCollectDataModel collect) {

        log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC WATCH --- START ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode());

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        try {

            proxyObject.exec(collect);

        } catch (ImppBusiException e) {

            log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC WATCH --- ImppBusiException:%s ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode(), e.getMessage());

        } catch (Exception e) {

            log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC WATCH --- Exception:%s ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode(), JSONObject.toJSONString(e));

        }

        saveCurExecResult(collect);

        stopWatch.stop();

        log.info("工厂{}第三方DB数据采集 --- CFG_CODE: {} EXEC --- CFG_KEY: {} EXEC --- EXEC WATCH --- END --- 耗时:{} ---", collect.getOrganizeCode(), collect.getCfgCode(), collect.getObjectCode(), stopWatch.getTotalTimeMillis());
    }

    private void saveCurExecResult(SxThirdPartyDbCollectDataModel collect) {
        if (collect.getExecResult() || !StringUtils.isEmpty(collect.getError()))
            thirdPartyDbCollectDataRecordService.exec(collect.getOrganizeCode(), collect.getUserInfo(), packRecord(collect));
    }

    private MesThirdPartyDbCollectDataRecord packRecord(SxThirdPartyDbCollectDataModel collect) {
        return new MesThirdPartyDbCollectDataRecord(collect.getCfgCode(), collect.getObjectCode(), collect.getCfgValue(),
                collect.getExecResult() ? CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() : CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), collect.getExecSize(), collect.getError());
    }
}
