Pu Zhibing
2025-04-03 1f09f6daaf73bc83cceb4ae22b862b7b365635cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ruoyi.other.controller;
 
 
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.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>
 *  前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@RestController
@RequestMapping("/red-packege-set")
public class RedPackegeSetController {
    @Resource
    private RedPackegeSetService redPackegeSetService;
    //获取当前生效的红包设置
    @ResponseBody
    @PostMapping("/get")
    public R<BigDecimal> get(){
        LocalDateTime now = LocalDateTime.now();
        RedPackegeSet one = redPackegeSetService.lambdaQuery().le(RedPackegeSet::getStartTime, now).ge(RedPackegeSet::getEndTime, now).one();
        if (one!=null){
            return R.ok(one.getPackegeAmount());
        }else {
            return R.ok(BigDecimal.ZERO);
 
        }
    }
 
 
    /**
     * 获取红包配置
     */
    @ApiOperation(value = "获取红包配置", tags = {"管理后台-活动管理-签到红包"})
    @GetMapping("/getRedPackegeSet")
    public R<RedPackegeSetDto> getRedPackegeSet(){
        List<RedPackegeSet> redPackegeSetList = redPackegeSetService.list();
        RedPackegeSetDto redPackegeSetDto = new RedPackegeSetDto();
        redPackegeSetDto.setRedPackegeSets(redPackegeSetList);
        return R.ok(redPackegeSetDto);
    }
 
 
    /**
     * 添加红包配置
     */
    @ApiOperation(value = "添加红包配置", tags = {"管理后台-活动管理-签到红包"})
    @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();
    }
 
    /**
     * 删除红包配置
     */
    @ApiOperation(value = "删除红包配置", tags = {"管理后台-活动管理-签到红包"})
    @GetMapping("/delRedPackegeSet")
    public R<Void> delRedPackegeSet(){
        redPackegeSetService.removeById(1);
        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);
    }
 
 
 
 
}