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.ISxProdDataToMonthlyStatisticsService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class SxProdDataToMonthlyStatisticsService implements ISxProdDataToMonthlyStatisticsService {


    @Autowired
    private final Map<String, ISxProdDataToMonthlyStatisticsDispatchService> dispatchServiceMap = new ConcurrentHashMap<>();

    public SxProdDataToMonthlyStatisticsService(Map<String, ISxProdDataToMonthlyStatisticsDispatchService> dispatchServiceMap) {
        this.dispatchServiceMap.clear();
        dispatchServiceMap.forEach(this.dispatchServiceMap::put);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = {ImppBusiException.class, Exception.class})
    public void exec(SxProdDataToAutoStatisticsModel model) {

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

        calcTime(model);

        for (String key : dispatchServiceMap.keySet()) {
            if (StringUtils.isEmpty(key)) continue;
            if (StringUtils.isEmpty(dispatchServiceMap.get(key).name()) || StringUtils.isEmpty(dispatchServiceMap.get(key).flag())) continue;
            if (!StringUtils.isEmpty(model.getStrategyClass()) && !dispatchServiceMap.get(key).name().equals(model.getStrategyClass())) continue;
            if (!dispatchServiceMap.get(key).flag().equals(model.getFlag())) continue;

            try {

                new SxProdDataToMonthlyStatisticsWatchService(
                        dispatchServiceMap.get(key)
                ).exec(new SxProdDataToAutoStatisticsModel().copy(model).strategyClass(key));

            } catch (Exception e) {
                log.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- DISPATCH EXCEPTION:[{}] --- THREAD:[{}]", model.getOrganizeCode(), model.getToday(), e.getMessage(), Thread.currentThread().getName());
            }
        }

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

    }

    private void calcTime(SxProdDataToAutoStatisticsModel model) {
        model.date(TimeTool.timeCalc(TimeTool.stringParseToDate(model.getToday(), MesExtConstWords.DATE_FORMAT_YM), Calendar.MONTH, -1, MesExtConstWords.DATE_FORMAT_YM))
                .startTime(model.getDate() + MesExtConstWords.DATE_DD_01)
                .endTime(model.getToday().substring(0, 7) + MesExtConstWords.DATE_DD_01);
    }

}
