package com.seer.teach.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.mp.entity.MpMessageGroupEntity;
import com.seer.teach.mp.entity.MpMessageGroupUserEntity;
import com.seer.teach.mp.mapper.MqMessageGroupUserMapper;
import com.seer.teach.mp.service.IMqMessageGroupService;
import com.seer.teach.mp.service.IMqMessageGroupUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 群组人员关系表 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-08-09 20:26:58
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MqMessageGroupUserServiceImpl extends ServiceImpl<MqMessageGroupUserMapper, MpMessageGroupUserEntity> implements IMqMessageGroupUserService {

    private final IMqMessageGroupService mqMessageGroupService;


    @Override
    public void create(Integer groupId, Set<Integer> userIds) {
        if(CollectionUtils.isEmpty(userIds)){
            return;
        }
        List<MpMessageGroupUserEntity> exitsUsers = list(new LambdaQueryWrapper<MpMessageGroupUserEntity>()
                .eq(MpMessageGroupUserEntity::getGroupId, groupId)
                .in(MpMessageGroupUserEntity::getUserId, userIds));
        MpMessageGroupEntity MesGroupUserEntity =  mqMessageGroupService.getById(groupId);
        AssertUtils.notNull(MesGroupUserEntity, ResultCodeEnum.MESSAGE_GROUP_NOT_EXIST);

        if(!CollectionUtils.isEmpty(exitsUsers)){
            Set<Integer> exitsUserIds = exitsUsers.stream().map(MpMessageGroupUserEntity::getUserId).collect(Collectors.toSet());
            userIds.removeAll(exitsUserIds);
        }
        if(CollectionUtils.isEmpty(userIds)){
            log.info("用户{}已存在群组中",userIds);
            return;
        }
        List<MpMessageGroupUserEntity> messageGroupUserList = userIds.stream().map(userId -> {
            MpMessageGroupUserEntity entity = new MpMessageGroupUserEntity();
            entity.setGroupId(groupId);
            entity.setUserId(userId);
            return entity;
        }).collect(Collectors.toList());
        boolean result = saveBatch(messageGroupUserList);
        log.info("添加用户{}到群组{}成功:{}",userIds.size(),groupId,result);
    }

    /**
     * 根据ID查询消息组用户
     * @param id
     * @return
     */
    @Override
    public MpMessageGroupUserEntity getMesGroupUserById(Integer id) {
        return getById(id);
    }

    /**
     * 分页查询消息组用户列表
     * @param params
     * @return
     */
    @Override
    public Page<MpMessageGroupUserEntity> pageList(Page<MpMessageGroupUserEntity> pageParam,MpMessageGroupUserEntity params) {
        LambdaQueryWrapper<MpMessageGroupUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MpMessageGroupUserEntity::getGroupId, params.getGroupId());
        Page<MpMessageGroupUserEntity> resultPage = this.page(pageParam, wrapper);
        log.info("分页查询消息组用户列表:{}",resultPage);
        return resultPage;
    }


    /**
     * 删除消息组用户
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        MpMessageGroupUserEntity entity = getById(id);
        if(entity == null){
            throw new RuntimeException("未查询到消息组用户");
        }
        boolean result = removeById(id);
        log.info("删除消息组用户{}成功:{}",id,result);
        if(!result){
            throw new RuntimeException("删除消息组用户失败");
        }
    }

    @Override
    public Collection<MpMessageGroupUserEntity> getGroupUserByGroupIds(Collection<Integer> groupIds) {
        if(CollectionUtils.isEmpty(groupIds)){
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<MpMessageGroupUserEntity>().in(MpMessageGroupUserEntity::getGroupId, groupIds));
    }
}
