新增定时下载国家库数据任务代码
parent
02e2819ec1
commit
a5ab536508
@ -0,0 +1,50 @@
|
|||||||
|
package com.glxp.api.task;
|
||||||
|
|
||||||
|
import com.glxp.api.constant.AsyncDiDlHelper;
|
||||||
|
import com.glxp.api.entity.basic.ProductInfoEntity;
|
||||||
|
import com.glxp.api.service.basic.ProductInfoService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AsyncDiDlService {
|
||||||
|
|
||||||
|
|
||||||
|
@Value("${UDI_SERVER_URL}")
|
||||||
|
private String udiUrl;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ProductInfoService productInfoService;
|
||||||
|
@Resource
|
||||||
|
AsyncDiDlHelper asyncDiDlHelper;
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AsyncDiDlService.class);
|
||||||
|
|
||||||
|
@Async
|
||||||
|
public void asyncDiByTime(String updateTime) {
|
||||||
|
int page = 1;
|
||||||
|
int limit = 200;
|
||||||
|
while (true) {
|
||||||
|
logger.info("更新时间:" + updateTime + "----" + page + "----" + limit);
|
||||||
|
List<ProductInfoEntity> productInfoEntityList = asyncDiDlHelper.dlByTime(udiUrl, page, limit, updateTime);
|
||||||
|
if (productInfoEntityList != null && productInfoEntityList.size() > 0) {
|
||||||
|
productInfoService.insertProductInfos(productInfoEntityList);
|
||||||
|
if (productInfoEntityList.size() < limit) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
page++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("更新时间:" + updateTime + "----" + "下载结束");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.glxp.api.task;
|
||||||
|
|
||||||
|
import com.glxp.api.dao.schedule.ScheduledDao;
|
||||||
|
import com.glxp.api.entity.system.ScheduledEntity;
|
||||||
|
import com.glxp.api.req.system.ScheduledRequest;
|
||||||
|
import com.glxp.api.util.DateUtil;
|
||||||
|
import com.glxp.api.util.RedisUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||||
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||||
|
import org.springframework.scheduling.support.CronTrigger;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@EnableScheduling
|
||||||
|
public class AsyncDiDlTask implements SchedulingConfigurer {
|
||||||
|
|
||||||
|
final Logger logger = LoggerFactory.getLogger(AsyncDiDlTask.class);
|
||||||
|
@Resource
|
||||||
|
RedisUtil redisUtil;
|
||||||
|
@Resource
|
||||||
|
private ScheduledDao scheduledDao;
|
||||||
|
@Resource
|
||||||
|
AsyncDiDlService asyncDiDlService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
||||||
|
scheduledTaskRegistrar.addTriggerTask(() -> process(),
|
||||||
|
triggerContext -> {
|
||||||
|
ScheduledRequest scheduledRequest = new ScheduledRequest();
|
||||||
|
scheduledRequest.setCronName("syncDi");
|
||||||
|
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
|
||||||
|
if (scheduledEntity == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String cron = scheduledEntity.getCron();//"0 55 5 * * ?";
|
||||||
|
if (cron.isEmpty()) {
|
||||||
|
logger.error("cron is null");
|
||||||
|
}
|
||||||
|
return new CronTrigger(cron).nextExecutionTime(triggerContext);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void process() {
|
||||||
|
String day = DateUtil.getLastDayFormat(-1);
|
||||||
|
asyncDiDlService.asyncDiByTime(day);
|
||||||
|
|
||||||
|
|
||||||
|
String lastUpDiTime = (String) redisUtil.get("lastDiUpTime");
|
||||||
|
if (lastUpDiTime == null) {
|
||||||
|
lastUpDiTime = DateUtil.getLastDayFormat(-10);
|
||||||
|
}
|
||||||
|
asyncDiDlService.asyncDiByTime(lastUpDiTime);
|
||||||
|
redisUtil.set("lastDiUpTime", DateUtil.formatDate(new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue