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-16 13:48
 **/
@Slf4j
@Service
public class SxProdDataPushNotifyStrategyAssistSevenService implements ISxProdDataPushNotifyStrategyFlagService {

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

        log.info("钉钉通知:上月所有数据明细推送至责任人和部长 --- SxProdDataPushNotifyStrategyAssistSevenService --- 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;
        LinkedHashMap<String, String> dataMap;
        for (Map.Entry<String, List<MesAssistOutAndEnter>> entry : ownerDeptMap.entrySet()) {
            resultData = new PushDataNotifyModel();
            dataMap = new LinkedHashMap<>();
            dataMap.put("月份", Integer.valueOf(param.getInfluenceDateStart().substring(5, 7)) + "月");
            dataMap.put("工时", statisticsInfluenceTime(entry.getValue()) + "分");
            dataMap.put("责任部门", entry.getKey());
            dataMap.put("合计", statisticsAmount(entry.getValue()) + "元");
            dataMap.put("单次", CollectionUtils.isEmpty(entry.getValue()) ? MesExtConstWords.ZERO_STR : String.valueOf(entry.getValue().size()));

            resultData.addListDing(dataMap).userNames(getUserNames(entry)).title("援入援出信息统计");
            resultList.add(resultData);
        }

        log.info("钉钉通知:上月所有数据明细推送至责任人和部长 --- SxProdDataPushNotifyStrategyAssistSevenService --- 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::getPersonResponsible).filter(x -> !StringUtils.isEmpty(x)).collect(Collectors.toSet()));
        resultSet.addAll(entry.getValue().stream().map(MesAssistOutAndEnter::getResponsibleMinister).filter(x -> !StringUtils.isEmpty(x)).collect(Collectors.toSet()));
        return  String.join(",",resultSet);
    }

    private Double statisticsInfluenceTime(List<MesAssistOutAndEnter> dataDbList) {
        return CollectionUtils.isEmpty(dataDbList) ? new Double(0) :
                dataDbList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getInfluenceTime()))).mapToDouble(MesAssistOutAndEnter::getInfluenceTime).sum();
    }

    private Double statisticsAmount(List<MesAssistOutAndEnter> dataDbList) {
        return CollectionUtils.isEmpty(dataDbList) ? new Double(0) :
                dataDbList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getAmount()))).mapToDouble(MesAssistOutAndEnter::getAmount).sum();
    }
}
