上节课我们封装好了异步线程任务类,通过这个类,可以调用工作流项目,然后开启请假审批工作流实例。这节课我们写程序,除了向tb_leave
表添加请假记录,还要用异步线程技术调用工作流项目。
在TbLeaveDao.xml
文件中,定义SQL语句。
<select id="searchContradiction" parameterType="HashMap" resultType="long"> SELECT COUNT(*) FROM tb_leave WHERE user_id=#{userId} AND `status`!=2 AND ((#{start} BETWEEN `start` AND `end`) OR (#{end} BETWEEN `start` AND `end`)) </select> <insert id="insert" parameterType="com.example.emos.api.db.pojo.TbLeave" useGeneratedKeys="true" keyProperty="id"> INSERT INTO tb_leave SET user_id = #{userId}, reason = #{reason}, `start` = #{start}, `end` = #{end}, days = #{days}, `type` = #{type} </insert>
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
在TbLeaveDao.java
接口中,声明DAO方法。
public interface TbLeaveDao { …… public long searchContradiction(HashMap param); public int insert(TbLeave leave); }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
在LeaveService.java
接口中,定义抽象方法。
public interface LeaveService { …… public boolean searchContradiction(HashMap param); public int insert(TbLeave leave); }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
在LeaveServiceImpl.java
类中,实现抽象方法。
public class LeaveServiceImpl implements LeaveService { @Autowired private LeaveWorkflowTask leaveWorkflowTask; …… @Override public boolean searchContradiction(HashMap param) { long count = leaveDao.searchContradiction(param); boolean bool = count > 0; return bool; } @Override public int insert(TbLeave leave) { int rows = leaveDao.insert(leave); //开启工作流 if (rows == 1) { leaveWorkflowTask.startLeaveWorkflow(leave.getId(), leave.getUserId(), leave.getDays()); } else { throw new EmosException("会议添加失败"); } return rows; } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
创建InsertLeaveForm.java
类,封装Ajax提交的数据。
@Data @Schema(description = "添加请假记录表单") public class InsertLeaveForm { @NotBlank(message = "reason不能为空") @Length(max = 200,message = "reason不能超过200字符") private String reason; @NotBlank(message = "start不能为空") @Schema(description = "起始时间") private String start; @NotBlank(message = "end不能为空") @Schema(description = "结束时间") private String end; @NotNull(message = "type不能为空") @Range(min = 1, max = 2, message = "type内容不正确") @Schema(description = "请假类型") private Byte type; }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
在LeaveController.java
类中,声明Web方法。
public class LeaveController { …… @PostMapping("/insert") @Operation(summary = "添加请假记录") @SaCheckLogin public R insert(@Valid @RequestBody InsertLeaveForm form) { //验证结束时间是不是早于开始时间 DateTime date_1 = DateUtil.parse(form.getStart()); DateTime date_2 = DateUtil.parse(form.getEnd()); if (date_1.isAfterOrEquals(date_2)) { return R.error("请假开始时间不能晚于或者等于截止时间"); } //判断当前请假是否跟其他请假有交集 HashMap param = new HashMap() {{ put("userId", StpUtil.getLoginIdAsInt()); put("start", form.getStart()); put("end", form.getEnd()); }}; if (leaveService.searchContradiction(param)) { return R.error("当前请假申请与已有请假申请日期上有交集覆盖"); } //计算请假天数 long hours = date_1.between(date_2, DateUnit.HOUR); String days = new BigDecimal(hours).divide(new BigDecimal(24), 1, RoundingMode.CEILING).toString(); if (days.contains(".0")) { days = days.replace(".0", ""); } if (days.equals("0")) { days = "0.1"; } TbLeave leave = JSONUtil.parse(form).toBean(TbLeave.class); leave.setUserId(StpUtil.getLoginIdAsInt()); leave.setDays(days); int rows = leaveService.insert(leave); return R.ok().put("rows", rows); } }
代码块预览 复制
- 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