同步客商数据BUG修改

This commit is contained in:
秦鸿展
2026-06-11 10:46:33 +08:00
parent 10a18545f8
commit b5f5f27b84
4 changed files with 60 additions and 15 deletions
@@ -21,7 +21,6 @@ import com.mhd.user.interfaces.vo.ShipperMasterDataSyncTimeVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
@@ -35,6 +34,7 @@ public class UserShipperMasterDataSyncService {
private static final String LOCK_PREFIX = "shipper:mdm:sync:";
private static final int PAGE_SIZE = 500;
private static final long SYNC_LOCK_MINUTES = 30;
@Resource
private ThirdPartyServiceFeign thirdPartyServiceFeign;
@@ -46,6 +46,8 @@ public class UserShipperMasterDataSyncService {
private UserShipperMdmSyncMapper userShipperMdmSyncMapper;
@Resource
private RedisLock redisLock;
@Resource
private AsyncUserShipperMasterDataSyncService asyncUserShipperMasterDataSyncService;
public ShipperMasterDataSyncTimeVo getLastSyncTime() {
LoginUser loginUser = requireLoginUser();
@@ -55,27 +57,65 @@ public class UserShipperMasterDataSyncService {
if (syncEntity != null) {
vo.setLastSyncTime(syncEntity.getLastSyncTime());
vo.setLastModifyRecordTime(syncEntity.getLastModifyRecordTime());
vo.setSyncStatus(syncEntity.getSyncStatus());
vo.setSyncMessage(syncEntity.getSyncMessage());
}
return vo;
}
public ShipperMasterDataSyncResultVo syncMasterData() {
public void syncMasterData() {
LoginUser loginUser = requireLoginUser();
Long organizationId = loginUser.getUserPo().getOrganizationId();
String lockKey = LOCK_PREFIX + organizationId;
boolean locked = redisLock.tryLock(lockKey, 30, TimeUnit.SECONDS);
boolean locked = redisLock.tryLock(lockKey, SYNC_LOCK_MINUTES, TimeUnit.MINUTES);
if (!locked) {
throw new ServiceException("当前组织正在同步主数据,请稍后再试");
}
try {
return doSync(loginUser);
} finally {
redisLock.unlock(lockKey);
markSyncInProgress(organizationId);
asyncUserShipperMasterDataSyncService.syncMasterDataAsync(loginUser, lockKey);
}
public ShipperMasterDataSyncResultVo executeSync(LoginUser loginUser) {
return doSync(loginUser);
}
public void markSyncFailed(Long organizationId, String errorMessage) {
UserShipperMdmSyncEntity entity = userShipperMdmSyncMapper.selectByOrganizationId(organizationId);
Date now = new Date();
if (entity == null) {
entity = new UserShipperMdmSyncEntity();
entity.setOrganizationId(organizationId);
entity.setCreateTime(now);
}
entity.setSyncStatus(2);
entity.setSyncMessage("同步失败:" + errorMessage);
entity.setUpdateTime(now);
if (entity.getId() == null) {
userShipperMdmSyncMapper.insert(entity);
} else {
userShipperMdmSyncMapper.updateById(entity);
}
}
@Transactional(rollbackFor = Exception.class)
protected ShipperMasterDataSyncResultVo doSync(LoginUser loginUser) {
private void markSyncInProgress(Long organizationId) {
UserShipperMdmSyncEntity entity = userShipperMdmSyncMapper.selectByOrganizationId(organizationId);
Date now = new Date();
if (entity == null) {
entity = new UserShipperMdmSyncEntity();
entity.setOrganizationId(organizationId);
entity.setCreateTime(now);
}
entity.setSyncStatus(0);
entity.setSyncMessage("同步中...");
entity.setUpdateTime(now);
if (entity.getId() == null) {
userShipperMdmSyncMapper.insert(entity);
} else {
userShipperMdmSyncMapper.updateById(entity);
}
}
private ShipperMasterDataSyncResultVo doSync(LoginUser loginUser) {
Long organizationId = loginUser.getUserPo().getOrganizationId();
String orgNcCode = resolveOrgNcCode(organizationId);
if (StringUtils.isEmpty(orgNcCode)) {
@@ -28,7 +28,7 @@ public class UserShipperMdmSyncEntity {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date lastSyncTime;
@ApiModelProperty("同步状态:1-成功 2-失败")
@ApiModelProperty("同步状态:0-同步中 1-成功 2-失败")
private Integer syncStatus;
@ApiModelProperty("同步结果摘要")
@@ -19,7 +19,6 @@ import com.mhd.common.security.service.TokenService;
import com.mhd.user.application.service.UserShipperMasterDataSyncService;
import com.mhd.user.interfaces.dto.updateDTO.SettlementInfoUpdateDTO;
import com.mhd.user.interfaces.vo.SettlementInfoQueryVo;
import com.mhd.user.interfaces.vo.ShipperMasterDataSyncResultVo;
import com.mhd.user.interfaces.vo.ShipperMasterDataSyncTimeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -287,11 +286,11 @@ public class UserShipperAPI extends BaseController {
@PostMapping("/syncMasterData")
public AjaxResult syncMasterData() {
try {
ShipperMasterDataSyncResultVo result = userShipperMasterDataSyncService.syncMasterData();
return AjaxResult.success("同步完成", result);
userShipperMasterDataSyncService.syncMasterData();
return AjaxResult.success("同步任务已提交,请通过查询同步状态接口获取进度");
} catch (Exception e) {
log.error("同步主数据失败", e);
return AjaxResult.error("同步主数据失败:" + e.getMessage());
log.error("提交同步主数据任务失败", e);
return AjaxResult.error("提交同步主数据任务失败:" + e.getMessage());
}
}
@@ -17,4 +17,10 @@ public class ShipperMasterDataSyncTimeVo {
@ApiModelProperty("MDM增量水位时间")
private String lastModifyRecordTime;
@ApiModelProperty("同步状态:0-同步中 1-成功 2-失败")
private Integer syncStatus;
@ApiModelProperty("同步结果摘要")
private String syncMessage;
}