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