package com.yaoai.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yaoai.domain.entity.TeamGroup;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeamGroupMapper extends BaseMapper<TeamGroup> {

    default List<TeamGroup> findByTenant(Long tenantId) {
        return selectList(Wrappers.<TeamGroup>lambdaQuery().eq(TeamGroup::getTenantId, tenantId)
                .orderByAsc(TeamGroup::getCreatedAt));
    }

    @Insert("INSERT IGNORE INTO group_members(group_id, user_id) VALUES(#{groupId}, #{userId})")
    void addMember(@Param("groupId") Long groupId, @Param("userId") Long userId);

    @Delete("DELETE FROM group_members WHERE group_id=#{groupId} AND user_id=#{userId}")
    void removeMember(@Param("groupId") Long groupId, @Param("userId") Long userId);

    @Delete("DELETE FROM group_members WHERE group_id=#{groupId}")
    void removeAllMembers(@Param("groupId") Long groupId);

    @Select("SELECT user_id FROM group_members WHERE group_id=#{groupId}")
    List<Long> findMemberIds(@Param("groupId") Long groupId);
}
