|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+package com.huimv.employment.service.contract;
|
|
|
2
|
+
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import com.huimv.employment.common.exception.BizException;
|
|
|
5
|
+import com.huimv.employment.common.exception.ErrorCode;
|
|
|
6
|
+import com.huimv.employment.dao.entity.FeContract;
|
|
|
7
|
+import com.huimv.employment.dao.entity.FeEmploymentOrder;
|
|
|
8
|
+import com.huimv.employment.dao.entity.FeEnterprise;
|
|
|
9
|
+import com.huimv.employment.dao.entity.FeServicePlan;
|
|
|
10
|
+import com.huimv.employment.dao.entity.FeWorkerRegistration;
|
|
|
11
|
+import com.huimv.employment.dao.mapper.FeContractMapper;
|
|
|
12
|
+import com.huimv.employment.dao.mapper.FeEmploymentOrderMapper;
|
|
|
13
|
+import com.huimv.employment.dao.mapper.FeServicePlanMapper;
|
|
|
14
|
+import com.huimv.employment.dao.mapper.FeWorkerRegistrationMapper;
|
|
|
15
|
+import com.huimv.employment.service.contract.dto.ContractSummaryResponse;
|
|
|
16
|
+import com.huimv.employment.service.enterprise.EnterpriseService;
|
|
|
17
|
+import org.slf4j.Logger;
|
|
|
18
|
+import org.slf4j.LoggerFactory;
|
|
|
19
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
20
|
+import org.springframework.stereotype.Service;
|
|
|
21
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
22
|
+import org.springframework.util.StringUtils;
|
|
|
23
|
+
|
|
|
24
|
+import java.time.LocalDateTime;
|
|
|
25
|
+import java.time.format.DateTimeFormatter;
|
|
|
26
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
27
|
+
|
|
|
28
|
+/**
|
|
|
29
|
+ * 电子合同:审核通过后生成待签合同;支持幂等与重新生成。
|
|
|
30
|
+ */
|
|
|
31
|
+@Service
|
|
|
32
|
+public class ContractService {
|
|
|
33
|
+
|
|
|
34
|
+ private static final Logger log = LoggerFactory.getLogger(ContractService.class);
|
|
|
35
|
+
|
|
|
36
|
+ private static final String SIGN_PENDING = "pending";
|
|
|
37
|
+ private static final String SIGN_SIGNING = "signing";
|
|
|
38
|
+ private static final String SIGN_SIGNED = "signed";
|
|
|
39
|
+ private static final String SIGN_VOIDED = "voided";
|
|
|
40
|
+ private static final String PROVIDER_MOCK = "mock";
|
|
|
41
|
+ private static final String REG_CONTRACT_SIGNING = "contract_signing";
|
|
|
42
|
+ private static final String REG_COMPLETED = "completed";
|
|
|
43
|
+
|
|
|
44
|
+ private static final DateTimeFormatter CONTRACT_NO_DAY = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
45
|
+
|
|
|
46
|
+ private final FeContractMapper feContractMapper;
|
|
|
47
|
+ private final FeEmploymentOrderMapper feEmploymentOrderMapper;
|
|
|
48
|
+ private final FeWorkerRegistrationMapper feWorkerRegistrationMapper;
|
|
|
49
|
+ private final FeServicePlanMapper feServicePlanMapper;
|
|
|
50
|
+ private final EnterpriseService enterpriseService;
|
|
|
51
|
+ private final ESignClient eSignClient;
|
|
|
52
|
+
|
|
|
53
|
+ public ContractService(FeContractMapper feContractMapper,
|
|
|
54
|
+ FeEmploymentOrderMapper feEmploymentOrderMapper,
|
|
|
55
|
+ FeWorkerRegistrationMapper feWorkerRegistrationMapper,
|
|
|
56
|
+ FeServicePlanMapper feServicePlanMapper,
|
|
|
57
|
+ EnterpriseService enterpriseService,
|
|
|
58
|
+ ESignClient eSignClient) {
|
|
|
59
|
+ this.feContractMapper = feContractMapper;
|
|
|
60
|
+ this.feEmploymentOrderMapper = feEmploymentOrderMapper;
|
|
|
61
|
+ this.feWorkerRegistrationMapper = feWorkerRegistrationMapper;
|
|
|
62
|
+ this.feServicePlanMapper = feServicePlanMapper;
|
|
|
63
|
+ this.enterpriseService = enterpriseService;
|
|
|
64
|
+ this.eSignClient = eSignClient;
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ /**
|
|
|
68
|
+ * 审核通过后创建或返回已有待签合同(同一 registration_id 幂等)。
|
|
|
69
|
+ * <p>电子签调用失败时仍保留 pending 合同记录,便于后续 {@link #regeneratePendingContract}。</p>
|
|
|
70
|
+ */
|
|
|
71
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
72
|
+ public ContractSummaryResponse createOrGetPendingForRegistration(FeWorkerRegistration reg, FeEmploymentOrder order) {
|
|
|
73
|
+ if (reg == null || reg.getId() == null) {
|
|
|
74
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "登记记录无效");
|
|
|
75
|
+ }
|
|
|
76
|
+ if (order == null || order.getId() == null) {
|
|
|
77
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "订单无效");
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ FeContract existing = findActiveByRegistrationId(reg.getId());
|
|
|
81
|
+ if (existing != null) {
|
|
|
82
|
+ if (needsESignRetry(existing)) {
|
|
|
83
|
+ tryFillESign(existing, reg);
|
|
|
84
|
+ existing = feContractMapper.selectById(existing.getId());
|
|
|
85
|
+ }
|
|
|
86
|
+ return toSummary(existing, false, "已存在待签合同");
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
90
|
+ String contractType = resolveContractType(order);
|
|
|
91
|
+ String title = buildContractTitle(contractType, reg.getRealName(), order.getTitle());
|
|
|
92
|
+ String contractNo = nextContractNo();
|
|
|
93
|
+
|
|
|
94
|
+ FeContract contract = new FeContract();
|
|
|
95
|
+ contract.setContractNo(contractNo);
|
|
|
96
|
+ contract.setOrderId(order.getId());
|
|
|
97
|
+ contract.setRegistrationId(reg.getId());
|
|
|
98
|
+ contract.setWorkerId(reg.getWorkerId());
|
|
|
99
|
+ contract.setEnterpriseId(order.getEnterpriseId() != null ? order.getEnterpriseId() : reg.getEnterpriseId());
|
|
|
100
|
+ contract.setContractType(contractType);
|
|
|
101
|
+ contract.setContractTitle(title);
|
|
|
102
|
+ contract.setSignStatus(SIGN_PENDING);
|
|
|
103
|
+ contract.setSignProvider(PROVIDER_MOCK);
|
|
|
104
|
+ contract.setCreateTime(now);
|
|
|
105
|
+ contract.setUpdateTime(now);
|
|
|
106
|
+ contract.setDelFlag(0);
|
|
|
107
|
+
|
|
|
108
|
+ try {
|
|
|
109
|
+ feContractMapper.insert(contract);
|
|
|
110
|
+ } catch (DuplicateKeyException ex) {
|
|
|
111
|
+ FeContract raced = findActiveByRegistrationId(reg.getId());
|
|
|
112
|
+ if (raced != null) {
|
|
|
113
|
+ return toSummary(raced, false, "已存在待签合同");
|
|
|
114
|
+ }
|
|
|
115
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "合同编号冲突,请重试");
|
|
|
116
|
+ }
|
|
|
117
|
+
|
|
|
118
|
+ try {
|
|
|
119
|
+ tryFillESign(contract, reg);
|
|
|
120
|
+ } catch (Exception ex) {
|
|
|
121
|
+ log.error("登记 registrationId={} 电子签创建失败,合同已落库待重试: {}", reg.getId(), ex.getMessage());
|
|
|
122
|
+ FeContract saved = feContractMapper.selectById(contract.getId());
|
|
|
123
|
+ ContractSummaryResponse response = toSummary(saved, true, "待签合同已创建,电子签通道暂不可用,请稍后重新生成");
|
|
|
124
|
+ return response;
|
|
|
125
|
+ }
|
|
|
126
|
+
|
|
|
127
|
+ FeContract saved = feContractMapper.selectById(contract.getId());
|
|
|
128
|
+ return toSummary(saved, true, "待签合同已生成");
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ /**
|
|
|
132
|
+ * 重新生成签署链接(电子签失败或链接失效时)。
|
|
|
133
|
+ * <p>仅允许 pending/signing;已签署不可重生成。无合同记录时按登记补建。</p>
|
|
|
134
|
+ */
|
|
|
135
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
136
|
+ public ContractSummaryResponse regeneratePendingContract(Long userId, Long registrationId) {
|
|
|
137
|
+ enterpriseService.requireEnterpriseUser(userId);
|
|
|
138
|
+ FeEnterprise enterprise = enterpriseService.findEnterpriseByUserId(userId);
|
|
|
139
|
+ if (enterprise == null) {
|
|
|
140
|
+ throw new BizException(ErrorCode.NOT_FOUND, "企业信息不存在");
|
|
|
141
|
+ }
|
|
|
142
|
+ if (registrationId == null) {
|
|
|
143
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "registration_id 不能为空");
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ FeWorkerRegistration reg = feWorkerRegistrationMapper.selectById(registrationId);
|
|
|
147
|
+ if (reg == null) {
|
|
|
148
|
+ throw new BizException(ErrorCode.NOT_FOUND, "登记记录不存在");
|
|
|
149
|
+ }
|
|
|
150
|
+ if (reg.getEnterpriseId() == null || !reg.getEnterpriseId().equals(enterprise.getId())) {
|
|
|
151
|
+ throw new BizException(ErrorCode.FORBIDDEN, "无权操作该登记合同");
|
|
|
152
|
+ }
|
|
|
153
|
+ if (!REG_CONTRACT_SIGNING.equals(reg.getRegStatus()) && !REG_COMPLETED.equals(reg.getRegStatus())) {
|
|
|
154
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "仅审核通过待签约的登记可重新生成合同");
|
|
|
155
|
+ }
|
|
|
156
|
+
|
|
|
157
|
+ FeEmploymentOrder order = feEmploymentOrderMapper.selectById(reg.getOrderId());
|
|
|
158
|
+ if (order == null) {
|
|
|
159
|
+ throw new BizException(ErrorCode.NOT_FOUND, "用工订单不存在");
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ FeContract contract = findActiveByRegistrationId(registrationId);
|
|
|
163
|
+ if (contract == null) {
|
|
|
164
|
+ return createOrGetPendingForRegistration(reg, order);
|
|
|
165
|
+ }
|
|
|
166
|
+ if (SIGN_SIGNED.equals(contract.getSignStatus())) {
|
|
|
167
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "合同已签署,无法重新生成");
|
|
|
168
|
+ }
|
|
|
169
|
+ if (SIGN_VOIDED.equals(contract.getSignStatus())) {
|
|
|
170
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "合同已作废,无法重新生成");
|
|
|
171
|
+ }
|
|
|
172
|
+ if (!SIGN_PENDING.equals(contract.getSignStatus()) && !SIGN_SIGNING.equals(contract.getSignStatus())) {
|
|
|
173
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "当前签署状态不可重新生成:" + contract.getSignStatus());
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ tryFillESign(contract, reg);
|
|
|
177
|
+ FeContract saved = feContractMapper.selectById(contract.getId());
|
|
|
178
|
+ return toSummary(saved, false, "签署链接已重新生成");
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ public FeContract findActiveByRegistrationId(Long registrationId) {
|
|
|
182
|
+ if (registrationId == null) {
|
|
|
183
|
+ return null;
|
|
|
184
|
+ }
|
|
|
185
|
+ return feContractMapper.selectOne(new LambdaQueryWrapper<FeContract>()
|
|
|
186
|
+ .eq(FeContract::getRegistrationId, registrationId)
|
|
|
187
|
+ .ne(FeContract::getSignStatus, SIGN_VOIDED)
|
|
|
188
|
+ .orderByDesc(FeContract::getId)
|
|
|
189
|
+ .last("LIMIT 1"));
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ private void tryFillESign(FeContract contract, FeWorkerRegistration reg) {
|
|
|
193
|
+ ESignClient.ESignCreateResult result = eSignClient.createPendingContract(
|
|
|
194
|
+ contract.getContractNo(),
|
|
|
195
|
+ contract.getContractType(),
|
|
|
196
|
+ contract.getContractTitle(),
|
|
|
197
|
+ reg != null ? reg.getRealName() : null);
|
|
|
198
|
+ FeContract update = new FeContract();
|
|
|
199
|
+ update.setId(contract.getId());
|
|
|
200
|
+ update.setExternalContractId(result.getExternalContractId());
|
|
|
201
|
+ update.setSignUrl(result.getSignUrl());
|
|
|
202
|
+ update.setSignProvider(PROVIDER_MOCK);
|
|
|
203
|
+ update.setSignStatus(SIGN_PENDING);
|
|
|
204
|
+ update.setUpdateTime(LocalDateTime.now());
|
|
|
205
|
+ feContractMapper.updateById(update);
|
|
|
206
|
+ contract.setExternalContractId(result.getExternalContractId());
|
|
|
207
|
+ contract.setSignUrl(result.getSignUrl());
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ private static boolean needsESignRetry(FeContract contract) {
|
|
|
211
|
+ if (contract == null) {
|
|
|
212
|
+ return false;
|
|
|
213
|
+ }
|
|
|
214
|
+ if (SIGN_SIGNED.equals(contract.getSignStatus()) || SIGN_VOIDED.equals(contract.getSignStatus())) {
|
|
|
215
|
+ return false;
|
|
|
216
|
+ }
|
|
|
217
|
+ return !StringUtils.hasText(contract.getSignUrl()) || !StringUtils.hasText(contract.getExternalContractId());
|
|
|
218
|
+ }
|
|
|
219
|
+
|
|
|
220
|
+ /**
|
|
|
221
|
+ * 按订单所选方案类型选择合同模板编码。
|
|
|
222
|
+ */
|
|
|
223
|
+ private String resolveContractType(FeEmploymentOrder order) {
|
|
|
224
|
+ if (order.getSelectedPlanId() == null) {
|
|
|
225
|
+ return "labor";
|
|
|
226
|
+ }
|
|
|
227
|
+ FeServicePlan plan = feServicePlanMapper.selectById(order.getSelectedPlanId());
|
|
|
228
|
+ if (plan == null || !StringUtils.hasText(plan.getPlanType())) {
|
|
|
229
|
+ return "labor";
|
|
|
230
|
+ }
|
|
|
231
|
+ String planType = plan.getPlanType().trim().toLowerCase();
|
|
|
232
|
+ if ("flexible".equals(planType)) {
|
|
|
233
|
+ return "labor_flexible";
|
|
|
234
|
+ }
|
|
|
235
|
+ if ("traditional".equals(planType)) {
|
|
|
236
|
+ return "labor_dispatch";
|
|
|
237
|
+ }
|
|
|
238
|
+ if ("hybrid".equals(planType)) {
|
|
|
239
|
+ return "labor_hybrid";
|
|
|
240
|
+ }
|
|
|
241
|
+ return "labor";
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ private static String buildContractTitle(String contractType, String workerName, String orderTitle) {
|
|
|
245
|
+ String typeLabel;
|
|
|
246
|
+ if ("labor_flexible".equals(contractType)) {
|
|
|
247
|
+ typeLabel = "灵活用工协议";
|
|
|
248
|
+ } else if ("labor_dispatch".equals(contractType)) {
|
|
|
249
|
+ typeLabel = "劳务派遣协议";
|
|
|
250
|
+ } else if ("labor_hybrid".equals(contractType)) {
|
|
|
251
|
+ typeLabel = "混合用工协议";
|
|
|
252
|
+ } else {
|
|
|
253
|
+ typeLabel = "劳动合同";
|
|
|
254
|
+ }
|
|
|
255
|
+ String name = StringUtils.hasText(workerName) ? workerName.trim() : "临时工";
|
|
|
256
|
+ if (StringUtils.hasText(orderTitle)) {
|
|
|
257
|
+ return typeLabel + "-" + name + "-" + orderTitle.trim();
|
|
|
258
|
+ }
|
|
|
259
|
+ return typeLabel + "-" + name;
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
|
262
|
+ private String nextContractNo() {
|
|
|
263
|
+ String day = LocalDateTime.now().format(CONTRACT_NO_DAY);
|
|
|
264
|
+ int rand = ThreadLocalRandom.current().nextInt(1000, 10000);
|
|
|
265
|
+ return "CT" + day + rand;
|
|
|
266
|
+ }
|
|
|
267
|
+
|
|
|
268
|
+ private static ContractSummaryResponse toSummary(FeContract contract, boolean created, String message) {
|
|
|
269
|
+ ContractSummaryResponse response = new ContractSummaryResponse();
|
|
|
270
|
+ if (contract != null) {
|
|
|
271
|
+ response.setContractId(contract.getId());
|
|
|
272
|
+ response.setContractNo(contract.getContractNo());
|
|
|
273
|
+ response.setRegistrationId(contract.getRegistrationId());
|
|
|
274
|
+ response.setContractType(contract.getContractType());
|
|
|
275
|
+ response.setContractTitle(contract.getContractTitle());
|
|
|
276
|
+ response.setSignStatus(contract.getSignStatus());
|
|
|
277
|
+ response.setSignProvider(contract.getSignProvider());
|
|
|
278
|
+ response.setSignUrl(contract.getSignUrl());
|
|
|
279
|
+ response.setExternalContractId(contract.getExternalContractId());
|
|
|
280
|
+ }
|
|
|
281
|
+ response.setCreated(created);
|
|
|
282
|
+ response.setMessage(message);
|
|
|
283
|
+ return response;
|
|
|
284
|
+ }
|
|
|
285
|
+}
|