Java要自动生成工单,需要编写定时任务。
可新建一个类:task/ScheduledTasks。
代码示例如下:
package com.cmit.kapok.system.task; import com.cmit.kapok.constants.BusiConstants; import com.cmit.kapok.system.api.miit_area.IMiitAreaService; import com.cmit.kapok.system.api.miit_order.IMiitOrderService; import com.cmit.kapok.system.api.miit_task_commit.IMiitTaskCommitService; import com.cmit.kapok.system.entity.miit_task_commit.MiitTaskCommit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Map; @Component @EnableScheduling public class ScheduledTasks { private final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); @Autowired private IMiitOrderService miitOrderService; @Autowired private IMiitAreaService miitAreaService; @Autowired private IMiitTaskCommitService miitTaskCommitService; static DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy年MM月dd日"); @Scheduled(cron ="0 54 15 * * ?") private void createTodayCommitTask(){ logger.info("开始自动生成提交任务"); List<String> areaCodeList = miitOrderService.queryTodayNeedSubmitAreaCode(); if(null != areaCodeList && areaCodeList.size()>0){ Map<String,String> areaMap = miitAreaService.mapOfAreaCode(); List<MiitTaskCommit> list = new ArrayList<>(); //新增提交任务 for(String areaCode:areaCodeList){ if(!areaMap.containsKey(areaCode)){ logger.error("areaCode="+areaCode+"无法转义,请联系管理员完善配置"); continue; } String areaName = areaMap.get(areaCode); String taskName = dtf.format(LocalDate.now())+areaName+"任务单"; MiitTaskCommit task = new MiitTaskCommit(taskName, BusiConstants.TASK_COMMIT_STATUS_TODO,areaCode,areaName,"system"); list.add(task); } miitTaskCommitService.insertForeach(list); //更新工单的提交任务ID for(MiitTaskCommit task:list){ miitOrderService.updateTodayCommitOrder(task.getAreaCode(),task.getId()); } } logger.info("提交任务生成结束"); } }
cron表达式格式:
{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
“0 54 15 * * ?” 即每天15点54分0秒触发任务。
标签:task,Java,areaCode,org,import,编写,定时,com,cmit From: https://www.cnblogs.com/luoyihao/p/16869195.html