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.DateUtil; 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 { heartService.pushData(syncDataSetEntity, null, i); } catch (Exception e) { log.error(ExceptionUtils.getStackTrace(e)); e.printStackTrace(); } }); redisUtil.set("SPS_SYNC_UPLOAD_DATA", curTime1); } else { log.info("当前时间{},上次上传时间{},间隔时间{},不需要上传数据", DateUtil.formatDate(curTime1, "yyyy-MM-dd HH:mm:ss"), DateUtil.formatDate(lastTime1, "yyyy-MM-dd HH:mm:ss"), timeInterval1); } } 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)); } }); } 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)); } // 暂时注释掉DI下载 // 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()); } } }