package com.ecep.contract.task; import java.io.File; import org.springframework.util.StringUtils; import com.ecep.contract.MessageHolder; import com.ecep.contract.service.CompanyFileService; import com.ecep.contract.service.CompanyService; import com.ecep.contract.vo.CompanyFileVo; import com.ecep.contract.vo.CompanyVo; import lombok.Getter; import lombok.Setter; /** * 公司文件移动任务类 * 用于将文件从老系统中移动到指定的企业目录中 */ public class CompanyFileMoveTasker extends Tasker { @Getter @Setter private CompanyVo company; @Getter private boolean filesMoved = false; @Override protected Boolean execute(MessageHolder holder) throws Exception { updateTitle("移动公司文件"); updateProgress(0, 100); if (company == null) { holder.error("公司对象为空"); updateProgress(100, 100); return false; } try { holder.info("开始移动公司文件:" + company.getName()); updateProgress(10, 100); // 获取CompanyFileService CompanyFileService companyFileService = getCachedBean(CompanyFileService.class); CompanyService companyService = getCachedBean(CompanyService.class); // 获取公司的所有文件 holder.info("获取公司文件列表..."); java.util.List list = companyFileService.findByCompany(company); updateProgress(20, 100); if (list.isEmpty()) { holder.info("该公司没有文件需要移动"); updateProgress(100, 100); return false; } holder.info("共有" + list.size() + "个文件需要处理"); // 确保公司目录存在 holder.info("检查并创建公司目录..."); if (companyService.makePathAbsent(company, holder)) { company = companyService.save(company); holder.info("公司目录已创建"); } updateProgress(30, 100); // 检查公司路径 String path = company.getPath(); if (!StringUtils.hasText(path)) { holder.error("异常, 企业目录未设置"); updateProgress(100, 100); return false; } File companyPath = new File(path); int movedCount = 0; int totalFiles = list.size(); // 逐个处理文件 for (int i = 0; i < list.size(); i++) { CompanyFileVo companyFile = list.get(i); String filePath = companyFile.getFilePath(); // 更新进度 updateProgress(30 + (i * 70) / totalFiles, 100); if (StringUtils.hasText(filePath)) { File file = new File(filePath); if (file.exists()) { if (file.getParentFile().equals(companyPath)) { holder.info("文件已在目标位置:" + file.getName()); continue; } File dest = new File(companyPath, file.getName()); holder.info("移动文件:" + file.getName() + " -> " + companyPath.getName()); if (file.renameTo(dest)) { companyFile.setFilePath(dest.getAbsolutePath()); companyFileService.save(companyFile); holder.info("文件移动成功:" + file.getName()); movedCount++; filesMoved = true; } else { holder.warn("文件移动失败:" + file.getName()); } } else { holder.warn("文件不存在:" + filePath); } } } holder.info("文件移动完成,成功移动" + movedCount + "个文件"); updateProgress(100, 100); return filesMoved; } catch (Exception e) { holder.error("文件移动过程中发生错误:" + e.getMessage()); updateProgress(100, 100); throw e; } } }