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.
131 lines
5.3 KiB
Java
131 lines
5.3 KiB
Java
package com.glxp.api.task;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.glxp.api.common.res.BaseResponse;
|
|
import com.glxp.api.dao.basic.SysWorkplaceDocumentDao;
|
|
import com.glxp.api.dao.collect.IoCollectSetBustypeMapper;
|
|
import com.glxp.api.dao.schedule.ScheduledDao;
|
|
import com.glxp.api.entity.collect.IoCollectSet;
|
|
import com.glxp.api.entity.collect.IoCollectSetBustype;
|
|
import com.glxp.api.entity.system.ScheduledEntity;
|
|
import com.glxp.api.req.basic.WorkBindBusTypeRequest;
|
|
import com.glxp.api.req.collect.CollectOrderRequest;
|
|
import com.glxp.api.req.system.ScheduledRequest;
|
|
import com.glxp.api.res.basic.SysWorkplaceDocumentResponse;
|
|
import com.glxp.api.service.collect.IoCollectOriginService;
|
|
import com.glxp.api.service.collect.IoCollectSetService;
|
|
import com.glxp.api.util.MsDateUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
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.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
@Slf4j
|
|
@Component
|
|
@EnableScheduling
|
|
public class AsyncIoCollectOrderDownloadTask implements SchedulingConfigurer {
|
|
|
|
final Logger logger = LoggerFactory.getLogger(AsyncIoCollectOrderDownloadTask.class);
|
|
|
|
@Resource
|
|
private ScheduledDao scheduledDao;
|
|
@Resource
|
|
SysWorkplaceDocumentDao sysWorkplaceDocumentDao;
|
|
@Resource
|
|
IoCollectOriginService collectOriginService;
|
|
@Resource
|
|
IoCollectSetService collectSetService;
|
|
@Resource
|
|
IoCollectSetBustypeMapper ioCollectSetBustypeMapper;
|
|
|
|
private static final String LOCK_KEY = "IO_COLLECT_ORDER_DOWNLOAD_LOCK";
|
|
private static final long LOCK_TIMEOUT = 30 * 60 * 1000; // 30分钟超时时间
|
|
|
|
|
|
// 使用原子布尔值作为简单的锁机制
|
|
private final AtomicBoolean isRunning = new AtomicBoolean(false);
|
|
|
|
|
|
@Override
|
|
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
|
scheduledTaskRegistrar.addTriggerTask(() -> processWithLock(),
|
|
triggerContext -> {
|
|
ScheduledRequest scheduledRequest = new ScheduledRequest();
|
|
scheduledRequest.setCronName("ioCollectOrderDownloadTask");
|
|
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
|
|
String cron = scheduledEntity != null ? scheduledEntity.getCron() : "0 */10 * * * ?";
|
|
if (cron.isEmpty()) {
|
|
logger.error("cron is null");
|
|
}
|
|
return new CronTrigger(cron).nextExecutionTime(triggerContext);
|
|
});
|
|
}
|
|
|
|
private void processWithLock() {
|
|
// 如果已经在运行,则跳过本次执行
|
|
if (!isRunning.compareAndSet(false, true)) {
|
|
log.warn("上一次任务还在执行中,跳过本次执行");
|
|
return;
|
|
}
|
|
try {
|
|
log.info("开始执行订单下载任务");
|
|
process();
|
|
} finally {
|
|
isRunning.set(false);
|
|
log.info("订单下载任务执行完成");
|
|
}
|
|
}
|
|
|
|
|
|
private void process() {
|
|
//获取下载的起始时间
|
|
IoCollectSet ioCollectSet = collectSetService.getSet();
|
|
Date startDownloadTime = ioCollectSet.getStartDownloadTime();
|
|
String startDownloadTimes = MsDateUtil.formatDateTime(startDownloadTime);
|
|
Boolean autoDownload = ioCollectSet.getAutoDownload();
|
|
|
|
if (StrUtil.isNotBlank(startDownloadTimes) && autoDownload) {
|
|
|
|
//获取当前时间
|
|
String paramValue = startDownloadTimes;
|
|
String nowTime = MsDateUtil.getDateTime();
|
|
CollectOrderRequest collectOrderRequest = new CollectOrderRequest();
|
|
|
|
collectOrderRequest.setStartTime(paramValue);
|
|
collectOrderRequest.setEndTime(nowTime);
|
|
|
|
List<IoCollectSetBustype> sysWorkplaceDocumentResponses = ioCollectSetBustypeMapper.selectList(new LambdaQueryWrapper<IoCollectSetBustype>().eq(IoCollectSetBustype::getSetCode, "autoDownload"));
|
|
if (CollUtil.isNotEmpty(sysWorkplaceDocumentResponses)) {
|
|
for (IoCollectSetBustype item : sysWorkplaceDocumentResponses) {
|
|
String documentTypeCode = item.getBusType();
|
|
collectOrderRequest.setBusType(documentTypeCode);
|
|
try {
|
|
BaseResponse baseResponse = collectOriginService.downloadOrderV2(collectOrderRequest);
|
|
if (baseResponse.getCode() == 20000) {
|
|
ioCollectSet.setStartDownloadTime(MsDateUtil.parseDate(nowTime));
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("自动下载原始单据出错:{}", ExceptionUtils.getStackTrace(e));
|
|
}
|
|
}
|
|
}
|
|
collectSetService.updateById(ioCollectSet);
|
|
}
|
|
|
|
}
|
|
|
|
}
|