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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataPushNotifyStrategyFlagService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataPushNotifyStrategyService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAssistOutAndEnter;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxAssistOutAndEnterModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxPushDataNotifyParamAssistModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesAssistOutAndEnterRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.model.platform.PushDataNotifyModel;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSONObject;
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.List;

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

    @Autowired
    private MesAssistOutAndEnterRepository assistOutAndEnterRepository;

    @Override
    public List<PushDataNotifyModel> exec(PushDataNotifyModel data) {

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

        SxPushDataNotifyParamAssistModel pushParam = analysisParam(data);

        List<MesAssistOutAndEnter> dataList = assistOutAndEnterRepository.findByHqlWhere(MesExtHqlPack.getAssistOutAndEnter(
                new SxAssistOutAndEnterModel(pushParam.getInfluenceDateStart(), pushParam.getInfluenceDateEnd()).organizeCode(pushParam.getOrganizeCode()).ownerDepartment(pushParam.getOwnerDepartment()), false));

        ISxProdDataPushNotifyStrategyFlagService strategyFlagService = (ISxProdDataPushNotifyStrategyFlagService) SpringContextsUtil.getBean(MesExtEnumUtil.PROD_DATA_PUSH_NOTIFY_STRATEGY_FLAG.flagOfClazz(pushParam.getFlag()));

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

        return (List<PushDataNotifyModel>) strategyFlagService.exec(data, pushParam, dataList);

    }

    private SxPushDataNotifyParamAssistModel analysisParam(PushDataNotifyModel data) {

        SxPushDataNotifyParamAssistModel pushParam = JSONObject.parseObject(data.getPushParam(), SxPushDataNotifyParamAssistModel.class);

        if (StringUtils.isEmpty(data.getApiParam())) return restockParamDate(pushParam);

        SxPushDataNotifyParamAssistModel apiParam = JSONObject.parseObject(data.getApiParam(), SxPushDataNotifyParamAssistModel.class);

        return restockParamDate(pushParam.influenceDate(apiParam.getInfluenceDate()));

    }

    private SxPushDataNotifyParamAssistModel restockParamDate(SxPushDataNotifyParamAssistModel pushParam) {
        String curMonth = TimeTool.getToday().substring(0, 7);
        return pushParam.influenceDateStart(append(!StringUtils.isEmpty(pushParam.getInfluenceDate()) ? pushParam.getInfluenceDate() : getMonth(curMonth, -1)))
                .influenceDateEnd(append(!StringUtils.isEmpty(pushParam.getInfluenceDate()) ? getMonth(pushParam.getInfluenceDate(), 1) : curMonth))
                .influenceDate(!StringUtils.isEmpty(pushParam.getInfluenceDate()) ? pushParam.getInfluenceDate() : pushParam.getInfluenceDateStart().substring(0, 7));
    }

    private String append(String influenceDate) { return influenceDate + MesExtConstWords.DATE_DD_01; }

    private String getMonth(String month, int i) { return TimeTool.timeCalc(TimeTool.stringParseToDate(month, MesExtConstWords.DATE_FORMAT_YM), Calendar.MONTH, i, MesExtConstWords.DATE_FORMAT_YM); }

}
