客商数据同步问题修改1
This commit is contained in:
+84
-69
@@ -24,9 +24,7 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
@@ -34,8 +32,11 @@ import java.util.concurrent.TimeUnit;
|
||||
public class UserShipperMasterDataSyncService {
|
||||
|
||||
private static final String LOCK_PREFIX = "shipper:mdm:sync:";
|
||||
private static final int PAGE_SIZE = 500;
|
||||
/** MDM 单页条数,不宜过大以免 MDM/Feign 超时 */
|
||||
private static final int PAGE_SIZE = 100;
|
||||
private static final long SYNC_LOCK_MINUTES = 30;
|
||||
/** MDM desc3:是否客户 */
|
||||
private static final String MDM_FLAG_YES = "1";
|
||||
|
||||
@Resource
|
||||
private ThirdPartyServiceFeign thirdPartyServiceFeign;
|
||||
@@ -128,41 +129,47 @@ public class UserShipperMasterDataSyncService {
|
||||
String lastModifyRecordTime = syncEntity == null ? null : syncEntity.getLastModifyRecordTime();
|
||||
|
||||
ShipperMasterDataSyncResultVo resultVo = new ShipperMasterDataSyncResultVo();
|
||||
List<MdmMerchantRecordDTO> allRecords = fetchAllRecords(orgNcCode, lastModifyRecordTime,
|
||||
loginUser.getUserPo().getTopOrganizationId());
|
||||
log.info("开始同步MDM客商, organizationId={}, orgNcCode={}, lastModifyRecordTime={}",
|
||||
organizationId, orgNcCode, lastModifyRecordTime);
|
||||
|
||||
String maxModifyTime = lastModifyRecordTime;
|
||||
for (MdmMerchantRecordDTO record : allRecords) {
|
||||
if (!belongsToOrg(record, orgNcCode)) {
|
||||
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
||||
continue;
|
||||
int currentPage = 1;
|
||||
Integer totalPages = null;
|
||||
do {
|
||||
MdmMerchantQueryResultDTO pageData = queryMerchantPage(orgNcCode, lastModifyRecordTime,
|
||||
loginUser.getUserPo().getTopOrganizationId(), currentPage);
|
||||
if (pageData == null || pageData.getDataInfo() == null) {
|
||||
break;
|
||||
}
|
||||
if (isDisabled(record)) {
|
||||
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
||||
continue;
|
||||
}
|
||||
if (StringUtils.isEmpty(record.getCode())) {
|
||||
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
UserShipperPo existing = userShipperApplicationService.findByCustomerNcCodeAndOrganizationId(
|
||||
record.getCode(), organizationId);
|
||||
if (existing == null) {
|
||||
userShipperApplicationService.addEnterpriseShipperFromMasterData(record, loginUser);
|
||||
resultVo.setAddCount(resultVo.getAddCount() + 1);
|
||||
} else {
|
||||
userShipperApplicationService.updateEnterpriseShipperFromMasterData(record, existing, loginUser);
|
||||
resultVo.setUpdateCount(resultVo.getUpdateCount() + 1);
|
||||
for (MdmMerchantRecordDTO record : pageData.getDataInfo()) {
|
||||
if (!shouldSyncRecord(record, orgNcCode)) {
|
||||
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
UserShipperPo existing = userShipperApplicationService.findByCustomerNcCodeAndOrganizationId(
|
||||
record.getCode(), organizationId);
|
||||
if (existing == null) {
|
||||
userShipperApplicationService.addEnterpriseShipperFromMasterData(record, loginUser);
|
||||
resultVo.setAddCount(resultVo.getAddCount() + 1);
|
||||
} else {
|
||||
userShipperApplicationService.updateEnterpriseShipperFromMasterData(record, existing, loginUser);
|
||||
resultVo.setUpdateCount(resultVo.getUpdateCount() + 1);
|
||||
}
|
||||
maxModifyTime = maxTime(maxModifyTime, record.getLastModifyRecordTime());
|
||||
} catch (Exception e) {
|
||||
resultVo.setFailCount(resultVo.getFailCount() + 1);
|
||||
String msg = "CODE=" + record.getCode() + " 同步失败:" + e.getMessage();
|
||||
resultVo.getErrorMessages().add(msg);
|
||||
log.error(msg, e);
|
||||
}
|
||||
maxModifyTime = maxTime(maxModifyTime, record.getLastModifyRecordTime());
|
||||
} catch (Exception e) {
|
||||
resultVo.setFailCount(resultVo.getFailCount() + 1);
|
||||
String msg = "CODE=" + record.getCode() + " 同步失败:" + e.getMessage();
|
||||
resultVo.getErrorMessages().add(msg);
|
||||
log.error(msg, e);
|
||||
}
|
||||
}
|
||||
totalPages = pageData.getTotalPages();
|
||||
if (totalPages == null || totalPages <= 0) {
|
||||
break;
|
||||
}
|
||||
currentPage++;
|
||||
} while (currentPage <= totalPages);
|
||||
|
||||
Date now = new Date();
|
||||
resultVo.setLastSyncTime(now);
|
||||
@@ -170,38 +177,45 @@ public class UserShipperMasterDataSyncService {
|
||||
return resultVo;
|
||||
}
|
||||
|
||||
private List<MdmMerchantRecordDTO> fetchAllRecords(String orgNcCode, String lastModifyRecordTime, Long topOrganizationId) {
|
||||
List<MdmMerchantRecordDTO> allRecords = new ArrayList<>();
|
||||
int currentPage = 1;
|
||||
Integer totalPages = null;
|
||||
do {
|
||||
MdmMerchantQueryDTO queryDTO = new MdmMerchantQueryDTO();
|
||||
queryDTO.setOrgNcCode(orgNcCode);
|
||||
if (StringUtils.isNotEmpty(lastModifyRecordTime)) {
|
||||
queryDTO.setLastModifyRecordTime(lastModifyRecordTime + "~");
|
||||
}
|
||||
queryDTO.setCurrentPage(currentPage);
|
||||
queryDTO.setCountPerPage(PAGE_SIZE);
|
||||
queryDTO.setTopOrganizationId(topOrganizationId);
|
||||
private MdmMerchantQueryResultDTO queryMerchantPage(String orgNcCode, String lastModifyRecordTime,
|
||||
Long topOrganizationId, int currentPage) {
|
||||
MdmMerchantQueryDTO queryDTO = new MdmMerchantQueryDTO();
|
||||
queryDTO.setOrgNcCode(orgNcCode);
|
||||
if (StringUtils.isNotEmpty(lastModifyRecordTime)) {
|
||||
queryDTO.setLastModifyRecordTime(lastModifyRecordTime + "~");
|
||||
}
|
||||
queryDTO.setCurrentPage(currentPage);
|
||||
queryDTO.setCountPerPage(PAGE_SIZE);
|
||||
queryDTO.setTopOrganizationId(topOrganizationId);
|
||||
|
||||
R<MdmMerchantQueryResultDTO> response = thirdPartyServiceFeign.queryMdmMerchant(queryDTO);
|
||||
if (response == null || response.getCode() != Constants.SUCCESS) {
|
||||
throw new ServiceException("调用MDM客商接口失败:" + (response == null ? "无响应" : response.getMsg()));
|
||||
}
|
||||
MdmMerchantQueryResultDTO data = response.getData();
|
||||
if (data == null) {
|
||||
break;
|
||||
}
|
||||
if (data.getDataInfo() != null) {
|
||||
allRecords.addAll(data.getDataInfo());
|
||||
}
|
||||
totalPages = data.getTotalPages();
|
||||
if (totalPages == null || totalPages <= 0) {
|
||||
break;
|
||||
}
|
||||
currentPage++;
|
||||
} while (currentPage <= totalPages);
|
||||
return allRecords;
|
||||
R<MdmMerchantQueryResultDTO> response = thirdPartyServiceFeign.queryMdmMerchant(queryDTO);
|
||||
if (response == null || response.getCode() != Constants.SUCCESS) {
|
||||
throw new ServiceException("调用MDM客商接口失败:" + (response == null ? "无响应" : response.getMsg()));
|
||||
}
|
||||
return response.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步过滤:MDM 查询侧 DESC24 无法精确过滤,必须在应用侧严格筛选。
|
||||
*/
|
||||
private boolean shouldSyncRecord(MdmMerchantRecordDTO record, String orgNcCode) {
|
||||
if (record == null || StringUtils.isEmpty(record.getCode())) {
|
||||
return false;
|
||||
}
|
||||
// 仅同步客户(desc3=是否客户)
|
||||
if (!MDM_FLAG_YES.equals(trimToEmpty(record.getDesc3()))) {
|
||||
return false;
|
||||
}
|
||||
// 对应组织编码必须等于当前组织 NC,空值不同步
|
||||
String recordOrgNc = trimToEmpty(record.getDesc24());
|
||||
if (StringUtils.isEmpty(recordOrgNc) || !orgNcCode.equals(recordOrgNc)) {
|
||||
return false;
|
||||
}
|
||||
// 跳过本组织主体(code 与组织 NC 相同,多为内部单位而非外部客商)
|
||||
if (orgNcCode.equals(trimToEmpty(record.getCode()))) {
|
||||
return false;
|
||||
}
|
||||
return !isDisabled(record);
|
||||
}
|
||||
|
||||
private void saveSyncWatermark(Long organizationId, String maxModifyTime, Date syncTime,
|
||||
@@ -242,16 +256,17 @@ public class UserShipperMasterDataSyncService {
|
||||
return orgJson.has("ncCode") ? orgJson.getString("ncCode") : null;
|
||||
}
|
||||
|
||||
private boolean belongsToOrg(MdmMerchantRecordDTO record, String orgNcCode) {
|
||||
return StringUtils.isEmpty(record.getDesc24()) || orgNcCode.equals(record.getDesc24());
|
||||
}
|
||||
|
||||
private boolean isDisabled(MdmMerchantRecordDTO record) {
|
||||
if (StringUtils.isEmpty(record.getDesc30())) {
|
||||
return false;
|
||||
}
|
||||
String status = record.getDesc30().trim();
|
||||
return "否".equals(status) || "停用".equals(status) || "0".equals(status);
|
||||
// 2=启用;0/3/否/停用 视为停用(结合 MDM 样例数据)
|
||||
return "否".equals(status) || "停用".equals(status) || "0".equals(status) || "3".equals(status);
|
||||
}
|
||||
|
||||
private String trimToEmpty(String value) {
|
||||
return value == null ? "" : value.trim();
|
||||
}
|
||||
|
||||
private String maxTime(String currentMax, String candidate) {
|
||||
|
||||
Reference in New Issue
Block a user