|
|
@@ -5,15 +5,22 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
5
|
5
|
import com.huimv.employment.common.exception.BizException;
|
|
6
|
6
|
import com.huimv.employment.common.exception.ErrorCode;
|
|
7
|
7
|
import com.huimv.employment.dao.entity.FeConversation;
|
|
|
8
|
+import com.huimv.employment.dao.entity.FeDraftCostSnapshot;
|
|
8
|
9
|
import com.huimv.employment.dao.entity.FeDraftField;
|
|
9
|
10
|
import com.huimv.employment.dao.entity.FeEmploymentDraft;
|
|
|
11
|
+import com.huimv.employment.dao.entity.FeEmploymentOrder;
|
|
10
|
12
|
import com.huimv.employment.dao.entity.FeEnterprise;
|
|
|
13
|
+import com.huimv.employment.dao.mapper.FeDraftCostSnapshotMapper;
|
|
11
|
14
|
import com.huimv.employment.dao.mapper.FeDraftFieldMapper;
|
|
12
|
15
|
import com.huimv.employment.dao.mapper.FeEmploymentDraftMapper;
|
|
|
16
|
+import com.huimv.employment.dao.mapper.FeEmploymentOrderMapper;
|
|
13
|
17
|
import com.huimv.employment.service.conversation.ConversationService;
|
|
|
18
|
+import com.huimv.employment.service.draft.dto.McpDraftCalculateResponse;
|
|
14
|
19
|
import com.huimv.employment.service.draft.dto.McpDraftGenerateRequest;
|
|
15
|
20
|
import com.huimv.employment.service.draft.dto.McpDraftGenerateResponse;
|
|
16
|
21
|
import com.huimv.employment.service.draft.dto.McpDraftInfoResponse;
|
|
|
22
|
+import com.huimv.employment.service.draft.dto.McpDraftSubmitRequest;
|
|
|
23
|
+import com.huimv.employment.service.draft.dto.McpDraftSubmitResponse;
|
|
17
|
24
|
import com.huimv.employment.service.draft.dto.McpDraftUpdateRequest;
|
|
18
|
25
|
import com.huimv.employment.service.draft.dto.McpDraftUpdateResponse;
|
|
19
|
26
|
import com.huimv.employment.service.enterprise.EnterpriseService;
|
|
|
@@ -47,6 +54,10 @@ public class McpDraftService {
|
|
47
|
54
|
private static final Logger log = LoggerFactory.getLogger(McpDraftService.class);
|
|
48
|
55
|
|
|
49
|
56
|
private static final String DB_STATUS_PENDING = "pending";
|
|
|
57
|
+ private static final String DB_STATUS_CONFIRMED = "confirmed";
|
|
|
58
|
+ private static final String ORDER_STATUS_CONFIRMED = "confirmed";
|
|
|
59
|
+ private static final String STEP_PLAN_CONFIRMED = "plan_confirmed";
|
|
|
60
|
+ private static final String SUBMIT_SUCCESS = "success";
|
|
50
|
61
|
private static final String MCP_STATUS_INCOMPLETE = "incomplete";
|
|
51
|
62
|
private static final String MCP_STATUS_READY = "ready";
|
|
52
|
63
|
private static final String MCP_STATUS_SUBMITTED = "submitted";
|
|
|
@@ -63,21 +74,33 @@ public class McpDraftService {
|
|
63
|
74
|
private static final Set<String> ALLOWED_SETTLEMENT_MODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
|
|
64
|
75
|
"daily", "piece", "monthly")));
|
|
65
|
76
|
|
|
|
77
|
+ private static final Set<String> ALLOWED_SCHEME_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
|
|
|
78
|
+ DraftCostCalculationEngine.SCHEME_A, DraftCostCalculationEngine.SCHEME_B)));
|
|
|
79
|
+
|
|
66
|
80
|
private final FeEmploymentDraftMapper feEmploymentDraftMapper;
|
|
67
|
81
|
private final FeDraftFieldMapper feDraftFieldMapper;
|
|
|
82
|
+ private final FeDraftCostSnapshotMapper feDraftCostSnapshotMapper;
|
|
|
83
|
+ private final FeEmploymentOrderMapper feEmploymentOrderMapper;
|
|
68
|
84
|
private final ConversationService conversationService;
|
|
69
|
85
|
private final EnterpriseService enterpriseService;
|
|
|
86
|
+ private final DraftCostCalculationEngine draftCostCalculationEngine;
|
|
70
|
87
|
private final ObjectMapper objectMapper;
|
|
71
|
88
|
|
|
72
|
89
|
public McpDraftService(FeEmploymentDraftMapper feEmploymentDraftMapper,
|
|
73
|
90
|
FeDraftFieldMapper feDraftFieldMapper,
|
|
|
91
|
+ FeDraftCostSnapshotMapper feDraftCostSnapshotMapper,
|
|
|
92
|
+ FeEmploymentOrderMapper feEmploymentOrderMapper,
|
|
74
|
93
|
ConversationService conversationService,
|
|
75
|
94
|
EnterpriseService enterpriseService,
|
|
|
95
|
+ DraftCostCalculationEngine draftCostCalculationEngine,
|
|
76
|
96
|
ObjectMapper objectMapper) {
|
|
77
|
97
|
this.feEmploymentDraftMapper = feEmploymentDraftMapper;
|
|
78
|
98
|
this.feDraftFieldMapper = feDraftFieldMapper;
|
|
|
99
|
+ this.feDraftCostSnapshotMapper = feDraftCostSnapshotMapper;
|
|
|
100
|
+ this.feEmploymentOrderMapper = feEmploymentOrderMapper;
|
|
79
|
101
|
this.conversationService = conversationService;
|
|
80
|
102
|
this.enterpriseService = enterpriseService;
|
|
|
103
|
+ this.draftCostCalculationEngine = draftCostCalculationEngine;
|
|
81
|
104
|
this.objectMapper = objectMapper;
|
|
82
|
105
|
}
|
|
83
|
106
|
|
|
|
@@ -138,6 +161,146 @@ public class McpDraftService {
|
|
138
|
161
|
return toInfoResponse(draft);
|
|
139
|
162
|
}
|
|
140
|
163
|
|
|
|
164
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
165
|
+ public McpDraftCalculateResponse calculate(String sessionId) {
|
|
|
166
|
+ FeConversation conversation = conversationService.requireBySessionId(sessionId);
|
|
|
167
|
+ FeEmploymentDraft draft = requireReadyDraft(conversation.getId());
|
|
|
168
|
+ List<DraftCostCalculationEngine.CalculatedScheme> schemes = draftCostCalculationEngine.calculate(draft);
|
|
|
169
|
+ persistCostSnapshots(draft, schemes);
|
|
|
170
|
+ updateDraftEstimates(draft, schemes, LocalDateTime.now());
|
|
|
171
|
+ return draftCostCalculationEngine.toApiResponse(schemes);
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
175
|
+ public McpDraftSubmitResponse submit(String sessionId, McpDraftSubmitRequest request) {
|
|
|
176
|
+ FeConversation conversation = conversationService.requireBySessionId(sessionId);
|
|
|
177
|
+ FeEmploymentDraft draft = requireReadyDraft(conversation.getId());
|
|
|
178
|
+ String schemeCode = request.getSelectedSchemeCode().trim().toUpperCase();
|
|
|
179
|
+ if (!ALLOWED_SCHEME_CODES.contains(schemeCode)) {
|
|
|
180
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "不支持的方案编码:" + request.getSelectedSchemeCode());
|
|
|
181
|
+ }
|
|
|
182
|
+ FeDraftCostSnapshot snapshot = findLatestSnapshot(draft.getId(), schemeCode);
|
|
|
183
|
+ if (snapshot == null) {
|
|
|
184
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "请先调用 calculate 获取方案后再提交");
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
188
|
+ FeEmploymentOrder order = buildOrderFromDraft(draft, snapshot, now);
|
|
|
189
|
+ order.setOrderNo("tmp_" + UUID.randomUUID().toString().replace("-", "").substring(0, 20));
|
|
|
190
|
+ try {
|
|
|
191
|
+ feEmploymentOrderMapper.insert(order);
|
|
|
192
|
+ } catch (DuplicateKeyException ex) {
|
|
|
193
|
+ throw new BizException(ErrorCode.BAD_REQUEST, "订单编号冲突,请重试");
|
|
|
194
|
+ }
|
|
|
195
|
+ order.setOrderNo(generateOrderNo(order.getId()));
|
|
|
196
|
+ feEmploymentOrderMapper.updateById(order);
|
|
|
197
|
+
|
|
|
198
|
+ FeEmploymentDraft draftUpdate = new FeEmploymentDraft();
|
|
|
199
|
+ draftUpdate.setId(draft.getId());
|
|
|
200
|
+ draftUpdate.setStatus(DB_STATUS_CONFIRMED);
|
|
|
201
|
+ draftUpdate.setConfirmedAt(now);
|
|
|
202
|
+ draftUpdate.setEstimatedTotal(snapshot.getEmployerTotalOutflow());
|
|
|
203
|
+ draftUpdate.setEstimatedPerCapita(snapshot.getEmployerAvgCostPerWorker());
|
|
|
204
|
+ draftUpdate.setUpdateTime(now);
|
|
|
205
|
+ feEmploymentDraftMapper.updateById(draftUpdate);
|
|
|
206
|
+
|
|
|
207
|
+ McpDraftSubmitResponse response = new McpDraftSubmitResponse();
|
|
|
208
|
+ response.setStatus(SUBMIT_SUCCESS);
|
|
|
209
|
+ response.setOrderId(order.getOrderNo());
|
|
|
210
|
+ return response;
|
|
|
211
|
+ }
|
|
|
212
|
+
|
|
|
213
|
+ private FeEmploymentDraft requireReadyDraft(Long conversationId) {
|
|
|
214
|
+ FeEmploymentDraft draft = requirePendingDraft(conversationId);
|
|
|
215
|
+ List<String> missing = parseMissingFields(draft.getMissingFields());
|
|
|
216
|
+ if (!missing.isEmpty()) {
|
|
|
217
|
+ throw new BizException(ErrorCode.BAD_REQUEST,
|
|
|
218
|
+ "草稿信息不完整,请先补全字段:" + String.join(", ", missing));
|
|
|
219
|
+ }
|
|
|
220
|
+ return draft;
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ private void persistCostSnapshots(FeEmploymentDraft draft,
|
|
|
224
|
+ List<DraftCostCalculationEngine.CalculatedScheme> schemes) {
|
|
|
225
|
+ feDraftCostSnapshotMapper.delete(new LambdaQueryWrapper<FeDraftCostSnapshot>()
|
|
|
226
|
+ .eq(FeDraftCostSnapshot::getDraftId, draft.getId()));
|
|
|
227
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
228
|
+ for (DraftCostCalculationEngine.CalculatedScheme scheme : schemes) {
|
|
|
229
|
+ FeDraftCostSnapshot snapshot = new FeDraftCostSnapshot();
|
|
|
230
|
+ snapshot.setDraftId(draft.getId());
|
|
|
231
|
+ snapshot.setPlanCode(scheme.getCode());
|
|
|
232
|
+ snapshot.setPlanName(scheme.getTitle());
|
|
|
233
|
+ snapshot.setEmployerTotalOutflow(scheme.getEmployerTotalOutflow());
|
|
|
234
|
+ snapshot.setEmployerAvgCostPerWorker(scheme.getEmployerAvgCostPerWorker());
|
|
|
235
|
+ snapshot.setWorkerTakeHomePerWorker(scheme.getWorkerTakeHomePerWorker());
|
|
|
236
|
+ snapshot.setIsRecommended(scheme.isRecommended());
|
|
|
237
|
+ snapshot.setComplianceTags(writeJsonArray(scheme.getTags()));
|
|
|
238
|
+ snapshot.setCalcVersion(draftCostCalculationEngine.calcVersion());
|
|
|
239
|
+ snapshot.setCreateTime(now);
|
|
|
240
|
+ feDraftCostSnapshotMapper.insert(snapshot);
|
|
|
241
|
+ }
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ private void updateDraftEstimates(FeEmploymentDraft draft,
|
|
|
245
|
+ List<DraftCostCalculationEngine.CalculatedScheme> schemes,
|
|
|
246
|
+ LocalDateTime now) {
|
|
|
247
|
+ DraftCostCalculationEngine.CalculatedScheme recommended = schemes.stream()
|
|
|
248
|
+ .filter(DraftCostCalculationEngine.CalculatedScheme::isRecommended)
|
|
|
249
|
+ .findFirst()
|
|
|
250
|
+ .orElse(schemes.get(0));
|
|
|
251
|
+ FeEmploymentDraft update = new FeEmploymentDraft();
|
|
|
252
|
+ update.setId(draft.getId());
|
|
|
253
|
+ update.setEstimatedTotal(recommended.getEmployerTotalOutflow());
|
|
|
254
|
+ update.setEstimatedPerCapita(recommended.getEmployerAvgCostPerWorker());
|
|
|
255
|
+ update.setUpdateTime(now);
|
|
|
256
|
+ feEmploymentDraftMapper.updateById(update);
|
|
|
257
|
+ }
|
|
|
258
|
+
|
|
|
259
|
+ private FeDraftCostSnapshot findLatestSnapshot(Long draftId, String schemeCode) {
|
|
|
260
|
+ return feDraftCostSnapshotMapper.selectOne(new LambdaQueryWrapper<FeDraftCostSnapshot>()
|
|
|
261
|
+ .eq(FeDraftCostSnapshot::getDraftId, draftId)
|
|
|
262
|
+ .eq(FeDraftCostSnapshot::getPlanCode, schemeCode)
|
|
|
263
|
+ .orderByDesc(FeDraftCostSnapshot::getCreateTime)
|
|
|
264
|
+ .last("LIMIT 1"));
|
|
|
265
|
+ }
|
|
|
266
|
+
|
|
|
267
|
+ private FeEmploymentOrder buildOrderFromDraft(FeEmploymentDraft draft,
|
|
|
268
|
+ FeDraftCostSnapshot snapshot,
|
|
|
269
|
+ LocalDateTime now) {
|
|
|
270
|
+ FeEmploymentOrder order = new FeEmploymentOrder();
|
|
|
271
|
+ order.setEnterpriseId(draft.getEnterpriseId());
|
|
|
272
|
+ order.setDraftId(draft.getId());
|
|
|
273
|
+ order.setTitle(StringUtils.hasText(draft.getTitle()) ? draft.getTitle() : "用工订单");
|
|
|
274
|
+ order.setWorkerCount(draft.getWorkerCount());
|
|
|
275
|
+ order.setWorkDays(draft.getWorkDays());
|
|
|
276
|
+ order.setWorkType(draft.getWorkType());
|
|
|
277
|
+ order.setWorkLocation(draft.getWorkLocation());
|
|
|
278
|
+ order.setWorkStartDate(draft.getWorkStartDate());
|
|
|
279
|
+ order.setWorkEndDate(draft.getWorkEndDate());
|
|
|
280
|
+ order.setOrderStatus(ORDER_STATUS_CONFIRMED);
|
|
|
281
|
+ order.setCurrentStepCode(STEP_PLAN_CONFIRMED);
|
|
|
282
|
+ order.setRegisteredCount(0);
|
|
|
283
|
+ order.setPendingCount(draft.getWorkerCount());
|
|
|
284
|
+ order.setAbnormalCount(0);
|
|
|
285
|
+ order.setTotalOutflow(snapshot.getEmployerTotalOutflow());
|
|
|
286
|
+ order.setCreateTime(now);
|
|
|
287
|
+ order.setUpdateTime(now);
|
|
|
288
|
+ order.setDelFlag(0);
|
|
|
289
|
+ return order;
|
|
|
290
|
+ }
|
|
|
291
|
+
|
|
|
292
|
+ private static String generateOrderNo(Long orderId) {
|
|
|
293
|
+ return String.format("ord_%d", orderId);
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ private String writeJsonArray(List<String> values) {
|
|
|
297
|
+ try {
|
|
|
298
|
+ return objectMapper.writeValueAsString(values);
|
|
|
299
|
+ } catch (Exception ex) {
|
|
|
300
|
+ return "[]";
|
|
|
301
|
+ }
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
141
|
304
|
private void validateGenerateRequest(McpDraftGenerateRequest request) {
|
|
142
|
305
|
if (request == null || !StringUtils.hasText(request.getSourceType())) {
|
|
143
|
306
|
throw new BizException(ErrorCode.BAD_REQUEST, "source_type 不能为空");
|