Skip to content

Commit

Permalink
Merge pull request #92 from cheng521521/main
Browse files Browse the repository at this point in the history
fix:修改批量加入代码
  • Loading branch information
yanyanho authored Dec 14, 2023
2 parents 812eac7 + d0ed0a1 commit 2e42514
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/dl/officialsite/team/TeamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
43 changes: 19 additions & 24 deletions src/main/java/com/dl/officialsite/team/TeamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +38,7 @@
* @ClassName TeamService
* @Author jackchen
* @Date 2023/10/21 17:23
* @Description TODO
* @Description TeamService
**/
@Slf4j
@Service
Expand Down Expand Up @@ -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<TeamMember> 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<TeamMember> teamMemberList = new ArrayList<>();
List<Long> memberIds = teamMembers.getMemberIds();

// 批量查询已存在的TeamMember
Map<Long, TeamMember> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,4 +33,8 @@ public interface TeamMemberRepository extends JpaRepository<TeamMember, Long> {

@Query(value = "select * from team_member where member_id = :member_id",nativeQuery = true)
List<TeamMember> 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<TeamMember> findByTeamAndMembers(@Param("team_id")Long teamId, @Param("memberIds")List<Long> memberIds);
}

0 comments on commit 2e42514

Please sign in to comment.