commit
3724da06cf
7 changed files with 2621 additions and 4 deletions
@ -0,0 +1,96 @@ |
|||||||
|
package org.springblade.lims.Scheduled; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springblade.lims.entry.Entrust; |
||||||
|
import org.springblade.lims.service.IEntrtrustService; |
||||||
|
import org.springblade.system.cache.DictBizCache; |
||||||
|
import org.springblade.system.enums.DictBizEnum; |
||||||
|
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* @BelongsProject: project_husbandry_back |
||||||
|
* @BelongsPackage:org.springblade.lims.Scheduled |
||||||
|
* @Annotation:定时任务 |
||||||
|
* @Author:YangMaoFu |
||||||
|
* @date :Created in 2023/7/6 14:18 |
||||||
|
* @CreateTime: 2026-04-16 23:27 |
||||||
|
* @Description: TODO |
||||||
|
* @Version: 1.0 |
||||||
|
*/ |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
@Component |
||||||
|
public class GlobalScheduledTasks { |
||||||
|
private final IEntrtrustService service; |
||||||
|
|
||||||
|
public GlobalScheduledTasks(IEntrtrustService service) { |
||||||
|
this.service = service; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 每晚 00:00 执行报告自动生成 |
||||||
|
*/ |
||||||
|
@Scheduled(cron = "0 0 0 * * ?") |
||||||
|
public void downloadFileAtMidnight() { |
||||||
|
// ===================== 核心:开关判断 =====================
|
||||||
|
String enabled = DictBizCache.getKey(DictBizEnum.SCHEDULED_TASK.getName(), "openScheduled"); |
||||||
|
if ("1".equals(enabled)) { |
||||||
|
log.info("===== 报告生成任务已关闭,不执行 ====="); |
||||||
|
return; |
||||||
|
} |
||||||
|
log.info("===== 凌晨0点,开始执行报告生成任务 =====: {}" ,LocalDateTime.now()); |
||||||
|
try { |
||||||
|
int successCount = 0; |
||||||
|
int failCount = 0; |
||||||
|
List<String> failMessages = new ArrayList<>(); |
||||||
|
String startCreateTime = DictBizCache.getKey(DictBizEnum.SCHEDULED_TASK.getName(), "startCreateTime"); |
||||||
|
// 空值检查
|
||||||
|
if (startCreateTime == null || startCreateTime.trim().isEmpty()) { |
||||||
|
log.error("开始创建时间配置为空,无法执行报告生成任务"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||||
|
Date creatTime = sdf.parse(startCreateTime + " 00:00:00"); |
||||||
|
LambdaQueryWrapper<Entrust> wrapper = new LambdaQueryWrapper<>(); |
||||||
|
wrapper.in(Entrust::getEntrustStatus, Arrays.asList("7", "8")); |
||||||
|
wrapper.eq(Entrust::getIsGenreport, "1"); |
||||||
|
wrapper.eq(Entrust::getIsDeleted, "0"); |
||||||
|
wrapper.between(Entrust::getCreateTime, creatTime, new Date()); |
||||||
|
List<Entrust> entrustList = service.list(wrapper); |
||||||
|
String creatTimeStr = sdf.format(creatTime); |
||||||
|
log.info("开始时间: {}", creatTimeStr); |
||||||
|
log.info("查询到需要生成报告的委托单数量: {}", entrustList.size()); |
||||||
|
|
||||||
|
// 遍历生成报告
|
||||||
|
for (Entrust entrust : entrustList) { |
||||||
|
try { |
||||||
|
String fullPath = service.generReport(entrust); |
||||||
|
successCount++; |
||||||
|
log.info("报告生成成功: {}", entrust.getAcceptanceNum()); |
||||||
|
log.info("报告生成路径: {}", fullPath); |
||||||
|
} catch (Exception e) { |
||||||
|
failCount++; |
||||||
|
String errorMsg = "委托单ID:" + entrust.getId() + |
||||||
|
"- 编号:" + entrust.getAcceptanceNum() + |
||||||
|
"- 错误:" + e.getMessage(); |
||||||
|
failMessages.add(errorMsg); |
||||||
|
log.error("报告生成失败: {}", errorMsg, e); |
||||||
|
} |
||||||
|
} |
||||||
|
log.info("===== 报告生成任务完成 =====: {}, 成功: {}, 失败: {}" ,LocalDateTime.now(), successCount, failCount); |
||||||
|
if (!failMessages.isEmpty()) { |
||||||
|
log.error("失败的委托单列表: {}", failMessages); |
||||||
|
} |
||||||
|
} catch (Exception e) { |
||||||
|
log.error("===== 报告生成任务异常 =====: {}" ,LocalDateTime.now(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,66 @@ |
|||||||
|
package org.springblade.lims.utils; |
||||||
|
|
||||||
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
||||||
|
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; |
||||||
|
import org.springblade.system.cache.DictBizCache; |
||||||
|
import org.springblade.system.enums.DictBizEnum; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileOutputStream; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @BelongsProject: project_husbandry_back |
||||||
|
* @BelongsPackage:org.springblade.lims.utils |
||||||
|
* @Annotation: ZIP压缩工具类 |
||||||
|
* @Author:YangMaoFu |
||||||
|
* @date :Created in 2023/7/6 14:18 |
||||||
|
* @CreateTime: 2026-04-14 23:14 |
||||||
|
* @Description: TODO |
||||||
|
* @Version: 1.0 |
||||||
|
*/ |
||||||
|
public class ZipUtils { |
||||||
|
|
||||||
|
/** |
||||||
|
* 将ZIP文件保存到服务器指定目录 |
||||||
|
* |
||||||
|
* @param fileMap 文件映射 |
||||||
|
* @param zipName ZIP文件名 |
||||||
|
* @return 完整文件路径 |
||||||
|
*/ |
||||||
|
public static String saveZipToServer(Map<String, byte[]> fileMap, String zipName) throws Exception { |
||||||
|
String saveDir = DictBizCache.getKey(DictBizEnum.SCHEDULED_TASK.getName(), "saveDirFile"); |
||||||
|
// 确保目录存在
|
||||||
|
File dir = new File(saveDir); |
||||||
|
if (!dir.exists()) { |
||||||
|
dir.mkdirs(); |
||||||
|
} |
||||||
|
|
||||||
|
// 生成完整路径
|
||||||
|
String fullPath = saveDir + File.separator + zipName; |
||||||
|
File zipFile = new File(fullPath); |
||||||
|
|
||||||
|
// 写入ZIP文件
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(zipFile); |
||||||
|
ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(fos)) { |
||||||
|
|
||||||
|
// 写入所有文件
|
||||||
|
for (Map.Entry<String, byte[]> entry : fileMap.entrySet()) { |
||||||
|
String fileName = entry.getKey(); |
||||||
|
byte[] fileData = entry.getValue(); |
||||||
|
|
||||||
|
// 创建Zip条目
|
||||||
|
ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName); |
||||||
|
zipOut.putArchiveEntry(zipEntry); |
||||||
|
|
||||||
|
// 写入文件数据
|
||||||
|
zipOut.write(fileData); |
||||||
|
zipOut.closeArchiveEntry(); |
||||||
|
} |
||||||
|
|
||||||
|
zipOut.finish(); |
||||||
|
} |
||||||
|
|
||||||
|
return fullPath; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue