dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.service.oa;
H 2
3 import cn.hutool.core.date.LocalDateTimeUtil;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.common.util.object.BeanUtils;
6 import com.iailab.module.bpm.api.task.BpmProcessInstanceApi;
7 import com.iailab.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
8 import com.iailab.module.bpm.controller.admin.oa.vo.BpmOALeaveCreateReqVO;
9 import com.iailab.module.bpm.controller.admin.oa.vo.BpmOALeavePageReqVO;
10 import com.iailab.module.bpm.dal.dataobject.oa.BpmOALeaveDO;
11 import com.iailab.module.bpm.dal.mysql.oa.BpmOALeaveMapper;
12 import com.iailab.module.bpm.enums.task.BpmTaskStatusEnum;
13 import org.springframework.stereotype.Service;
14 import org.springframework.transaction.annotation.Transactional;
15 import org.springframework.validation.annotation.Validated;
16
17 import javax.annotation.Resource;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
22 import static com.iailab.module.bpm.enums.ErrorCodeConstants.OA_LEAVE_NOT_EXISTS;
23
24 /**
25  * OA 请假申请 Service 实现类
26  *
27  * @author jason
28  * @author iailab
29  */
30 @Service
31 @Validated
32 public class BpmOALeaveServiceImpl implements BpmOALeaveService {
33
34     /**
35      * OA 请假对应的流程定义 KEY
36      */
37     public static final String PROCESS_KEY = "oa_leave";
38
39     @Resource
40     private BpmOALeaveMapper leaveMapper;
41
42     @Resource
43     private BpmProcessInstanceApi processInstanceApi;
44
45     @Override
46     @Transactional(rollbackFor = Exception.class)
47     public Long createLeave(Long userId, BpmOALeaveCreateReqVO createReqVO) {
48         // 插入 OA 请假单
49         long day = LocalDateTimeUtil.between(createReqVO.getStartTime(), createReqVO.getEndTime()).toDays();
50         BpmOALeaveDO leave = BeanUtils.toBean(createReqVO, BpmOALeaveDO.class)
51                 .setUserId(userId).setDay(day).setStatus(BpmTaskStatusEnum.RUNNING.getStatus());
52         leaveMapper.insert(leave);
53
54         // 发起 BPM 流程
55         Map<String, Object> processInstanceVariables = new HashMap<>();
56         processInstanceVariables.put("day", day);
57         String processInstanceId = processInstanceApi.createProcessInstance(userId,
58                 new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
59                         .setVariables(processInstanceVariables).setBusinessKey(String.valueOf(leave.getId()))
60                         .setStartUserSelectAssignees(createReqVO.getStartUserSelectAssignees())).getCheckedData();
61
62         // 将工作流的编号,更新到 OA 请假单中
63         leaveMapper.updateById(new BpmOALeaveDO().setId(leave.getId()).setProcessInstanceId(processInstanceId));
64         return leave.getId();
65     }
66
67     @Override
68     public void updateLeaveStatus(Long id, Integer status) {
69         validateLeaveExists(id);
70         leaveMapper.updateById(new BpmOALeaveDO().setId(id).setStatus(status));
71     }
72
73     private void validateLeaveExists(Long id) {
74         if (leaveMapper.selectById(id) == null) {
75             throw exception(OA_LEAVE_NOT_EXISTS);
76         }
77     }
78
79     @Override
80     public BpmOALeaveDO getLeave(Long id) {
81         return leaveMapper.selectById(id);
82     }
83
84     @Override
85     public PageResult<BpmOALeaveDO> getLeavePage(Long userId, BpmOALeavePageReqVO pageReqVO) {
86         return leaveMapper.selectPage(userId, pageReqVO);
87     }
88
89 }