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.pojo.bean.MesAssistOutAndEnter;
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.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Description 援入援出钉钉通知:本月所有未确认数据明细推送至担当和责任人
 * @author jiaqi.hou
 * @date 2023-08-21 10:52
 **/
@Slf4j
@Service
public class SxProdDataPushNotifyStrategyAssistNineService implements ISxProdDataPushNotifyStrategyFlagService {

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

        log.info("援入援出钉钉通知:本月所有未确认数据明细推送至担当和责任人 --- SxProdDataPushNotifyStrategyAssistNineService --- START --- DATA:{} --- THREAD:{}", JSONObject.toJSONString(data), Thread.currentThread().getName());

        SxPushDataNotifyParamAssistModel param = (SxPushDataNotifyParamAssistModel) pushParam;

        List<MesAssistOutAndEnter> dataDbList = (List<MesAssistOutAndEnter>) obj[0];
        if (CollectionUtils.isEmpty(dataDbList)) return null;

        //根据责任部门分组
        Map<String, List<MesAssistOutAndEnter>> ownerDeptMap = dataDbList.stream().filter(x -> !StringUtils.isEmpty(x.getOwnerDepartment()))
                .collect(Collectors.groupingBy(MesAssistOutAndEnter::getOwnerDepartment));

        List<PushDataNotifyModel> resultList = new ArrayList<>();
        PushDataNotifyModel resultData;
        for (Map.Entry<String, List<MesAssistOutAndEnter>> entry : ownerDeptMap.entrySet()) {
            resultData = new PushDataNotifyModel();
            resultData.userNames(getUserNames(entry)).title(String.format("%s月份%s有未确认的援入援出明细，请及时确认",param.getInfluenceDate(),entry.getKey()));
            resultList.add(resultData);
        }

        log.info("援入援出钉钉通知:本月所有未确认数据明细推送至担当和责任人 --- SxProdDataPushNotifyStrategyAssistNineService --- END --- DATA:{} --- THREAD:{}", JSONObject.toJSONString(data), Thread.currentThread().getName());

        return resultList;
    }

    private static String getUserNames(Map.Entry<String, List<MesAssistOutAndEnter>> entry) {
        Set<String> resultSet = new HashSet<>();
        resultSet.addAll(entry.getValue().stream().map(MesAssistOutAndEnter::getTakeOn).filter(x -> !StringUtils.isEmpty(x)).collect(Collectors.toSet()));
        resultSet.addAll(entry.getValue().stream().map(MesAssistOutAndEnter::getPersonResponsible).filter(x -> !StringUtils.isEmpty(x)).collect(Collectors.toSet()));
        return  String.join(",",resultSet);
    }
}
