|
|
@@ -3,10 +3,13 @@ package com.izouma.yags.service;
|
|
|
import com.izouma.yags.camp.api.CampApiService;
|
|
|
import com.izouma.yags.camp.api.RongYaoRole;
|
|
|
import com.izouma.yags.domain.BindGame;
|
|
|
+import com.izouma.yags.dto.PageQuery;
|
|
|
import com.izouma.yags.exception.BusinessException;
|
|
|
import com.izouma.yags.repo.BindGameRepo;
|
|
|
+import com.izouma.yags.utils.JpaUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
@@ -22,7 +25,15 @@ public class BindGameService {
|
|
|
private CampApiService campApiService;
|
|
|
private BindGameRepo bindGameRepo;
|
|
|
|
|
|
- public Object bind(Long userId, Long gameId, String campId, String roleId) throws IOException {
|
|
|
+ public Page<BindGame> all(PageQuery pageQuery) {
|
|
|
+ return bindGameRepo.findAll(JpaUtils.toSpecification(pageQuery, BindGame.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<BindGame> userBindGame(Long userId) {
|
|
|
+ return bindGameRepo.findByUserId(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object bind(Long userId, Long gameId, String zone, String campId, String roleId) throws IOException {
|
|
|
Objects.requireNonNull(gameId);
|
|
|
Objects.requireNonNull(campId);
|
|
|
if (gameId == 21) {
|
|
|
@@ -36,11 +47,19 @@ public class BindGameService {
|
|
|
map.put("msg", "请选择角色");
|
|
|
map.put("roleItems", roleItems);
|
|
|
} else {
|
|
|
- BindGame bindGame = bindGameRepo.findByUserIdAndGameId(userId, gameId)
|
|
|
- .orElse(new BindGame(userId));
|
|
|
+ List<BindGame> bindList = bindGameRepo.findByUserIdAndGameIdAndZone(userId, gameId, zone);
|
|
|
+ if (bindList.stream().anyMatch(b -> b.getRongYaoRole() != null
|
|
|
+ && b.getRongYaoRole().getRoleId().equals(roleId))) {
|
|
|
+ throw new BusinessException("请勿重复绑定");
|
|
|
+ }
|
|
|
+ BindGame bindGame = new BindGame(userId);
|
|
|
bindGame.setGameId(gameId);
|
|
|
+ bindGame.setZone(zone);
|
|
|
bindGame.setRongYaoRole(roleItems.stream().filter(r -> r.getRoleId().equals(roleId))
|
|
|
.findFirst().orElseThrow(new BusinessException("未查询到游戏角色")));
|
|
|
+ if (bindList.isEmpty() || bindList.stream().noneMatch(BindGame::isActive)) {
|
|
|
+ bindGame.setActive(true);
|
|
|
+ }
|
|
|
bindGameRepo.save(bindGame);
|
|
|
map.put("code", 0);
|
|
|
}
|
|
|
@@ -48,4 +67,31 @@ public class BindGameService {
|
|
|
}
|
|
|
throw new BusinessException("暂不支持该游戏");
|
|
|
}
|
|
|
+
|
|
|
+ public void unbind(Long userId, Long bindId) {
|
|
|
+ BindGame bindGame = bindGameRepo.findById(bindId).orElseThrow(new BusinessException("未查询到绑定记录"));
|
|
|
+ bindGameRepo.delete(bindGame);
|
|
|
+ List<BindGame> bindList = bindGameRepo.findByUserIdAndGameIdAndZone(userId, bindGame.getGameId(), bindGame.getZone());
|
|
|
+ if (!bindList.isEmpty() && bindList.stream().noneMatch(BindGame::isActive)) {
|
|
|
+ bindList.stream().findAny().ifPresent(b -> {
|
|
|
+ b.setActive(true);
|
|
|
+ bindGameRepo.save(b);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void activate(Long userId, Long bindId) {
|
|
|
+ BindGame bindGame = bindGameRepo.findById(bindId).orElseThrow(new BusinessException("未查询到绑定记录"));
|
|
|
+ if (bindGame.getUserId().equals(userId)) {
|
|
|
+ List<BindGame> bindList = bindGameRepo.findByUserIdAndGameIdAndZone(userId, bindGame.getGameId(), bindGame.getZone());
|
|
|
+ bindList.stream().filter(b -> !b.getId().equals(bindId)).forEach(b -> {
|
|
|
+ b.setActive(false);
|
|
|
+ bindGameRepo.save(b);
|
|
|
+ });
|
|
|
+ bindGame.setActive(true);
|
|
|
+ bindGameRepo.save(bindGame);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("无权激活");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|