|
| 1 | +package com.codingapi.springboot.flow.service.impl; |
| 2 | + |
| 3 | +import com.codingapi.springboot.flow.bind.BindDataSnapshot; |
| 4 | +import com.codingapi.springboot.flow.domain.FlowWork; |
| 5 | +import com.codingapi.springboot.flow.em.FlowType; |
| 6 | +import com.codingapi.springboot.flow.event.FlowApprovalEvent; |
| 7 | +import com.codingapi.springboot.flow.record.FlowRecord; |
| 8 | +import com.codingapi.springboot.flow.repository.FlowBindDataRepository; |
| 9 | +import com.codingapi.springboot.flow.repository.FlowProcessRepository; |
| 10 | +import com.codingapi.springboot.flow.repository.FlowRecordRepository; |
| 11 | +import com.codingapi.springboot.flow.repository.FlowWorkRepository; |
| 12 | +import com.codingapi.springboot.flow.service.FlowServiceRepositoryHolder; |
| 13 | +import com.codingapi.springboot.flow.user.IFlowOperator; |
| 14 | +import com.codingapi.springboot.framework.event.EventPusher; |
| 15 | +import org.springframework.transaction.annotation.Transactional; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +@Transactional |
| 20 | +public class FlowNotifyService { |
| 21 | + |
| 22 | + private final String processId; |
| 23 | + private final IFlowOperator currentOperator; |
| 24 | + private final FlowRecordRepository flowRecordRepository; |
| 25 | + private final FlowBindDataRepository flowBindDataRepository; |
| 26 | + private final FlowWorkRepository flowWorkRepository; |
| 27 | + private final FlowProcessRepository flowProcessRepository; |
| 28 | + |
| 29 | + |
| 30 | + public FlowNotifyService(String processId, IFlowOperator currentOperator, FlowServiceRepositoryHolder flowServiceRepositoryHolder) { |
| 31 | + this.processId = processId; |
| 32 | + this.currentOperator = currentOperator; |
| 33 | + this.flowRecordRepository = flowServiceRepositoryHolder.getFlowRecordRepository(); |
| 34 | + this.flowBindDataRepository = flowServiceRepositoryHolder.getFlowBindDataRepository(); |
| 35 | + this.flowWorkRepository = flowServiceRepositoryHolder.getFlowWorkRepository(); |
| 36 | + this.flowProcessRepository = flowServiceRepositoryHolder.getFlowProcessRepository(); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * 获取流程设计对象 |
| 42 | + */ |
| 43 | + public FlowWork loadFlowWork(FlowRecord flowRecord) { |
| 44 | + FlowWork flowWork = flowProcessRepository.getFlowWorkByProcessId(flowRecord.getProcessId()); |
| 45 | + if (flowWork == null) { |
| 46 | + flowWork = flowWorkRepository.getFlowWorkByCode(flowRecord.getWorkCode()); |
| 47 | + } |
| 48 | + if (flowWork == null) { |
| 49 | + throw new IllegalArgumentException("flow work not found"); |
| 50 | + } |
| 51 | + flowWork.enableValidate(); |
| 52 | + |
| 53 | + return flowWork; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * 流程通知 |
| 58 | + */ |
| 59 | + public void notifyFlow() { |
| 60 | + List<FlowRecord> flowRecords = flowRecordRepository.findFlowRecordByProcessId(processId); |
| 61 | + List<FlowRecord> waitingRecords = flowRecords.stream().filter(FlowRecord::isWaiting).toList(); |
| 62 | + for (FlowRecord flowRecord : waitingRecords) { |
| 63 | + if (flowRecord.isOperator(currentOperator)) { |
| 64 | + flowRecord.setFlowType(FlowType.TODO); |
| 65 | + flowRecordRepository.update(flowRecord); |
| 66 | + |
| 67 | + BindDataSnapshot snapshot = flowBindDataRepository.getBindDataSnapshotById(flowRecord.getSnapshotId()); |
| 68 | + |
| 69 | + FlowWork flowWork = this.loadFlowWork(flowRecord); |
| 70 | + |
| 71 | + EventPusher.push(new FlowApprovalEvent(FlowApprovalEvent.STATE_TODO, |
| 72 | + flowRecord, |
| 73 | + flowRecord.getCurrentOperator(), |
| 74 | + flowWork, |
| 75 | + snapshot.toBindData() |
| 76 | + ), true); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments