package com.lxzn.activiti.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.lxzn.activiti.dao.ToEquipmentTaskMapper; import com.lxzn.activiti.service.IToEquipmentTaskService; import com.lxzn.framework.domain.activiti.ToEquipmentTask; import com.lxzn.framework.domain.activiti.response.ActivitiCode; import com.lxzn.framework.domain.nc.DeviceInfo; import com.lxzn.framework.exception.ExceptionCast; import com.lxzn.framework.utils.ValidateUtil; import com.lxzn.framework.utils.file.FileUtil; import com.lxzn.nc.service.IDeviceGroupService; import com.lxzn.nc.service.IDeviceInfoService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class ToEquipmentTaskServiceImpl extends ServiceImpl implements IToEquipmentTaskService { @Autowired private IDeviceInfoService deviceInfoService; @Autowired private IDeviceGroupService deviceGroupService; @Override @Scheduled(cron = "${taskSync}") public void updateNcFileFromTask() { List taskList = this.selectTaskSyncFlag(1); if (taskList == null || taskList.isEmpty()) { return; } List successTasks = new ArrayList<>(); for (ToEquipmentTask task : taskList) { DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(task.getDeviceNo()); if (deviceInfo == null) { continue; } List pathList = deviceGroupService.findListParentTreeAll(deviceInfo.getGroupId()); if (pathList == null || pathList.isEmpty()) { continue; } String targetPath = StringUtils.join(pathList.toArray(), "/") + "/" + deviceInfo.getDeviceNo() + "/send/" + task.getFileName(); String sourceFilePath = task.getFilePath() + "/" + task.getFileEncodeName(); Boolean copySuccess = FileUtil.copyFileUpName(sourceFilePath, targetPath, task.getFileSuffix()); if (copySuccess) { task.setSyncFlag(2); successTasks.add(task); } } if (!successTasks.isEmpty()) { super.saveOrUpdateBatch(successTasks); } deleteSourceFilesAfterBatchUpdate(taskList); } /** * 批量更新后检查并删除源文件 */ private void deleteSourceFilesAfterBatchUpdate(List allTasks) { Map filePathMap = new HashMap<>(); for (ToEquipmentTask task : allTasks) { if (task.getSyncFlag() == 2) { String key = task.getFilePath() + "_" + task.getFileEncodeName(); if (!filePathMap.containsKey(key)) { filePathMap.put(key, task.getFilePath() + "/" + task.getFileEncodeName()); } } } for (Map.Entry entry : filePathMap.entrySet()) { String[] parts = entry.getKey().split("_"); if (parts.length >= 2) { String filePath = parts[0]; String fileEncodeName = parts[1]; List remainingTasks = this.selectTaskByNameAndSyncFlag( filePath, fileEncodeName, 1 ); if (remainingTasks == null || remainingTasks.isEmpty()) { FileUtil.deleteNcFile(entry.getValue()); } } } } @Override public List selectTaskSyncFlag(Integer syncFlag) { if (!ValidateUtil.validateInteger(syncFlag)) { return null; } return super.lambdaQuery() .eq(ToEquipmentTask::getSyncFlag, syncFlag) .list(); } @Override public List selectTaskByNameAndSyncFlag(String filePath, String fileEncodeName, Integer syncFlag) { if (!ValidateUtil.validateInteger(syncFlag) || !ValidateUtil.validateString(filePath) || !ValidateUtil.validateString(fileEncodeName)) { return null; } return super.lambdaQuery() .eq(ToEquipmentTask::getSyncFlag, syncFlag) .eq(ToEquipmentTask::getFilePath, filePath) .eq(ToEquipmentTask::getFileEncodeName, fileEncodeName) .list(); } }