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;
/**
*
* 议事投票 前端控制器
*
*
* @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> pageList(@RequestBody TDeliberationVoteQuery query) {
return R.ok(deliberationVoteService.pageList(query));
}
/**
* 获取议事投票管理管理列表
*/
@ApiOperation(value = "获取议事投票管理列表")
@PostMapping(value = "/list")
public R> 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 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 maps =
new ImmutableMap.Builder().
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 maps =
new ImmutableMap.Builder().
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 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 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 maps =
new ImmutableMap.Builder().
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 maps =
new ImmutableMap.Builder().
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 deliberationVoteOptions = dto.getDeliberationVoteOptions();
for (TDeliberationVoteOption deliberationVoteOption : deliberationVoteOptions) {
deliberationVoteOption.setVoteId(dto.getId());
}
deliberationVoteOptionService.saveBatch(deliberationVoteOptions);
return R.ok();
}
/**
* 查看议事投票管理详情
*/
@ApiOperation(value = "查看议事投票管理详情")
@GetMapping(value = "/getDetailById")
public R getDetailById(@RequestParam String id) {
TDeliberationVote deliberationVote = deliberationVoteService.getById(id);
// 查询选项
List 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 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 deleteByIds(@RequestBody List 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 updateReleaseFinish(@RequestParam(value = "id") String id,
@RequestParam(value = "status") Integer status) {
TDeliberationVote deliberationVote = deliberationVoteService.getById(id);
if(status == 2){
// 添加定时任务
Map maps =
new ImmutableMap.Builder().
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();
}
}