You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/task/SyncHeartService.java

144 lines
4.8 KiB
Java

package com.glxp.api.task;
import cn.hutool.core.thread.ThreadUtil;
import com.glxp.api.constant.BasicExportTypeEnum;
import com.glxp.api.dao.system.SyncDataSetDao;
import com.glxp.api.entity.system.SyncDataSetEntity;
import com.glxp.api.service.sync.HeartService;
import com.glxp.api.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
@Slf4j
@Service
public class SyncHeartService {
@Resource
RedisUtil redisUtil;
@Resource
HeartService heartService;
@Resource
private SyncDataSetDao syncDataSetDao;
public void syncProcess() {
SyncDataSetEntity syncDataSetEntity = syncDataSetDao.selectSet();
if (!syncDataSetEntity.isDownstreamEnable()) {
return;
}
//查询数据同步设置
pushData(syncDataSetEntity);
pushOrder(syncDataSetEntity);
pullData(syncDataSetEntity);
}
public void pushData(SyncDataSetEntity syncDataSetEntity) {
//定时上传最近更新基础数据至上游轮询时间
long timeInterval1 = syncDataSetEntity.getSyncTime() * 6 * 1000L;
long curTime1 = System.currentTimeMillis();
Long lastTime1 = (Long) redisUtil.get("SPS_SYNC_UPLOAD_DATA");
if (lastTime1 == null) {
lastTime1 = System.currentTimeMillis();
redisUtil.set("SPS_SYNC_UPLOAD_DATA", lastTime1);
}
try {
if (curTime1 - lastTime1 > timeInterval1) {
Arrays.stream(BasicExportTypeEnum.values()).forEach(i -> {
try {
3 months ago
heartService.pushData(syncDataSetEntity, null, i);
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
e.printStackTrace();
}
});
redisUtil.set("SPS_SYNC_UPLOAD_DATA", curTime1);
}
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
e.printStackTrace();
}
}
public void customPushData(BasicExportTypeEnum basicExportTypeEnum) {
SyncDataSetEntity syncDataSetEntity = syncDataSetDao.selectSet();
ThreadUtil.execAsync(() -> {
try {
heartService.pushData(syncDataSetEntity, null, basicExportTypeEnum);
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
}
});
}
3 months ago
public void pushOrder(SyncDataSetEntity syncDataSetEntity) {
//定时上传最近更新单据数据至上游轮询时间
long timeInterval2 = syncDataSetEntity.getOrderSyncTime() * 6 * 1000L;
long curTime2 = System.currentTimeMillis();
Long lastTime2 = (Long) redisUtil.get("SPS_SYNC_UPLOAD_ORDER");
if (lastTime2 == null) {
lastTime2 = System.currentTimeMillis();
redisUtil.set("SPS_SYNC_UPLOAD_ORDER", lastTime2);
}
try {
if (curTime2 - lastTime2 > timeInterval2) {
heartService.uploadAllOrder(null);
heartService.uploadAllBusOrder(null);
redisUtil.set("SPS_SYNC_UPLOAD_ORDER", curTime2);
}
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
e.printStackTrace();
}
}
public void pullData(SyncDataSetEntity syncDataSetEntity) {
if (syncDataSetEntity == null) {
syncDataSetEntity = syncDataSetDao.selectSet();
if (!syncDataSetEntity.isDownstreamEnable()) {
return;
}
}
//定时下载上游最近更新数据轮询时间
long timeInterval = syncDataSetEntity.getSyncDownloadTime() * 6 * 100;
long curTime = System.currentTimeMillis();
Long lastTime = (Long) redisUtil.get("SPS_SYNC_DOWNLOAD_DATA");
if (lastTime == null) {
lastTime = System.currentTimeMillis();
redisUtil.set("SPS_SYNC_DOWNLOAD_DATA", lastTime);
}
if (curTime - lastTime > timeInterval) {
try {
heartService.dlAllOrder();
} catch (Exception e) {
e.printStackTrace();
log.error(ExceptionUtils.getStackTrace(e));
}
try {
heartService.dlAllDiProducts();
} catch (Exception e) {
e.printStackTrace();
log.error(ExceptionUtils.getStackTrace(e));
}
Arrays.stream(BasicExportTypeEnum.values()).forEach(i -> {
heartService.pullData(i);
});
redisUtil.set("SPS_SYNC_DOWNLOAD_DATA", System.currentTimeMillis());
}
}
}