团队关联人员功能
This commit is contained in:
parent
ae0ffa83a7
commit
eb0fb8eadc
|
|
@ -8,8 +8,10 @@ import org.hibernate.validator.constraints.Length;
|
|||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
|
|
@ -35,6 +37,9 @@ public class TeamDto {
|
|||
@Length(max = 32, message = "团队名称字符长度不能超过32", groups = {ADD.class, UPDATE.class})
|
||||
private String teamName;
|
||||
|
||||
@NotEmpty(message = "请选择至少一名团队成员", groups = {ADD.class, UPDATE.class})
|
||||
private List<Long> memberIds;
|
||||
|
||||
/**
|
||||
* 团队备注
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -4,7 +4,11 @@ import com.fasterxml.jackson.annotation.JsonFormat;
|
|||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @className:TeamVo
|
||||
|
|
@ -36,6 +40,26 @@ public class TeamVo {
|
|||
*/
|
||||
private Long teamNum;
|
||||
|
||||
/**
|
||||
* 新接收数据库GROUP_CONCAT拼接的成员ID字符串(如 "1001,1002,1003")
|
||||
*/
|
||||
private String memberIdsStr;
|
||||
|
||||
/**
|
||||
* 转换后的成员ID数组
|
||||
*/
|
||||
public List<Long> getMemberIds() {
|
||||
// 空值处理:避免字符串为空/Null时转换报错
|
||||
if (memberIdsStr == null || memberIdsStr.trim().isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
// 将逗号分隔的字符串转成Long类型的List
|
||||
return Arrays.stream(memberIdsStr.split(","))
|
||||
.map(String::trim) // 去除空格(防止拼接时出现多余空格)
|
||||
.map(Long::valueOf) // 转成Long类型
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -52,4 +52,33 @@ public interface ITeamMapper {
|
|||
* @date 2025/12/15 17:25
|
||||
*/
|
||||
List<Integer> teamIsAssociation(TeamDto dto);
|
||||
|
||||
/**
|
||||
* 批量插入团队-用户关联数据
|
||||
* @param teamId 团队ID
|
||||
* @param userIds 用户ID列表
|
||||
* @return void
|
||||
* @author lhdhy
|
||||
* @date 2026/1/7
|
||||
*/
|
||||
void batchInsertUserTeam(@Param("teamId") Long teamId, @Param("userIds") List<Long> userIds);
|
||||
|
||||
/**
|
||||
* 查询指定团队已关联的成员ID列表
|
||||
* @param teamId 团队ID
|
||||
* @return List<Long> 用户ID列表
|
||||
* @author lhdhy
|
||||
* @date 2026/1/7
|
||||
*/
|
||||
List<Long> selectUserIdsByTeamId(@Param("teamId") Long teamId);
|
||||
|
||||
/**
|
||||
* 批量删除指定团队的指定成员关联数据
|
||||
* @param teamId 团队ID
|
||||
* @param userIds 要移除的用户ID列表
|
||||
* @return void
|
||||
* @author lhdhy
|
||||
* @date 2026/1/7
|
||||
*/
|
||||
void batchDeleteUserTeam(@Param("teamId") Long teamId, @Param("userIds") List<Long> userIds);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @className:TeamServiceImpl
|
||||
|
|
@ -62,6 +63,14 @@ public class TeamServiceImpl implements ITeamService {
|
|||
// 2.添加团队数据
|
||||
teamMapper.operData(dto, 1);
|
||||
|
||||
Long teamId = dto.getTeamId();
|
||||
if (teamId == null) {
|
||||
throw new RuntimeException("团队ID生成失败");
|
||||
}
|
||||
|
||||
// 3.批量插入团队成员
|
||||
teamMapper.batchInsertUserTeam(teamId, dto.getMemberIds());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(), e);
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
|
|
@ -71,6 +80,7 @@ public class TeamServiceImpl implements ITeamService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AjaxResult editData(TeamDto dto) {
|
||||
// 校验数据是否合法
|
||||
String validResult = validatorsUtils.valid(dto, TeamDto.UPDATE.class);
|
||||
|
|
@ -86,6 +96,32 @@ public class TeamServiceImpl implements ITeamService {
|
|||
// 2.修改团队数据
|
||||
teamMapper.operData(dto, 2);
|
||||
|
||||
Long teamId = dto.getTeamId();
|
||||
// 3.查询该团队当前已关联的成员ID列表
|
||||
List<Long> existMemberIds = teamMapper.selectUserIdsByTeamId(teamId);
|
||||
// 前端传递的新成员ID列表
|
||||
List<Long> newMemberIds = dto.getMemberIds();
|
||||
|
||||
// 需要新增的成员(新选 - 现有)
|
||||
List<Long> addMemberIds = newMemberIds.stream()
|
||||
.filter(userId -> !existMemberIds.contains(userId))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 需要移除的成员(现有 - 新选)
|
||||
List<Long> removeMemberIds = existMemberIds.stream()
|
||||
.filter(userId -> !newMemberIds.contains(userId))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 6.执行新增成员关联
|
||||
if (!addMemberIds.isEmpty()) {
|
||||
teamMapper.batchInsertUserTeam(teamId, addMemberIds);
|
||||
}
|
||||
|
||||
// 7.执行移除成员关联
|
||||
if (!removeMemberIds.isEmpty()) {
|
||||
teamMapper.batchDeleteUserTeam(teamId, removeMemberIds);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(), e);
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bonus.system.mapper.ITeamMapper">
|
||||
<!--操作团队数据-->
|
||||
<insert id="operData">
|
||||
<insert id="operData" useGeneratedKeys="true" keyProperty="params.teamId" keyColumn="team_id">
|
||||
<if test="operType == 1">
|
||||
INSERT INTO sys_team
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
|
|
@ -38,7 +38,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
st.team_name AS teamName,
|
||||
st.create_time AS createTime,
|
||||
st.team_remark AS teamRemark,
|
||||
COUNT(suta.user_id) AS teamNum
|
||||
COUNT(suta.user_id) AS teamNum,
|
||||
-- 接该团队的所有成员ID为字符串
|
||||
GROUP_CONCAT(DISTINCT suta.user_id) AS memberIdsStr
|
||||
FROM sys_team st
|
||||
LEFT JOIN sys_user_team_association suta ON st.team_id = suta.team_id
|
||||
<where>
|
||||
|
|
@ -47,7 +49,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</if>
|
||||
AND st.del_flag = '0'
|
||||
</where>
|
||||
GROUP BY st.team_id, st.team_name, st.create_time
|
||||
GROUP BY st.team_id
|
||||
ORDER BY st.create_time DESC
|
||||
</select>
|
||||
<!--查询团队名称是否重复-->
|
||||
|
|
@ -64,4 +66,30 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
SELECT COUNT(*)
|
||||
FROM sys_user_team_association WHERE team_id = #{teamId}
|
||||
</select>
|
||||
|
||||
<!-- 批量插入团队-用户关联数据 -->
|
||||
<insert id="batchInsertUserTeam">
|
||||
INSERT INTO sys_user_team_association (user_id, team_id)
|
||||
VALUES
|
||||
<foreach collection="userIds" item="userId" separator=",">
|
||||
(#{userId}, #{teamId})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<!-- 查询指定团队已关联的成员ID列表 -->
|
||||
<select id="selectUserIdsByTeamId" resultType="java.lang.Long">
|
||||
SELECT user_id
|
||||
FROM sys_user_team_association
|
||||
WHERE team_id = #{teamId}
|
||||
</select>
|
||||
|
||||
<!-- 批量删除指定团队的指定成员关联数据 -->
|
||||
<delete id="batchDeleteUserTeam">
|
||||
DELETE FROM sys_user_team_association
|
||||
WHERE team_id = #{teamId}
|
||||
AND user_id IN
|
||||
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
|
||||
#{userId}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
|
|
|
|||
Loading…
Reference in New Issue