添加定时任务

main 16
hshansha 6 months ago
parent e50bf6cda6
commit 0b26acf5f7

@ -3,6 +3,7 @@ package com.ruoyi;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* *
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* @author ruoyi * @author ruoyi
*/ */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableScheduling
public class RuoYiApplication public class RuoYiApplication
{ {
public static void main(String[] args) public static void main(String[] args)

@ -1,14 +1,18 @@
package com.ruoyi.kaohe.controller; package com.ruoyi.kaohe.controller;
import java.util.Date;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.ruoyi.kaohe.domain.KhPcEmp; import com.ruoyi.kaohe.domain.KhPcEmp;
import com.ruoyi.kaohe.domain.KhPingce;
import com.ruoyi.kaohe.domain.KhVoteEmp; import com.ruoyi.kaohe.domain.KhVoteEmp;
import com.ruoyi.kaohe.service.IKhPcEmpService; import com.ruoyi.kaohe.service.IKhPcEmpService;
import com.ruoyi.kaohe.service.IKhVoteEmpService; import com.ruoyi.kaohe.service.IKhVoteEmpService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -35,8 +39,7 @@ import com.ruoyi.common.core.page.TableDataInfo;
*/ */
@RestController @RestController
@RequestMapping("/kaohe/vote") @RequestMapping("/kaohe/vote")
public class KhVoteController extends BaseController public class KhVoteController extends BaseController {
{
@Autowired @Autowired
private IKhVoteService khVoteService; private IKhVoteService khVoteService;
@Autowired @Autowired
@ -44,24 +47,48 @@ public class KhVoteController extends BaseController
@Autowired @Autowired
private IKhVoteEmpService voteEmpService; private IKhVoteEmpService voteEmpService;
/*@Scheduled(cron = "0 * * * * ?") // 每分钟的第0秒执行一次 监测时间修改评分状态
public void performTask() {
System.out.println("进入定时任务-------------------------------------------------------------");
//获取所有未完成的评分任务
List<KhVote> list = khVoteService.selectUnfinishedVote();
for (KhVote vote : list) {
Date sTime = vote.getsTime();
Date eTime = vote.geteTime();
String state = vote.getState();
// 获取当前日期和时间
Date now = new Date();
// 根据当前时间与开始、结束时间关系 修改状态
if (state.equals("0") && (now.after(sTime) && now.before(eTime))) {
vote.setState("1");
khVoteService.updateKhVote(vote);
System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务: " + vote.getVoteTitle() + " 状态为进行中");
}
if (now.after(eTime)) {
vote.setState("2");
khVoteService.updateKhVote(vote);
System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务: " + vote.getVoteTitle() + " 状态为已完成");
}
}
}*/
/** /**
* *
*/ */
@PreAuthorize("@ss.hasPermi('kaohe:vote:list')") @PreAuthorize("@ss.hasPermi('kaohe:vote:list')")
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo list(KhVote khVote) public TableDataInfo list(KhVote khVote) {
{
startPage(); startPage();
List<KhVote> list = khVoteService.selectKhVoteList(khVote); List<KhVote> list = khVoteService.selectKhVoteList(khVote);
return getDataTable(list); return getDataTable(list);
} }
/** /**
* pingceIditemIdsstate * pingceIditemIdsstate
*/ */
@PreAuthorize("@ss.hasPermi('kaohe:vote:list')") @PreAuthorize("@ss.hasPermi('kaohe:vote:list')")
@GetMapping("/listByConditons") @GetMapping("/listByConditons")
public TableDataInfo getListByConditons(KhVote khVote) public TableDataInfo getListByConditons(KhVote khVote) {
{
startPage(); startPage();
List<KhVote> list = khVoteService.getListByPcIdAndItemIds(khVote); List<KhVote> list = khVoteService.getListByPcIdAndItemIds(khVote);
return getDataTable(list); return getDataTable(list);
@ -73,8 +100,7 @@ public class KhVoteController extends BaseController
@PreAuthorize("@ss.hasPermi('kaohe:vote:export')") @PreAuthorize("@ss.hasPermi('kaohe:vote:export')")
@Log(title = "投票评测", businessType = BusinessType.EXPORT) @Log(title = "投票评测", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
public void export(HttpServletResponse response, KhVote khVote) public void export(HttpServletResponse response, KhVote khVote) {
{
List<KhVote> list = khVoteService.selectKhVoteList(khVote); List<KhVote> list = khVoteService.selectKhVoteList(khVote);
ExcelUtil<KhVote> util = new ExcelUtil<KhVote>(KhVote.class); ExcelUtil<KhVote> util = new ExcelUtil<KhVote>(KhVote.class);
util.exportExcel(response, list, "投票评测数据"); util.exportExcel(response, list, "投票评测数据");
@ -85,8 +111,7 @@ public class KhVoteController extends BaseController
*/ */
@PreAuthorize("@ss.hasPermi('kaohe:vote:query')") @PreAuthorize("@ss.hasPermi('kaohe:vote:query')")
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id) public AjaxResult getInfo(@PathVariable("id") Long id) {
{
return success(khVoteService.selectKhVoteById(id)); return success(khVoteService.selectKhVoteById(id));
} }
@ -97,14 +122,13 @@ public class KhVoteController extends BaseController
@Log(title = "投票评测", businessType = BusinessType.INSERT) @Log(title = "投票评测", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
@Transactional @Transactional
public AjaxResult add(@RequestBody KhVote khVote) public AjaxResult add(@RequestBody KhVote khVote) {
{
khVoteService.insertKhVote(khVote); khVoteService.insertKhVote(khVote);
//根据评测id查询职工评测关联 职工 //根据评测id查询职工评测关联 职工
KhPcEmp query = new KhPcEmp(); KhPcEmp query = new KhPcEmp();
query.setPcId(khVote.getPingceId()); query.setPcId(khVote.getPingceId());
List<KhPcEmp> pcEmps = pcEmpService.selectKhPcEmpList(query); List<KhPcEmp> pcEmps = pcEmpService.selectKhPcEmpList(query);
for(KhPcEmp pcEmp:pcEmps){ for (KhPcEmp pcEmp : pcEmps) {
//新增投票选项关联表 //新增投票选项关联表
KhVoteEmp voteEmp = new KhVoteEmp(); KhVoteEmp voteEmp = new KhVoteEmp();
voteEmp.setBkhdxId(pcEmp.getBkhdxId()); voteEmp.setBkhdxId(pcEmp.getBkhdxId());
@ -123,8 +147,7 @@ public class KhVoteController extends BaseController
@PreAuthorize("@ss.hasPermi('kaohe:vote:edit')") @PreAuthorize("@ss.hasPermi('kaohe:vote:edit')")
@Log(title = "投票评测", businessType = BusinessType.UPDATE) @Log(title = "投票评测", businessType = BusinessType.UPDATE)
@PutMapping @PutMapping
public AjaxResult edit(@RequestBody KhVote khVote) public AjaxResult edit(@RequestBody KhVote khVote) {
{
return toAjax(khVoteService.updateKhVote(khVote)); return toAjax(khVoteService.updateKhVote(khVote));
} }
@ -134,8 +157,7 @@ public class KhVoteController extends BaseController
@PreAuthorize("@ss.hasPermi('kaohe:vote:remove')") @PreAuthorize("@ss.hasPermi('kaohe:vote:remove')")
@Log(title = "投票评测", businessType = BusinessType.DELETE) @Log(title = "投票评测", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}") @DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids) public AjaxResult remove(@PathVariable Long[] ids) {
{
return toAjax(khVoteService.deleteKhVoteByIds(ids)); return toAjax(khVoteService.deleteKhVoteByIds(ids));
} }
} }

@ -60,4 +60,6 @@ public interface KhVoteMapper
public int deleteKhVoteByIds(Long[] ids); public int deleteKhVoteByIds(Long[] ids);
List<KhVote> getListByPcIdAndItemIds(KhVote khVote); List<KhVote> getListByPcIdAndItemIds(KhVote khVote);
List<KhVote> selectUnfinishedVote();
} }

@ -60,4 +60,6 @@ public interface IKhVoteService
public int deleteKhVoteById(Long id); public int deleteKhVoteById(Long id);
List<KhVote> getListByPcIdAndItemIds(KhVote khVote); List<KhVote> getListByPcIdAndItemIds(KhVote khVote);
List<KhVote> selectUnfinishedVote();
} }

@ -98,4 +98,9 @@ public class KhVoteServiceImpl implements IKhVoteService
public List<KhVote> getListByPcIdAndItemIds(KhVote khVote) { public List<KhVote> getListByPcIdAndItemIds(KhVote khVote) {
return khVoteMapper.getListByPcIdAndItemIds(khVote); return khVoteMapper.getListByPcIdAndItemIds(khVote);
} }
@Override
public List<KhVote> selectUnfinishedVote() {
return khVoteMapper.selectUnfinishedVote();
}
} }

@ -61,6 +61,10 @@
</if> </if>
</where> </where>
</select> </select>
<select id="selectUnfinishedVote" parameterType="KhVote" resultMap="KhVoteResult">
select v.*,it.type_id from kh_vote v left join kh_items it on v.khitem_id =it.id
where v.state !='2'
</select>
<select id="selectKhVoteById" parameterType="Long" resultMap="KhVoteResult"> <select id="selectKhVoteById" parameterType="Long" resultMap="KhVoteResult">
select v.*,it.type_id from kh_vote v left join kh_items it on v.khitem_id =it.id select v.*,it.type_id from kh_vote v left join kh_items it on v.khitem_id =it.id

@ -34,6 +34,10 @@
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId> <artifactId>ruoyi-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-kaohe</artifactId>
</dependency>
</dependencies> </dependencies>

@ -1,8 +1,14 @@
package com.ruoyi.quartz.task; package com.ruoyi.quartz.task;
import com.ruoyi.kaohe.domain.KhVote;
import com.ruoyi.kaohe.service.IKhVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import java.util.Date;
import java.util.List;
/** /**
* *
* *
@ -11,6 +17,33 @@ import com.ruoyi.common.utils.StringUtils;
@Component("ryTask") @Component("ryTask")
public class RyTask public class RyTask
{ {
@Autowired
private IKhVoteService khVoteService;
public void performTask() {
System.out.println("进入定时任务-------------------------------------------------------------");
//获取所有未完成的评分任务
List<KhVote> list = khVoteService.selectUnfinishedVote();
for (KhVote vote : list) {
Date sTime = vote.getsTime();
Date eTime = vote.geteTime();
String state = vote.getState();
// 获取当前日期和时间
Date now = new Date();
// 根据当前时间与开始、结束时间关系 修改状态
if (state.equals("0") && (now.after(sTime) && now.before(eTime))) {
vote.setState("1");
khVoteService.updateKhVote(vote);
System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务: " + vote.getVoteTitle() + " 状态为进行中");
}
if (now.after(eTime)) {
vote.setState("2");
khVoteService.updateKhVote(vote);
System.out.println("执行定时任务: " + System.currentTimeMillis() + " 修改评分任务: " + vote.getVoteTitle() + " 状态为已完成");
}
}
}
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
{ {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i)); System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));

Loading…
Cancel
Save