刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package jnpf.dmsService;
 
import jnpf.base.UserInfo;
import jnpf.dmsEntity.fenfa.DmsWenjianDayinRenwuEntity;
import jnpf.dmsEntity.print.DmsWenjianBufaPrintSource;
import jnpf.dmsEntity.wenjianbianhao.DmsFlowEventType;
import jnpf.dmsEntity.wenjianbianhao.form.DmsFlowEventForm;
import jnpf.dmsMapper.DmsWenjianBufaPrintMapper;
import jnpf.dmsMapper.DmsWenjianFenfaMapper;
import jnpf.dmsPermission.DmsCurrentIdentity;
import jnpf.exception.DataException;
import jnpf.flowable.WorkFlowApi;
import jnpf.flowable.entity.TaskEntity;
import jnpf.flowable.enums.TaskStatusEnum;
import jnpf.util.RandomUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/** 补发申请的流程结束副作用;原生表单继续负责主子表持久化。 */
@Service
@RequiredArgsConstructor
public class DmsWenjianBufaPrintService {
    private final DmsCurrentIdentity identity;
    private final WorkFlowApi workflow;
    private final DmsWenjianBufaPrintMapper sourceMapper;
    private final DmsWenjianFenfaMapper taskMapper;
    private final DmsWenjianDayinHaomaService numbers;
 
    @Transactional(rollbackFor = Exception.class)
    public int onFlowEnd(DmsFlowEventForm form) {
        if (form == null || form.getEventType() != DmsFlowEventType.END) {
            throw new DataException("补发打印任务仅允许流程结束事件触发");
        }
        String flowId = required(form.getFlowId(), "流程ID");
        String taskId = required(form.getTaskId(), "流程任务ID");
        UserInfo actor = identity.requireUser();
        String tenantId = identity.tenantId(actor);
        TaskEntity flow = workflow.getInfoSubmit(taskId);
        if (flow == null || !taskId.equals(flow.getId())
                || (!flowId.equals(flow.getFlowId()) && !flowId.equals(flow.getTemplateId()))) {
            throw new DataException("未找到匹配的补发流程任务");
        }
        // END may also be emitted for rejected flows. Never treat the event alone as approval.
        if (!Integer.valueOf(TaskStatusEnum.PASSED.getCode()).equals(flow.getStatus())) {
            return 0;
        }
        if (flow.getEndTime() == null) throw new DataException("补发流程尚未结束");
        List<DmsWenjianBufaPrintSource> requests = sourceMapper.lockRequests(tenantId, taskId);
        if (requests.size() != 1) throw new DataException("流程必须对应当前租户唯一补发申请");
        DmsWenjianBufaPrintSource request = requests.get(0);
        required(request.getFlowId(), "补发申请流程ID");
        if ((!java.util.Objects.equals(flow.getFlowId(), request.getFlowId())
                && !java.util.Objects.equals(flow.getTemplateId(), request.getFlowId()))
                || !Integer.valueOf(TaskStatusEnum.PASSED.getCode()).equals(request.getFlowState())) {
            throw new DataException("补发申请流程或审批状态不匹配");
        }
        List<DmsWenjianBufaPrintSource.Detail> details = sourceMapper.selectDetails(tenantId, request.getId());
        if (details.isEmpty()) throw new DataException("补发申请明细不能为空");
        List<DmsWenjianDayinRenwuEntity> tasks = new ArrayList<>();
        Date now = new Date();
        for (DmsWenjianBufaPrintSource.Detail detail : details) {
            tasks.add(buildTask(tenantId, request, detail, flow.getEndTime(), now));
        }
        tasks.sort(java.util.Comparator.comparing(DmsWenjianDayinRenwuEntity::getWenjianBianhao).thenComparing(DmsWenjianDayinRenwuEntity::getBanbenhao).thenComparing(DmsWenjianDayinRenwuEntity::getId));
        int created = 0;
        for (DmsWenjianDayinRenwuEntity task : tasks) {
            int inserted = taskMapper.insertPrintTaskIfAbsent(task);
            if (inserted == 1) numbers.ensureTaskNumbers(tenantId, task, 0);
            created += inserted;
        }
        return created;
    }
 
    private DmsWenjianDayinRenwuEntity buildTask(String tenantId, DmsWenjianBufaPrintSource request,
            DmsWenjianBufaPrintSource.Detail detail, Date approvedAt, Date now) {
        if (detail.getResolvedVersionId() == null
                || !detail.getResolvedVersionId().equals(detail.getVersionId())) {
            throw new DataException("补发明细的文件或版本不存在、不匹配或已删除:" + detail.getId());
        }
        DmsWenjianDayinRenwuEntity task = new DmsWenjianDayinRenwuEntity();
        task.setId(RandomUtil.uuId());
        task.setTenantId(tenantId);
        task.setLaiyuanLeixing("wenjian_bufa");
        task.setLaiyuanDanjuId(request.getId());
        task.setLaiyuanMingxiId(required(detail.getId(), "补发明细ID"));
        task.setWenjianId(required(detail.getFileId(), "文件ID"));
        task.setWenjianBianhao(required(detail.getFileNumber(), "文件编号"));
        task.setWenjianMingcheng(required(detail.getFileName(), "文件名称"));
        task.setBanbenhao(required(detail.getVersionLabel(), "版本号"));
        task.setJieshouBumenId(required(request.getDepartmentId(), "补发申请部门"));
        task.setDayinFenshu(copies(detail.getCopies()));
        task.setCreatorUserId(required(request.getApplicantId(), "补发申请人"));
        task.setCreatorTime(now);
        task.setShenheWanchengRiqi(approvedAt);
        task.setShengxiaoRiqi(detail.getEffectiveDate());
        task.setShifouShoukong(required(detail.getControlled(), "文件受控标志"));
        task.setWenjianLeibie("[\"wenjian\"]");
        task.setBeizhuShuoming(detail.getPurpose() == null ? request.getRemark() : detail.getPurpose());
        task.setBizStatus("daidayin");
        return task;
    }
 
    private int copies(String value) {
        try {
            int copies = Integer.parseInt(required(value, "补发份数"));
            if (copies > 0) return copies;
        } catch (NumberFormatException ignored) {
            // Reject decimals, overflow and non-numeric form values.
        }
        throw new DataException("补发份数必须为正整数");
    }
 
    private String required(String value, String label) {
        if (value == null || value.trim().isEmpty()) throw new DataException(label + "不能为空");
        return value.trim();
    }
}