全部开发者教程

企业级在线办公系统

上节课我们封装好了异步线程任务类,通过这个类,可以调用工作流项目,然后开启请假审批工作流实例。这节课我们写程序,除了向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>

TbLeaveDao.java接口中,声明DAO方法。

public interface TbLeaveDao {
    ……
    public long searchContradiction(HashMap param);
    public int insert(TbLeave leave);
}

二、编写业务层代码

LeaveService.java接口中,定义抽象方法。

public interface LeaveService {
    ……
    public boolean searchContradiction(HashMap param);
    public int insert(TbLeave leave);   
}

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;
    }
}

三、编写Web层代码

创建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;
}

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);
    }
}