From e9d5d32c7ee67eec2f47ed80fe9aae92a0ce7ec1 Mon Sep 17 00:00:00 2001 From: luodangjia <luodangjia> Date: 星期二, 14 一月 2025 10:17:38 +0800 Subject: [PATCH] 12.18 --- ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/RedPackegeSetController.java | 77 +++++++++++++++++++++++++++++--------- 1 files changed, 59 insertions(+), 18 deletions(-) diff --git a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/RedPackegeSetController.java b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/RedPackegeSetController.java index dfef420..d70cbd1 100644 --- a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/RedPackegeSetController.java +++ b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/RedPackegeSetController.java @@ -4,17 +4,19 @@ import com.ruoyi.account.api.model.AppUser; import com.ruoyi.common.core.domain.R; import com.ruoyi.other.api.domain.RedPackegeSet; +import com.ruoyi.other.dto.RedPackegeSetDto; import com.ruoyi.other.service.RedPackegeSetService; import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; /** * <p> @@ -49,8 +51,11 @@ */ @ApiOperation(value = "获取红包配置", tags = {"管理后台-活动管理-签到红包"}) @GetMapping("/getRedPackegeSet") - public R<RedPackegeSet> getRedPackegeSet(){ - return R.ok(redPackegeSetService.getOne(null)); + public R<RedPackegeSetDto> getRedPackegeSet(){ + List<RedPackegeSet> redPackegeSetList = redPackegeSetService.list(); + RedPackegeSetDto redPackegeSetDto = new RedPackegeSetDto(); + redPackegeSetDto.setRedPackegeSets(redPackegeSetList); + return R.ok(redPackegeSetDto); } @@ -58,18 +63,15 @@ * 添加红包配置 */ @ApiOperation(value = "添加红包配置", tags = {"管理后台-活动管理-签到红包"}) - @GetMapping("/addRedPackegeSet") - public R<Void> addRedPackegeSet(RedPackegeSet redPackegeSet){ - RedPackegeSet one = redPackegeSetService.getOne(null); - if (one==null){ - redPackegeSet.setId(1); - redPackegeSetService.save(redPackegeSet); - }else { - if (one.getId()==null){ - return R.fail("参数错误"); - } - redPackegeSetService.updateById(redPackegeSet); + @PostMapping("/addRedPackegeSet") + @Transactional(rollbackFor = Exception.class) + public R<Void> addRedPackegeSet(@RequestBody RedPackegeSetDto redPackegeSets){ + List<RedPackegeSet> redPackegeSetList = redPackegeSets.getRedPackegeSets(); + if (hasOverlap(redPackegeSetList)) { + return R.fail("时间段存在重叠,请重新配置"); } + redPackegeSetService.remove(null); + redPackegeSetService.saveBatch(redPackegeSetList); return R.ok(); } @@ -83,5 +85,44 @@ return R.ok(); } + public static boolean hasOverlap(List<RedPackegeSet> redPackegeSetList) { + // 首先根据startTime对列表进行排序 + Collections.sort(redPackegeSetList, Comparator.comparing(RedPackegeSet::getStartTime)); + + // 然后检查是否存在重叠 + for (int i = 0; i < redPackegeSetList.size() - 1; i++) { + RedPackegeSet current = redPackegeSetList.get(i); + RedPackegeSet next = redPackegeSetList.get(i + 1); + + // 如果当前时间段的结束时间晚于或等于下一个时间段的开始时间,则存在重叠 + if (current.getEndTime().isAfter(next.getStartTime()) || + current.getEndTime().isEqual(next.getStartTime())) { + return true; + } + } + + return false; + } + + public static void main(String[] args) { + // 示例数据 + RedPackegeSet redPackegeSet = new RedPackegeSet(); + redPackegeSet.setStartTime(LocalDateTime.of(2025, 1, 13, 0, 0)); + redPackegeSet.setEndTime(LocalDateTime.of(2025, 1, 14, 0, 0)); + + RedPackegeSet redPackegeSet2 = new RedPackegeSet(); + redPackegeSet2.setStartTime(LocalDateTime.of(2025, 1, 13, 0, 0)); + redPackegeSet2.setEndTime(LocalDateTime.of(2025, 1, 14, 0, 0)); + List<RedPackegeSet> redPackegeSetList = Arrays.asList( + redPackegeSet,redPackegeSet2 + ); + + boolean overlapExists = hasOverlap(redPackegeSetList); + System.out.println("是否存在重叠: " + overlapExists); + } + + + + } -- Gitblit v1.7.1