package com.iailab.module.bpm.service.oa;
|
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
import com.iailab.framework.common.pojo.PageResult;
|
import com.iailab.framework.common.util.object.BeanUtils;
|
import com.iailab.module.bpm.api.task.BpmProcessInstanceApi;
|
import com.iailab.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
|
import com.iailab.module.bpm.controller.admin.oa.vo.BpmOALeaveCreateReqVO;
|
import com.iailab.module.bpm.controller.admin.oa.vo.BpmOALeavePageReqVO;
|
import com.iailab.module.bpm.dal.dataobject.oa.BpmOALeaveDO;
|
import com.iailab.module.bpm.dal.mysql.oa.BpmOALeaveMapper;
|
import com.iailab.module.bpm.enums.task.BpmTaskStatusEnum;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import javax.annotation.Resource;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static com.iailab.module.bpm.enums.ErrorCodeConstants.OA_LEAVE_NOT_EXISTS;
|
|
/**
|
* OA 请假申请 Service 实现类
|
*
|
* @author jason
|
* @author iailab
|
*/
|
@Service
|
@Validated
|
public class BpmOALeaveServiceImpl implements BpmOALeaveService {
|
|
/**
|
* OA 请假对应的流程定义 KEY
|
*/
|
public static final String PROCESS_KEY = "oa_leave";
|
|
@Resource
|
private BpmOALeaveMapper leaveMapper;
|
|
@Resource
|
private BpmProcessInstanceApi processInstanceApi;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Long createLeave(Long userId, BpmOALeaveCreateReqVO createReqVO) {
|
// 插入 OA 请假单
|
long day = LocalDateTimeUtil.between(createReqVO.getStartTime(), createReqVO.getEndTime()).toDays();
|
BpmOALeaveDO leave = BeanUtils.toBean(createReqVO, BpmOALeaveDO.class)
|
.setUserId(userId).setDay(day).setStatus(BpmTaskStatusEnum.RUNNING.getStatus());
|
leaveMapper.insert(leave);
|
|
// 发起 BPM 流程
|
Map<String, Object> processInstanceVariables = new HashMap<>();
|
processInstanceVariables.put("day", day);
|
String processInstanceId = processInstanceApi.createProcessInstance(userId,
|
new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
|
.setVariables(processInstanceVariables).setBusinessKey(String.valueOf(leave.getId()))
|
.setStartUserSelectAssignees(createReqVO.getStartUserSelectAssignees())).getCheckedData();
|
|
// 将工作流的编号,更新到 OA 请假单中
|
leaveMapper.updateById(new BpmOALeaveDO().setId(leave.getId()).setProcessInstanceId(processInstanceId));
|
return leave.getId();
|
}
|
|
@Override
|
public void updateLeaveStatus(Long id, Integer status) {
|
validateLeaveExists(id);
|
leaveMapper.updateById(new BpmOALeaveDO().setId(id).setStatus(status));
|
}
|
|
private void validateLeaveExists(Long id) {
|
if (leaveMapper.selectById(id) == null) {
|
throw exception(OA_LEAVE_NOT_EXISTS);
|
}
|
}
|
|
@Override
|
public BpmOALeaveDO getLeave(Long id) {
|
return leaveMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<BpmOALeaveDO> getLeavePage(Long userId, BpmOALeavePageReqVO pageReqVO) {
|
return leaveMapper.selectPage(userId, pageReqVO);
|
}
|
|
}
|