|
|
@@ -27,6 +27,8 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class BentelerYidaSyncService {
|
|
|
|
|
|
+ private static final String YIDA_VERSION_CONFLICT_MESSAGE = "实例数据已修改";
|
|
|
+
|
|
|
private final YDClient_Form ydClientForm;
|
|
|
private final YDService ydService;
|
|
|
private final YDConf ydConf;
|
|
|
@@ -129,6 +131,22 @@ public class BentelerYidaSyncService {
|
|
|
private EiamFormSyncResult writeback(String formInstanceId, String tableFieldId,
|
|
|
List<Map<String, Object>> rows,
|
|
|
EiamBatchResult batchResult) {
|
|
|
+ Map<String, Object> update = buildWritebackUpdate(tableFieldId, rows, batchResult);
|
|
|
+ try {
|
|
|
+ updateForm(formInstanceId, update);
|
|
|
+ return EiamFormSyncResult.of(formInstanceId, batchResult, true, "回写成功");
|
|
|
+ } catch (RuntimeException ex) {
|
|
|
+ if (isVersionConflict(ex)) {
|
|
|
+ return retryWriteback(formInstanceId, tableFieldId, rows, batchResult);
|
|
|
+ }
|
|
|
+ log.error("宜搭实例执行结果回写失败, formInstanceId={}", formInstanceId, ex);
|
|
|
+ return writebackFailure(formInstanceId, batchResult, ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildWritebackUpdate(String tableFieldId,
|
|
|
+ List<Map<String, Object>> rows,
|
|
|
+ EiamBatchResult batchResult) {
|
|
|
Map<String, Object> update = new HashMap<>();
|
|
|
update.put(tableFieldId, mapper.rebuildRows(tableFieldId, rows,
|
|
|
batchResult.getItems()));
|
|
|
@@ -137,17 +155,74 @@ public class BentelerYidaSyncService {
|
|
|
update.put(conf.getTotalFieldId(), batchResult.getTotal());
|
|
|
update.put(conf.getSuccessFieldId(), batchResult.getSuccessCount());
|
|
|
update.put(conf.getFailedFieldId(), batchResult.getFailedCount());
|
|
|
+ return update;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateForm(String formInstanceId, Map<String, Object> update) {
|
|
|
Map<String, Object> bodyExt = new HashMap<>();
|
|
|
bodyExt.put("useLatestVersion", true);
|
|
|
bodyExt.put("ignoreEmpty", false);
|
|
|
+ ydClientForm.updateForm(auth(), formInstanceId, JSON.toJSONString(update), bodyExt);
|
|
|
+ }
|
|
|
+
|
|
|
+ private EiamFormSyncResult retryWriteback(String formInstanceId, String tableFieldId,
|
|
|
+ List<Map<String, Object>> originalRows,
|
|
|
+ EiamBatchResult batchResult) {
|
|
|
+ log.warn("宜搭实例版本冲突,重新读取后重试回写, formInstanceId={}", formInstanceId);
|
|
|
try {
|
|
|
- ydClientForm.updateForm(auth(), formInstanceId, JSON.toJSONString(update), bodyExt);
|
|
|
- return EiamFormSyncResult.of(formInstanceId, batchResult, true, "回写成功");
|
|
|
- } catch (RuntimeException ex) {
|
|
|
- log.error("宜搭实例执行结果回写失败, formInstanceId={}", formInstanceId, ex);
|
|
|
- return EiamFormSyncResult.of(formInstanceId, batchResult, false,
|
|
|
- StringUtils.defaultIfBlank(ex.getMessage(), "宜搭回写失败"));
|
|
|
+ Map<String, Object> latestInstance = ydClientForm.getForm(auth(), formInstanceId, null);
|
|
|
+ Map<String, Object> latestFormData = formData(latestInstance);
|
|
|
+ List<Map<String, Object>> latestRows = resolveDetailRows(formInstanceId, tableFieldId,
|
|
|
+ latestFormData.get(tableFieldId));
|
|
|
+ if (!samePeople(originalRows, latestRows, batchResult.getOperation())) {
|
|
|
+ throw new McException("YIDA_FORM_ROWS_CHANGED",
|
|
|
+ "宜搭人员子表已变化,停止自动重试回写");
|
|
|
+ }
|
|
|
+ updateForm(formInstanceId, buildWritebackUpdate(
|
|
|
+ tableFieldId, latestRows, batchResult));
|
|
|
+ return EiamFormSyncResult.of(formInstanceId, batchResult, true, "冲突重试回写成功");
|
|
|
+ } catch (RuntimeException retryEx) {
|
|
|
+ log.error("宜搭实例冲突重试回写失败, formInstanceId={}", formInstanceId, retryEx);
|
|
|
+ return writebackFailure(formInstanceId, batchResult, retryEx);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean samePeople(List<Map<String, Object>> originalRows,
|
|
|
+ List<Map<String, Object>> latestRows,
|
|
|
+ EiamBatchOperation operation) {
|
|
|
+ if (originalRows.size() != latestRows.size()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String identityFieldId = identityFieldId(operation);
|
|
|
+ for (int index = 0; index < originalRows.size(); index++) {
|
|
|
+ if (!StringUtils.equals(
|
|
|
+ firstText(originalRows.get(index).get(identityFieldId)),
|
|
|
+ firstText(latestRows.get(index).get(identityFieldId)))) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String identityFieldId(EiamBatchOperation operation) {
|
|
|
+ if (operation == EiamBatchOperation.CREATE) {
|
|
|
+ return conf.getOnboardingPhoneFieldId();
|
|
|
+ }
|
|
|
+ if (operation == EiamBatchOperation.UPDATE) {
|
|
|
+ return conf.getUpdateEmployeeFieldId() + "_id";
|
|
|
}
|
|
|
+ return conf.getOffboardingEmployeeFieldId() + "_id";
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isVersionConflict(RuntimeException ex) {
|
|
|
+ return StringUtils.contains(ex.getMessage(), YIDA_VERSION_CONFLICT_MESSAGE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private EiamFormSyncResult writebackFailure(String formInstanceId,
|
|
|
+ EiamBatchResult batchResult,
|
|
|
+ RuntimeException ex) {
|
|
|
+ return EiamFormSyncResult.of(formInstanceId, batchResult, false,
|
|
|
+ StringUtils.defaultIfBlank(ex.getMessage(), "宜搭回写失败"));
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|