客商数据同步问题修改1
This commit is contained in:
+50
-35
@@ -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,20 +129,20 @@ 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;
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(record.getCode())) {
|
|
||||||
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
resultVo.setSkipCount(resultVo.getSkipCount() + 1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -163,6 +164,12 @@ public class UserShipperMasterDataSyncService {
|
|||||||
log.error(msg, e);
|
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,11 +177,8 @@ 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;
|
|
||||||
Integer totalPages = null;
|
|
||||||
do {
|
|
||||||
MdmMerchantQueryDTO queryDTO = new MdmMerchantQueryDTO();
|
MdmMerchantQueryDTO queryDTO = new MdmMerchantQueryDTO();
|
||||||
queryDTO.setOrgNcCode(orgNcCode);
|
queryDTO.setOrgNcCode(orgNcCode);
|
||||||
if (StringUtils.isNotEmpty(lastModifyRecordTime)) {
|
if (StringUtils.isNotEmpty(lastModifyRecordTime)) {
|
||||||
@@ -188,20 +192,30 @@ public class UserShipperMasterDataSyncService {
|
|||||||
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) {
|
|
||||||
allRecords.addAll(data.getDataInfo());
|
/**
|
||||||
|
* 同步过滤:MDM 查询侧 DESC24 无法精确过滤,必须在应用侧严格筛选。
|
||||||
|
*/
|
||||||
|
private boolean shouldSyncRecord(MdmMerchantRecordDTO record, String orgNcCode) {
|
||||||
|
if (record == null || StringUtils.isEmpty(record.getCode())) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
totalPages = data.getTotalPages();
|
// 仅同步客户(desc3=是否客户)
|
||||||
if (totalPages == null || totalPages <= 0) {
|
if (!MDM_FLAG_YES.equals(trimToEmpty(record.getDesc3()))) {
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
currentPage++;
|
// 对应组织编码必须等于当前组织 NC,空值不同步
|
||||||
} while (currentPage <= totalPages);
|
String recordOrgNc = trimToEmpty(record.getDesc24());
|
||||||
return allRecords;
|
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