package com.ruoyi.project.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceErrorException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.enums.ReadStatusEnum;
import com.ruoyi.project.common.domain.MessageChatContent;
import com.ruoyi.project.common.domain.MessageChatGroup;
import com.ruoyi.project.common.domain.MessageChatUser;
import com.ruoyi.project.common.domain.MessageContentUser;
import com.ruoyi.project.common.dto.MessageChatUserDto;
import com.ruoyi.project.common.mapper.MessageChatUserMapper;
import com.ruoyi.project.common.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天室参与者Service业务层处理
 *
 * @author jubs
 * @date 2024-05-14
 */
@Service
public class MessageChatUserServiceImpl extends ServiceImpl<MessageChatUserMapper, MessageChatUser> implements IMessageChatUserService {
    @Resource
    private MessageChatUserMapper messageChatUserMapper;

    @Resource
    private IMessageChatGroupService iMessageChatGroupService;
    @Resource
    private IMessageChatContentService iMessageChatContentService;
    @Resource
    private IMessageContentUserService iMessageContentUserService;


    @Override
    public int deleteMe(MessageChatUserDto messageChatUser) {
        Long currentUserId = SecurityUtils.getUserId();
        Long chatRoomId = messageChatUser.getMessageChatId();
        List<MessageChatUser> messageChatUsers = list(
                Wrappers.<MessageChatUser>lambdaQuery()
                        .eq(MessageChatUser::getMessageChatId, chatRoomId)
                        .eq(MessageChatUser::getUserId, currentUserId)
                        .eq(MessageChatUser::getDelFlag, DelFlagEnum.NORMAL.getValue())
        );
        if (messageChatUsers != null && !messageChatUsers.isEmpty()) {
            this.update(
                    Wrappers.<MessageChatUser>lambdaUpdate()
                            .eq(MessageChatUser::getMessageChatId, chatRoomId)
                            .eq(MessageChatUser::getUserId, currentUserId)
                            .set(MessageChatUser::getVisible, ChatRoomVisibleEnum.HIDE.getCode())
                            .set(MessageChatUser::getUpdateTime, new Date())
                            .set(MessageChatUser::getUpdateBy, currentUserId)
            );
        } else {
            //数据已被删除
            throw new ServiceErrorException(MessageUtils.message("common.dataDeleted"));
        }
        // @Date 2024/7/8 14:56 @Author jubs 😀 @description: 追加需求，在删除聊天室时，已读该聊天室的所有讯息
        List<MessageChatGroup> messageChatGroups = iMessageChatGroupService.list(
                Wrappers.<MessageChatGroup>lambdaQuery()
                        .eq(MessageChatGroup::getMessageChatId, chatRoomId)
                        .eq(MessageChatGroup::getDelFlag, DelFlagEnum.NORMAL.getValue())
        );
        if (messageChatGroups != null && !messageChatGroups.isEmpty()) {
            List<Long> groupIds = messageChatGroups.stream().map(MessageChatGroup::getId).collect(Collectors.toList());
            if (!groupIds.isEmpty()) {
                //查询聊天记录
                List<MessageChatContent> messageChatContents = iMessageChatContentService.list(
                        Wrappers.<MessageChatContent>lambdaQuery()
                                .in(MessageChatContent::getMessageChatGroupId, groupIds)
                                .eq(MessageChatContent::getDelFlag, DelFlagEnum.NORMAL.getValue())
                );
                if (messageChatContents != null && !messageChatContents.isEmpty()) {
                    List<Long> contentIds = messageChatContents.stream().map(MessageChatContent::getId).collect(Collectors.toList());
                    //查询这个聊天室下 对于我来说 未读的信息
                    List<MessageContentUser> messageToMe = iMessageContentUserService.list(
                            Wrappers.<MessageContentUser>lambdaQuery()
                                    .in(MessageContentUser::getMessageChatContentId, contentIds)
                                    .eq(MessageContentUser::getReceiveUserId, currentUserId)
                                    .eq(MessageContentUser::getDelFlag, DelFlagEnum.NORMAL.getValue())
                                    .eq(MessageContentUser::getReadStatus, ReadStatusEnum.UN_READ.getValue())
                    );
                    if (messageToMe != null && !messageToMe.isEmpty()) {

                        List<Long> messageToMeIds = messageToMe.stream()
                                .map(MessageContentUser::getId)
                                .collect(Collectors.toList());
                        if (!messageToMeIds.isEmpty()) {
                            iMessageContentUserService.update(
                                    Wrappers.<MessageContentUser>lambdaUpdate()
                                            .in(MessageContentUser::getId, messageToMeIds)
                                            .set(MessageContentUser::getReadStatus, ReadStatusEnum.READ.getValue())
                                            .set(MessageContentUser::getUpdateBy, currentUserId)
                                            .set(MessageContentUser::getUpdateTime, new Date())
                            );
                        }
                    }
                }
            }
        }
        //查询 对于我来说 还有未读讯息
        List<MessageContentUser> contentUnRead = iMessageContentUserService.list(
                Wrappers.<MessageContentUser>lambdaQuery()
                        .eq(MessageContentUser::getReceiveUserId, currentUserId)
                        .eq(MessageContentUser::getDelFlag, DelFlagEnum.NORMAL.getValue())
                        .eq(MessageContentUser::getReadStatus, ReadStatusEnum.UN_READ.getValue())
        );
        return contentUnRead != null && !contentUnRead.isEmpty() ? contentUnRead.size() : 0;
    }
}
