liujie
2025-11-11 bb38f6c6274d4e2cc656a005fd7ba526566adcfc
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.ruoyi.web.controller.api;
 
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.TDeliberationVoteDTO;
import com.ruoyi.system.model.TCourse;
import com.ruoyi.system.model.TDeliberationVote;
import com.ruoyi.system.model.TDeliberationVoteOption;
import com.ruoyi.system.query.TDeliberationVoteQuery;
import com.ruoyi.system.service.TDeliberationVoteOptionService;
import com.ruoyi.system.service.TDeliberationVoteService;
import com.ruoyi.system.task.base.QuartzManager;
import com.ruoyi.system.task.base.TimeJobType;
import com.ruoyi.system.task.jobs.DeliberationVotePush;
import com.ruoyi.system.vo.TDeliberationVoteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 议事投票 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-11-06
 */
@Api(tags = "议事投票")
@RestController
@RequestMapping("/t-deliberation-vote")
public class TDeliberationVoteController {
    private final TDeliberationVoteService deliberationVoteService;
    private final TDeliberationVoteOptionService deliberationVoteOptionService;
    @Autowired
    public TDeliberationVoteController(TDeliberationVoteService deliberationVoteService, TDeliberationVoteOptionService deliberationVoteOptionService) {
        this.deliberationVoteService = deliberationVoteService;
        this.deliberationVoteOptionService = deliberationVoteOptionService;
    }
 
    /**
     * 获取议事投票管理管理列表
     */
    @ApiOperation(value = "获取议事投票管理分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TDeliberationVoteVO>> pageList(@RequestBody TDeliberationVoteQuery query) {
        return R.ok(deliberationVoteService.pageList(query));
    }
 
    /**
     * 获取议事投票管理管理列表
     */
    @ApiOperation(value = "获取议事投票管理列表")
    @PostMapping(value = "/list")
    public R<List<TDeliberationVote>> list() {
        return R.ok(deliberationVoteService.list(Wrappers.lambdaQuery(TDeliberationVote.class).orderByDesc(TDeliberationVote::getCreateTime)));
    }
 
