|
|
@@ -2,10 +2,13 @@ package org.dromara.business.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.dromara.business.domain.*;
|
|
|
import org.dromara.business.domain.bo.ItemsPrizeBo;
|
|
|
import org.dromara.business.domain.bo.TournamentBlindStructuresBo;
|
|
|
-import org.dromara.business.domain.vo.TournamentBlindStructuresListVo;
|
|
|
+import org.dromara.business.domain.dto.ItemsPrizeDto;
|
|
|
+import org.dromara.business.domain.dto.TournamentsDto;
|
|
|
+import org.dromara.business.domain.vo.*;
|
|
|
import org.dromara.business.mapper.*;
|
|
|
import org.dromara.business.service.IItemsService;
|
|
|
import org.dromara.common.core.utils.MapstructUtils;
|
|
|
@@ -19,13 +22,19 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.system.domain.vo.SysOssVo;
|
|
|
import org.dromara.system.service.ISysOssService;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.dromara.business.domain.bo.TournamentsBo;
|
|
|
-import org.dromara.business.domain.vo.TournamentsVo;
|
|
|
import org.dromara.business.service.ITournamentsService;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 【请填写功能名称】Service业务层处理
|
|
|
@@ -64,7 +73,47 @@ public class TournamentsServiceImpl implements ITournamentsService {
|
|
|
*/
|
|
|
@Override
|
|
|
public TournamentsVo queryById(Long id){
|
|
|
- return baseMapper.selectVoByIdInfo(id);
|
|
|
+ TournamentsVo tournamentsVo = baseMapper.selectVoByIdInfo(id);
|
|
|
+ Long tournamentId = tournamentsVo.getId();
|
|
|
+ //赛事报名条件
|
|
|
+ TournamentEntryConditionsVo tournamentEntryConditionsVo = tournamentEntryConditionsMapper.selectByTournamentInfo(tournamentId);
|
|
|
+ if(tournamentEntryConditionsVo!=null){
|
|
|
+ tournamentsVo.setItemsId(tournamentEntryConditionsVo.getItemId());
|
|
|
+ tournamentsVo.setItemsNum(tournamentEntryConditionsVo.getRequiredQuantity());
|
|
|
+ }
|
|
|
+
|
|
|
+ //绑定的盲注信息
|
|
|
+ TournamentBlindStructuresVo tournamentBlindStructuresVo = tournamentBlindStructuresMapper.selectTournamentBlindStructureByTournamentId(tournamentId);
|
|
|
+ if(tournamentBlindStructuresVo!=null){
|
|
|
+ tournamentsVo.setBlindStructureId(tournamentBlindStructuresVo.getBlindStructureId());
|
|
|
+ }
|
|
|
+
|
|
|
+ //排名
|
|
|
+ List<PrizeDistributionsVo> prizeDistributionsVos = prizeDistributionsMapper.selectByTournamentId(tournamentId);
|
|
|
+ if(prizeDistributionsVos!=null){
|
|
|
+ List<ItemsPrizeDto> itemsPrizeList=new ArrayList<>();
|
|
|
+
|
|
|
+ // 提取并按排名正序排序后,取出 id 列表
|
|
|
+ List<Long> ids = prizeDistributionsVos.stream()
|
|
|
+ .sorted(Comparator.comparing(PrizeDistributionsVo::getPaiming))
|
|
|
+ .map(PrizeDistributionsVo::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ int i=0;
|
|
|
+ for (Long id2 : ids) {
|
|
|
+ i++;
|
|
|
+ //奖品
|
|
|
+ PrizeDistributionItemsVo prizeDistributionItemsVos = prizeDistributionItemsMapper.selectByPrizeDistributionId(id2);
|
|
|
+ ItemsPrizeDto itemsPrizeBo = new ItemsPrizeDto();
|
|
|
+ itemsPrizeBo.setRanking(Long.valueOf(i));
|
|
|
+ itemsPrizeBo.setItemId(prizeDistributionItemsVos.getItemId());
|
|
|
+ itemsPrizeBo.setQuantity(prizeDistributionItemsVos.getQuantity());
|
|
|
+ itemsPrizeList.add(itemsPrizeBo);
|
|
|
+
|
|
|
+ }
|
|
|
+ tournamentsVo.setItemsPrizeList(itemsPrizeList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return tournamentsVo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -117,35 +166,63 @@ public class TournamentsServiceImpl implements ITournamentsService {
|
|
|
* @return 是否新增成功
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean insertByBo(TournamentsBo bo) {
|
|
|
- // 转换 BO -> Entity
|
|
|
- Tournaments add = MapstructUtils.convert(bo, Tournaments.class);
|
|
|
- validEntityBeforeSave(add);
|
|
|
- List<ItemsPrizeBo> itemsPrizeList = bo.getItemsPrizeList();
|
|
|
- if (CollectionUtils.isEmpty(itemsPrizeList)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ public Boolean insertByBo(TournamentsDto bo, MultipartFile competitionIcon) {
|
|
|
+ try {
|
|
|
+ // 初始化 ObjectMapper
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
- // 插入赛事主表
|
|
|
- boolean flag = baseMapper.insertTournament(add) > 0;
|
|
|
- if (!flag) {
|
|
|
- throw new RuntimeException("赛事插入失败");
|
|
|
- }
|
|
|
+ // 转换 BO -> Entity
|
|
|
+ Tournaments add = new Tournaments();
|
|
|
+ BeanUtils.copyProperties(bo, add);
|
|
|
|
|
|
- Long tournamentId = add.getId();
|
|
|
+ validEntityBeforeSave(add);
|
|
|
|
|
|
- // 绑定盲注结构
|
|
|
- bindBlindStructure(tournamentId, bo.getBlindStructureId());
|
|
|
+ // 解析 itemsPrizeList JSON 字符串
|
|
|
+ List<ItemsPrizeDto> prizeList = parsePrizeList(bo.getItemsPrizeList(), mapper);
|
|
|
|
|
|
- // 添加报名条件
|
|
|
- addEntryCondition(tournamentId, bo.getItemsId(), bo.getItemsNum());
|
|
|
+ if (CollectionUtils.isEmpty(prizeList)) {
|
|
|
+ log.error("奖励列表为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 处理奖励分布
|
|
|
- processPrizeDistributions(tournamentId, itemsPrizeList);
|
|
|
+ // 处理开始时间
|
|
|
+ add.setStartTime(parseDateTime(bo.getStartTime()));
|
|
|
|
|
|
- return true;
|
|
|
+ // 上传赛事图标
|
|
|
+ if (competitionIcon != null && !competitionIcon.isEmpty()) {
|
|
|
+ SysOssVo sysOssVo = this.uploadTournament(competitionIcon);
|
|
|
+ if (sysOssVo != null) {
|
|
|
+ add.setCompetitionIcon(sysOssVo.getUrl());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入赛事主表
|
|
|
+ boolean flag = baseMapper.insertTournament(add) > 0;
|
|
|
+ if (!flag) {
|
|
|
+ throw new RuntimeException("赛事插入失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long tournamentId = add.getId();
|
|
|
+
|
|
|
+ // 绑定盲注结构
|
|
|
+ bindBlindStructure(tournamentId, bo.getBlindStructureId());
|
|
|
+
|
|
|
+ // 添加报名条件
|
|
|
+ addEntryCondition(tournamentId, bo.getItemsId(), bo.getItemsNum());
|
|
|
+
|
|
|
+ // 处理奖励分布
|
|
|
+ processPrizeDistributions(tournamentId, prizeList);
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("赛事插入过程中发生异常", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
private void bindBlindStructure(Long tournamentId, Long blindStructureId) {
|
|
|
TournamentBlindStructure structure = new TournamentBlindStructure();
|
|
|
structure.setTournamentId(tournamentId);
|
|
|
@@ -161,20 +238,20 @@ public class TournamentsServiceImpl implements ITournamentsService {
|
|
|
tournamentEntryConditionsMapper.insert(condition);
|
|
|
}
|
|
|
|
|
|
- private void processPrizeDistributions(Long tournamentId, List<ItemsPrizeBo> itemsPrizeList) {
|
|
|
+ private void processPrizeDistributions(Long tournamentId, List<ItemsPrizeDto> itemsPrizeList) {
|
|
|
// 排序(按排名正序)
|
|
|
- Collections.sort(itemsPrizeList, Comparator.comparing(ItemsPrizeBo::getRanking));
|
|
|
+ Collections.sort(itemsPrizeList, Comparator.comparing(ItemsPrizeDto::getRanking));
|
|
|
|
|
|
- for (ItemsPrizeBo prizeBo : itemsPrizeList) {
|
|
|
+ for (ItemsPrizeDto prizeBo : itemsPrizeList) {
|
|
|
PrizeDistributions prizeDistribution = new PrizeDistributions();
|
|
|
prizeDistribution.setTournamentId(tournamentId);
|
|
|
- prizeDistribution.setPaiming(prizeBo.getRanking());
|
|
|
+ prizeDistribution.setPaiming(Long.valueOf(prizeBo.getRanking()));
|
|
|
prizeDistributionsMapper.insert(prizeDistribution);
|
|
|
|
|
|
PrizeDistributionItems distributionItems = new PrizeDistributionItems();
|
|
|
distributionItems.setPrizeDistributionId(prizeDistribution.getId());
|
|
|
- distributionItems.setItemId(prizeBo.getItemId());
|
|
|
- distributionItems.setQuantity(prizeBo.getQuantity());
|
|
|
+ distributionItems.setItemId(Long.valueOf(prizeBo.getItemId()));
|
|
|
+ distributionItems.setQuantity(Long.valueOf(prizeBo.getQuantity()));
|
|
|
prizeDistributionItemsMapper.insert(distributionItems);
|
|
|
}
|
|
|
}
|
|
|
@@ -186,10 +263,68 @@ public class TournamentsServiceImpl implements ITournamentsService {
|
|
|
* @return 是否修改成功
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean updateByBo(TournamentsBo bo) {
|
|
|
- Tournaments update = MapstructUtils.convert(bo, Tournaments.class);
|
|
|
- validEntityBeforeSave(update);
|
|
|
- return baseMapper.updateTournamentsById(update) > 0;
|
|
|
+ public Boolean updateByBo(TournamentsDto bo, MultipartFile competitionIcon) {
|
|
|
+ try {
|
|
|
+ // 初始化 ObjectMapper
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
+ // 转换 BO -> Entity
|
|
|
+ Tournaments update = new Tournaments();
|
|
|
+ BeanUtils.copyProperties(bo, update);
|
|
|
+
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ // 解析 itemsPrizeList JSON 字符串
|
|
|
+ List<ItemsPrizeDto> prizeList = parsePrizeList(bo.getItemsPrizeList(), mapper);
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(prizeList)) {
|
|
|
+ log.error("奖励列表为空");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理开始时间
|
|
|
+ update.setStartTime(parseDateTime(bo.getStartTime()));
|
|
|
+
|
|
|
+ // 上传赛事图标
|
|
|
+ if (competitionIcon != null && !competitionIcon.isEmpty()) {
|
|
|
+ SysOssVo sysOssVo = this.uploadTournament(competitionIcon);
|
|
|
+ if (sysOssVo != null) {
|
|
|
+ update.setCompetitionIcon(sysOssVo.getUrl());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入赛事主表
|
|
|
+ boolean flag = baseMapper.updateTournamentsById(update) > 0;
|
|
|
+ if (!flag) {
|
|
|
+ throw new RuntimeException("赛事修改失败");
|
|
|
+ }
|
|
|
+ Long tournamentId= update.getId();
|
|
|
+ //删除 旧盲注绑定信息
|
|
|
+ tournamentBlindStructuresMapper.deleteTournamentBlindStructureByTournamentId(tournamentId);
|
|
|
+
|
|
|
+ // 绑定盲注结构
|
|
|
+ bindBlindStructure(tournamentId, bo.getBlindStructureId());
|
|
|
+
|
|
|
+ // 修改报名条件
|
|
|
+ TournamentEntryConditions tournamentEntryConditions=new TournamentEntryConditions();
|
|
|
+ tournamentEntryConditions.setItemId(bo.getItemsId());
|
|
|
+ tournamentEntryConditions.setRequiredQuantity(bo.getItemsNum());
|
|
|
+ tournamentEntryConditionsMapper.updateByTournamentId(tournamentEntryConditions);
|
|
|
+
|
|
|
+ //删除奖励排名 同时删除奖励对应信息
|
|
|
+ List<PrizeDistributionsVo> prizeDistributionsVos = prizeDistributionsMapper.selectByTournamentId(tournamentId);
|
|
|
+ if(prizeDistributionsVos.size()>0){
|
|
|
+ for (PrizeDistributionsVo prizeDistributionsVo : prizeDistributionsVos) {
|
|
|
+ prizeDistributionsMapper.deleteById(prizeDistributionsVo.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //重新处理奖励分布
|
|
|
+ processPrizeDistributions(tournamentId, prizeList);
|
|
|
+ return true;
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("赛事修改过程中发生异常", e);
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -238,4 +373,32 @@ public class TournamentsServiceImpl implements ITournamentsService {
|
|
|
return sysOssVo;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private List<ItemsPrizeDto> parsePrizeList(String itemsPrizeListJson, ObjectMapper mapper) throws Exception {
|
|
|
+ if (itemsPrizeListJson == null || itemsPrizeListJson.isBlank()) {
|
|
|
+ throw new IllegalArgumentException("itemsPrizeList 不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ String cleanedJson = itemsPrizeListJson.trim();
|
|
|
+ if (cleanedJson.startsWith(",")) {
|
|
|
+ cleanedJson = cleanedJson.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return mapper.readValue(
|
|
|
+ cleanedJson,
|
|
|
+ mapper.getTypeFactory().constructCollectionType(List.class, ItemsPrizeDto.class)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private Date parseDateTime(String dateTimeStr) throws ParseException {
|
|
|
+ if (dateTimeStr == null || dateTimeStr.isBlank()) {
|
|
|
+ throw new IllegalArgumentException("startTime 不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, formatter);
|
|
|
+ return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|