package cn.axzo.msg.center.message.service.impl;

import cn.axzo.foundation.result.ApiResult;
import cn.axzo.msg.center.common.enums.TableIsDeleteEnum;
import cn.axzo.msg.center.common.utils.BizAssertions;
import cn.axzo.msg.center.dal.MessageGroupNodeDao;
import cn.axzo.msg.center.domain.entity.MessageGroupNode;
import cn.axzo.msg.center.domain.persistence.BaseEntityExt;
import cn.axzo.msg.center.inside.notices.config.PendingMessageBizConfig;
import cn.axzo.msg.center.service.enums.AppTerminalTypeEnum;
import cn.axzo.orggateway.api.orgteamourelation.OrgTeamOuRelationApi;
import cn.axzo.orggateway.api.orgteamourelation.resp.OrgTeamOuRelationResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

/**
 * @author cold_blade
 * @version 1.0
 * @description
 * @date 2023/9/28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PendingMessageNewServiceImpl {

    private final OrgTeamOuRelationApi orgTeamOuRelationApi;
    private final PendingMessageBizConfig pendingMessageBizConfig;
    private final MessageGroupNodeDao messageGroupNodeDao;

    public Map<String, Object> getTerminalConfigInfo() {
        Map<AppTerminalTypeEnum, List<Long>> appType2NodeIds = pendingMessageBizConfig.getMsgGroupConfig();
        if (appType2NodeIds == null || appType2NodeIds.isEmpty()) {
            return Collections.emptyMap();
        }
        Set<Long> nodeIds = appType2NodeIds.values().stream()
                .flatMap(Collection::stream)
                .collect(toSet());
        Map<Long, MessageGroupNode> nodeId2Node = messageGroupNodeDao.lambdaQuery()
                .eq(MessageGroupNode::getIsDelete, TableIsDeleteEnum.NORMAL.value)
                .in(MessageGroupNode::getId, nodeIds)
                .list().stream()
                .collect(toMap(BaseEntityExt::getId, identity()));
        HashMap<String, Object> result = new HashMap<>();
        for (AppTerminalTypeEnum terminal : appType2NodeIds.keySet()) {
            List<Long> ids = appType2NodeIds.getOrDefault(terminal, Collections.emptyList());
            List<Map<String, Object>> nodeInfoList = new LinkedList<>();
            for (Long id : ids) {
                MessageGroupNode node = nodeId2Node.get(id);
                if (node == null) continue;
                HashMap<String, Object> info = new HashMap<>();
                info.put("id", node.getId());
                info.put("name", node.getName());
                info.put("code", node.getCode());
                nodeInfoList.add(info);
            }
            result.put(String.format("%s:%s", terminal.name(), terminal.getDesc()), nodeInfoList);
        }
        return result;
    }

    @NotEmpty
    public List<Long> determineOuIds(Long ouId) {
        List<Long> ouIds = new ArrayList<>();
        if (ouId == null)
            return ouIds;
        ouIds.add(ouId);
        // 2. 查询ouId下面所有的平台班组id当成ouId
        if (ouId != 0) {
            ApiResult<List<OrgTeamOuRelationResp>> resp =
                    orgTeamOuRelationApi.teamOuRelationList(ouId);
            List<OrgTeamOuRelationResp> relations =
                    BizAssertions.assertResponse(resp, "获取平台班组id失败");
            relations.stream()
                    .map(OrgTeamOuRelationResp::getTeamOuId)
                    .distinct()
                    .forEach(ouIds::add);
        }
        return ouIds;
    }

}