    /**
     * 添加议事投票管理管理
     */
    @Log(title = "议事投票管理信息-新增议事投票管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加议事投票管理")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TDeliberationVoteDTO dto) {
        if (deliberationVoteService.isExit(dto)) {
            return R.fail("议事投票标题已存在");
        }
        LocalDateTime startTime = dto.getStartTime();
        LocalDateTime endTime = dto.getEndTime();
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(startTime)){
            dto.setStatus(1);
        }
        if(now.isAfter(startTime) && now.isBefore(endTime)){
            dto.setStatus(2);
        }
        if(now.isAfter(endTime)){
            dto.setStatus(3);
        }
        deliberationVoteService.save(dto);
        if(now.isBefore(startTime)){
            // 添加定时任务
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", dto.getId()).put("type","1")
                            .build();
            QuartzManager.addJob(
                    DeliberationVotePush.class,
                    (DeliberationVotePush.name+dto.getId()).toUpperCase(),
                    TimeJobType.DELIBERATION_VOTE_PUSH,
                    DateUtils.localDateTimeToDate(dto.getStartTime()),
                    maps
            );
        }
        if(now.isAfter(startTime) && now.isBefore(endTime)){
            // 添加定时任务
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", dto.getId()).put("type","2")
                            .build();
            QuartzManager.addJob(
                    DeliberationVotePush.class,
                    (DeliberationVotePush.name+dto.getId()).toUpperCase(),
                    TimeJobType.DELIBERATION_VOTE_PUSH,
                    DateUtils.localDateTimeToDate(dto.getEndTime()),
                    maps
            );
        }
        // 存储选项
        List<TDeliberationVoteOption> deliberationVoteOptions = dto.getDeliberationVoteOptions();
        for (TDeliberationVoteOption deliberationVoteOption : deliberationVoteOptions) {
            deliberationVoteOption.setVoteId(dto.getId());
        }
        deliberationVoteOptionService.saveBatch(deliberationVoteOptions);
        return R.ok();
    }
 
    /**
     * 修改议事投票管理
     */
    @Log(title = "议事投票管理信息-修改议事投票管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改议事投票管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TDeliberationVoteDTO dto) {
        if (deliberationVoteService.isExit(dto)) {
            return R.fail("议事投票标题已存在");
        }
        TDeliberationVote deliberationVote = deliberationVoteService.getById(dto.getId());
        if(deliberationVote.getStatus() == 2 || deliberationVote.getStatus() == 3){
            return R.fail("投票正在进行中或已结束,无法修改");
        }
        LocalDateTime startTime = dto.getStartTime();
        LocalDateTime endTime = dto.getEndTime();
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(startTime)){
            dto.setStatus(1);
            // 添加定时任务
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", deliberationVote.getId()).put("type","1")
                            .build();
            QuartzManager.addJob(
                    DeliberationVotePush.class,
                    (DeliberationVotePush.name+deliberationVote.getId()).toUpperCase(),
                    TimeJobType.DELIBERATION_VOTE_PUSH,
                    DateUtils.localDateTimeToDate(deliberationVote.getStartTime()),
                    maps
            );
        }
        if(now.isAfter(startTime) && now.isBefore(endTime)){
            dto.setStatus(2);
            // 添加定时任务
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", deliberationVote.getId()).put("type","2")
                            .build();
            QuartzManager.addJob(
                    DeliberationVotePush.class,
                    (DeliberationVotePush.name+deliberationVote.getId()).toUpperCase(),
                    TimeJobType.DELIBERATION_VOTE_PUSH,
                    DateUtils.localDateTimeToDate(deliberationVote.getEndTime()),
                    maps
            );
        }
        if(now.isAfter(endTime)){
            dto.setStatus(3);
        }
        deliberationVoteService.updateById(dto);
        // 删除所有选项
        deliberationVoteOptionService.remove(Wrappers.lambdaQuery(TDeliberationVoteOption.class).eq(TDeliberationVoteOption::getVoteId, dto.getId()));
        // 存储选项
        List<TDeliberationVoteOption> deliberationVoteOptions = dto.getDeliberationVoteOptions();
        for (TDeliberationVoteOption deliberationVoteOption : deliberationVoteOptions) {
            deliberationVoteOption.setVoteId(dto.getId());
        }
        deliberationVoteOptionService.saveBatch(deliberationVoteOptions);
 
        return R.ok();
    }
 
    /**
     * 查看议事投票管理详情
     */
    @ApiOperation(value = "查看议事投票管理详情")
    @GetMapping(value = "/getDetailById")
    public R<TDeliberationVote> getDetailById(@RequestParam String id) {
        TDeliberationVote deliberationVote = deliberationVoteService.getById(id);
        // 查询选项
        List<TDeliberationVoteOption> deliberationVoteOptions = deliberationVoteOptionService.list(Wrappers.lambdaQuery(TDeliberationVoteOption.class)
                .eq(TDeliberationVoteOption::getVoteId, id));
        if(!CollectionUtils.isEmpty(deliberationVoteOptions)){
            // 查看选项人数
            for (TDeliberationVoteOption deliberationVoteOption : deliberationVoteOptions) {
                String voteCount = deliberationVoteOption.getVoteCount();
                deliberationVoteOption.setVoteCountNum(voteCount == null ? 0 : voteCount.split(",").length);
            }
        }
        deliberationVote.setOptions(deliberationVoteOptions);
        return R.ok(deliberationVote);
    }
 
    /**
     * 删除议事投票管理
     */
    @Log(title = "议事投票管理信息-删除议事投票管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除议事投票管理")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        // 先删除选项
        deliberationVoteOptionService.remove(Wrappers.lambdaQuery(TDeliberationVoteOption.class).eq(TDeliberationVoteOption::getVoteId, id));
        return R.ok(deliberationVoteService.removeById(id));
    }
 
    /**
     * 批量删除议事投票管理
     */
    @Log(title = "议事投票管理信息-删除议事投票管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除议事投票管理")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        // 先删除选项
        deliberationVoteOptionService.remove(Wrappers.lambdaQuery(TDeliberationVoteOption.class).in(TDeliberationVoteOption::getVoteId, ids));
        return R.ok(deliberationVoteService.removeByIds(ids));
    }
 
    /**
     * 议事投票发布和完成
     */
    @Log(title = "议事投票管理信息-议事投票发布和完成", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "议事投票发布和完成")
    @PostMapping(value = "/updateReleaseFinish")
    public R<Boolean> updateReleaseFinish(@RequestParam(value = "id") String id,
                                          @RequestParam(value = "status") Integer status) {
 
        TDeliberationVote deliberationVote = deliberationVoteService.getById(id);
        if(status == 2){
            // 添加定时任务
            Map<String, ? extends Object> maps =
                    new ImmutableMap.Builder<String, String>().
                            put("id", deliberationVote.getId()).put("type","2")
                            .build();
            QuartzManager.addJob(
                    DeliberationVotePush.class,
                    (DeliberationVotePush.name+deliberationVote.getId()).toUpperCase(),
                    TimeJobType.DELIBERATION_VOTE_PUSH,
                    DateUtils.localDateTimeToDate(deliberationVote.getEndTime()),
                    maps
            );
        }
        deliberationVoteService.update(Wrappers.lambdaUpdate(TDeliberationVote.class)
                .set(TDeliberationVote::getStatus, status)
                .eq(TDeliberationVote::getId, id));
        return R.ok();
    }
 
}