From d0ed0a1c891ab07f21dbabc51e063b8dabf9d1f4 Mon Sep 17 00:00:00 2001 From: chengpengxiang <15503679582@163.com> Date: Wed, 13 Dec 2023 22:55:51 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=94=B9=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=8A=A0=E5=85=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserSharingController.java | 4 +- .../dl/officialsite/team/TeamController.java | 9 ++-- .../com/dl/officialsite/team/TeamService.java | 43 ++++++++----------- .../team/teammember/TeamMemberRepository.java | 5 ++- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/dl/officialsite/sharing/controller/UserSharingController.java b/src/main/java/com/dl/officialsite/sharing/controller/UserSharingController.java index 7f802ec8..d86087fd 100644 --- a/src/main/java/com/dl/officialsite/sharing/controller/UserSharingController.java +++ b/src/main/java/com/dl/officialsite/sharing/controller/UserSharingController.java @@ -7,10 +7,10 @@ import com.dl.officialsite.sharing.model.resp.AllSharingResp; import com.dl.officialsite.sharing.model.resp.SharingByUserResp; import com.dl.officialsite.sharing.service.IUserSharingService; -import io.swagger.annotations.Api; + +import lombok.extern.slf4j.Slf4j;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.repository.query.Param; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/com/dl/officialsite/team/TeamController.java b/src/main/java/com/dl/officialsite/team/TeamController.java index 2389643b..1f50790b 100644 --- a/src/main/java/com/dl/officialsite/team/TeamController.java +++ b/src/main/java/com/dl/officialsite/team/TeamController.java @@ -35,12 +35,12 @@ BaseResponse list(@RequestParam String address) { } /** - * 新增团队 todo + * 新增团队 */ @PutMapping BaseResponse create(@RequestBody TeamVO team, @RequestParam String address) { - if (!address.equals("0x4DDE628ef50dE13E6E369353128A0d7899B54B6b")) { - throw new BizException(CodeEnums.TEAM_NOT_EXIST.getCode(), CodeEnums.TEAM_NOT_EXIST.getMsg()); + if (teamService.checkMemberIsAdmin(address)) { + throw new BizException(CodeEnums.NOT_THE_ADMIN.getCode(), CodeEnums.NOT_THE_ADMIN.getMsg()); } Team TeamNew = teamService.add(team); return BaseResponse.successWithData(TeamNew); @@ -60,6 +60,9 @@ BaseResponse join(@RequestBody TeamMemberJoinVO teamMember, @RequestParam String */ @PostMapping("/join/batch") BaseResponse batchJoin(@RequestBody TeamMemberBatchJoinVO teamMembers, @RequestParam String address) { + if (teamService.checkMemberIsAdmin(address)) { + throw new BizException(CodeEnums.NOT_THE_ADMIN.getCode(), CodeEnums.NOT_THE_ADMIN.getMsg()); + } teamService.batchJoin(teamMembers); return BaseResponse.successWithData(null); } diff --git a/src/main/java/com/dl/officialsite/team/TeamService.java b/src/main/java/com/dl/officialsite/team/TeamService.java index cabd182f..a7f86ebc 100644 --- a/src/main/java/com/dl/officialsite/team/TeamService.java +++ b/src/main/java/com/dl/officialsite/team/TeamService.java @@ -18,7 +18,10 @@ import com.dl.officialsite.team.vo.TeamsWithMembers; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; @@ -35,7 +38,7 @@ * @ClassName TeamService * @Author jackchen * @Date 2023/10/21 17:23 - * @Description TODO + * @Description TeamService **/ @Slf4j @Service @@ -287,28 +290,20 @@ public TeamsWithMembers getTeamById(Long teamId) { } public void batchJoin(TeamMemberBatchJoinVO teamMembers) { - - - // for admin interface - // todo batch insert ? - teamMembers.getMemberIds().forEach(memberId -> { - Optional optional = teamMemberRepository.findByTeamAndMember( - teamMembers.getTeamId(), memberId); - if (optional.isPresent()) { - TeamMember teamMember2 = optional.get(); -// if (teamMember2.getStatus() == Constants.REQUEST_TEAM) { -// throw new BizException(CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getCode(), -// CodeEnums.MEMBER_ALREADY_REQUEST_TEAM.getMsg()); -// } // ignore existence - teamMember2.setStatus(Constants.APPROVE_TEAM); - teamMemberRepository.save(teamMember2); - } else { - TeamMember teamMember1 = new TeamMember(); - teamMember1.setMemberId(memberId); - teamMember1.setTeamId(teamMembers.getTeamId()); - teamMember1.setStatus(Constants.APPROVE_TEAM); - teamMemberRepository.save(teamMember1); - } - }); + List teamMemberList = new ArrayList<>(); + List memberIds = teamMembers.getMemberIds(); + + // 批量查询已存在的TeamMember + Map existingMembersMap = teamMemberRepository.findByTeamAndMembers(teamMembers.getTeamId(), memberIds) + .stream() + .collect(Collectors.toMap(TeamMember::getMemberId, Function.identity())); + for (Long memberId : memberIds) { + TeamMember teamMember = existingMembersMap.getOrDefault(memberId, new TeamMember()); + teamMember.setMemberId(memberId); + teamMember.setTeamId(teamMembers.getTeamId()); + teamMember.setStatus(Constants.APPROVE_TEAM); + teamMemberList.add(teamMember); + } + teamMemberRepository.saveAll(teamMemberList); } } \ No newline at end of file diff --git a/src/main/java/com/dl/officialsite/team/teammember/TeamMemberRepository.java b/src/main/java/com/dl/officialsite/team/teammember/TeamMemberRepository.java index 51b9ffe6..32e4e830 100644 --- a/src/main/java/com/dl/officialsite/team/teammember/TeamMemberRepository.java +++ b/src/main/java/com/dl/officialsite/team/teammember/TeamMemberRepository.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.Optional; - import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -34,4 +33,8 @@ public interface TeamMemberRepository extends JpaRepository { @Query(value = "select * from team_member where member_id = :member_id",nativeQuery = true) List findByMemberId(@Param("member_id")Long memberId); + + @Query(value = "select * from team_member where team_id = :team_id and member_id in (:memberIds)", + nativeQuery = true) + List findByTeamAndMembers(@Param("team_id")Long teamId, @Param("memberIds")List memberIds); }