package com.seer.teach.mp.api;

import com.seer.teach.mp.api.dto.MpSendMessageDTO;
import com.seer.teach.mp.entity.MpMessageGroupUserEntity;
import com.seer.teach.mp.entity.MpTemplateMessageConfigEntity;
import com.seer.teach.mp.service.IMpTemplateMessageConfigService;
import com.seer.teach.mp.service.IMpTemplateMessageService;
import com.seer.teach.mp.service.IMqMessageGroupUserService;
import com.seer.teach.mp.service.dto.MpTemplateMessageDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RequiredArgsConstructor
@RestController
public class MpMessageServiceApiImpl implements MpMessageServiceApi {

    private final IMpTemplateMessageConfigService mpTemplateMessageConfigService;

    private final IMqMessageGroupUserService mqMessageGroupUserService;

    private final IMpTemplateMessageService mpTemplateMessageService;

    @Override
    public List<Integer> getGroupUserIdForMessageByBusinessSceneCode(String businessSceneCode) {
        MpTemplateMessageConfigEntity messageConfigEntity = mpTemplateMessageConfigService.getGroupIdsByBusinessSceneCode(businessSceneCode);
        if ( Objects.isNull(messageConfigEntity) || messageConfigEntity.getGroupIds().isEmpty()) {
            return Collections.emptyList();
        }
        Collection<MpMessageGroupUserEntity> groupUsers = mqMessageGroupUserService.getGroupUserByGroupIds(messageConfigEntity.getGroupIds());
        if(CollectionUtils.isEmpty(groupUsers)){
            return Collections.emptyList();
        }
        return groupUsers.stream().map(MpMessageGroupUserEntity::getUserId).collect(Collectors.toList());
    }

    @Override
    public boolean sendMessage(MpSendMessageDTO mpSendMessageDTO) {
        Integer orderId = Integer.valueOf(mpSendMessageDTO.getBusinessId().toString());
        Map<String, Object> data = mpSendMessageDTO.getData();
        String businessSceneCode = mpSendMessageDTO.getBusinessSceneCode();
        log.debug("开始发送模板消息");
        MpTemplateMessageConfigEntity messageConfigEntity = mpTemplateMessageConfigService.getGroupIdsByBusinessSceneCode(businessSceneCode);
        List<Integer> userIds = this.getGroupUserIdForMessageByBusinessSceneCode(businessSceneCode);
        if(CollectionUtils.isEmpty(userIds)){
            log.debug("sendMessage 没有找到用户");
            return false;
        }
        MpTemplateMessageDTO mpTemplateMessageDTO = new MpTemplateMessageDTO();
        mpTemplateMessageDTO.setBusinessId(String.valueOf(orderId));
        mpTemplateMessageDTO.setData(data);
        mpTemplateMessageDTO.setAppId(messageConfigEntity.getAppId());
        mpTemplateMessageDTO.setBusinessId(mpSendMessageDTO.getBusinessId().toString());
        mpTemplateMessageDTO.setUserIds(userIds);
        mpTemplateMessageDTO.setUrl(messageConfigEntity.getUrl());
        mpTemplateMessageDTO.setTemplateId(messageConfigEntity.getTemplateId());
        mpTemplateMessageDTO.setMiniProgramAppId(messageConfigEntity.getMiniProgramAppId());
        mpTemplateMessageDTO.setMiniProgramPagePath(messageConfigEntity.getMiniProgramPagePath());
        mpTemplateMessageService.sendTemplateMessage(mpTemplateMessageDTO);
        log.debug("发送模板消息结束");
        return true;
    }
}