springboot简单审批流实现代码
时间: 2023-03-27 08:03:49 浏览: 207
你好,以下是 Spring Boot 简单审批流实现的代码示例:
1. 定义审批流程节点
```
public enum ApprovalNode {
MANAGER, // 经理
DIRECTOR, // 总监
CEO // CEO
}
```
2. 定义审批申请实体类
```
@Data
public class ApprovalRequest {
private Long id; // 申请ID
private String applicant; // 申请人
private BigDecimal amount; // 申请金额
private ApprovalNode currentNode; // 当前审批节点
private String remark; // 备注
}
```
3. 定义审批记录实体类
```
@Data
public class ApprovalRecord {
private Long id; // 记录ID
private Long requestId; // 申请ID
private ApprovalNode node; // 审批节点
private String approver; // 审批人
private LocalDateTime approveTime; // 审批时间
private String remark; // 备注
}
```
4. 定义审批服务接口
```
public interface ApprovalService {
/**
* 提交审批申请
*
* @param request 申请信息
* @return 申请ID
*/
Long submitApprovalRequest(ApprovalRequest request);
/**
* 获取当前审批节点的审批记录
*
* @param requestId 申请ID
* @return 审批记录列表
*/
List<ApprovalRecord> getApprovalRecords(Long requestId);
/**
* 审批申请
*
* @param requestId 申请ID
* @param node 审批节点
* @param approver 审批人
* @param remark 备注
* @return true-审批通过,false-审批不通过
*/
boolean approve(Long requestId, ApprovalNode node, String approver, String remark);
}
```
5. 实现审批服务
```
@Service
public class ApprovalServiceImpl implements ApprovalService {
private final Map<Long, ApprovalRequest> requestMap = new ConcurrentHashMap<>();
private final Map<Long, List<ApprovalRecord>> recordMap = new ConcurrentHashMap<>();
@Override
public Long submitApprovalRequest(ApprovalRequest request) {
Long id = System.currentTimeMillis();
request.setId(id);
request.setCurrentNode(ApprovalNode.MANAGER);
requestMap.put(id, request);
recordMap.put(id, new ArrayList<>());
return id;
}
@Override
public List<ApprovalRecord> getApprovalRecords(Long requestId) {
return recordMap.get(requestId);
}
@Override
public boolean approve(Long requestId, ApprovalNode node, String approver, String remark) {
ApprovalRequest request = requestMap.get(requestId);
if (request == null) {
throw new IllegalArgumentException("Invalid request ID");
}
if (request.getCurrentNode() != node) {
throw new IllegalArgumentException("Invalid approval node");
}
ApprovalRecord record = new ApprovalRecord();
record.setId(System.currentTimeMillis());
record.setRequestId(requestId);
record.setNode(node);
record.setApprover(approver);
record.setApproveTime(LocalDateTime.now());
record.setRemark(remark);
recordMap.get(requestId).add(record);
if (node == ApprovalNode.MANAGER) {
request.setCurrentNode(ApprovalNode.DIRECTOR);
} else if (node == ApprovalNode.DIRECTOR) {
request.setCurrentNode(ApprovalNode.CEO);
} else {
return true;
}
return false;
}
}
```
以上就是 Spring Boot 简单审批流实现的代码示例,希望对你有帮助。
阅读全文