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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxAssistOutAndEnterService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataPushNotifyStrategyFlagService;
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.model.bu3.SxPushDataNotifyParamModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.pojo.model.platform.PushDataNotifyModel;
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.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

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

    @Autowired
    private ISxAssistOutAndEnterService assistOutAndEnterService;

    @Autowired
    private SxProdDataPushNotifyStrategyFileCommonService strategyFileCommonService;

    @Override
    public List<PushDataNotifyModel> exec(PushDataNotifyModel data, SxPushDataNotifyParamModel pushParam, Object... obj) {

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

        SxPushDataNotifyParamAssistModel param = (SxPushDataNotifyParamAssistModel) pushParam;

        List<SxAssistOutAndEnterModel> dataDbList = getResultList(pushParam.getOrganizeCode(), (List<MesAssistOutAndEnter>) obj[0]);

        List<PushDataNotifyModel> resultList = new ArrayList<>();

        String title = String.format("援进援出[%s]明细信息", param.getInfluenceDate());

        Map<String, String> map = new HashMap<>();
        map.put(MesExtConstWords.CONTENT, String.format("%s共%s", title, CollectionUtils.isEmpty(dataDbList) ? "0条!" : String.format("%s条,附件如下,请查看!", dataDbList.size())));

        data.title(String.format("%s附件", title)).addListMail(map);

        strategyFileCommonService.exec(data, pushParam, dataDbList, MesExtConstWords.SX_ASSIST_PUSH_NOTIFY_REPORT, title);

        resultList.add(data);

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

        return resultList;
    }

    private List<SxAssistOutAndEnterModel> getResultList(String organizeCode, List<MesAssistOutAndEnter> dataDbList) {
        List<SxAssistOutAndEnterModel> resultList = new ArrayList<>();
        assistOutAndEnterService.packResultList(organizeCode, resultList, dataDbList);
        return CollectionUtils.isEmpty(resultList) ? resultList :
                resultList.stream().filter(o -> null != o).sorted(Comparator.comparing(SxAssistOutAndEnterModel::getOwnerDepartment)
                        .thenComparing(SxAssistOutAndEnterModel::getWorkCenterCode).thenComparing(SxAssistOutAndEnterModel::getAssistOutAndEnterDetail)).collect(Collectors.toList());
    }

}
