lyh
2026-03-12 e5bd30e774dba285e0e22bbfa5e4e0d936d88e6b
src/main/java/com/lxzn/activiti/service/impl/ToEquipmentTaskServiceImpl.java
@@ -4,8 +4,120 @@
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<ToEquipmentTaskMapper, ToEquipmentTask> implements IToEquipmentTaskService {
    @Autowired
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private IDeviceGroupService deviceGroupService;
    @Override
    @Scheduled(cron = "${taskSync}")
    public void updateNcFileFromTask() {
        List<ToEquipmentTask> taskList = this.selectTaskSyncFlag(1);
        if (taskList == null || taskList.isEmpty()) {
            return;
        }
        List<ToEquipmentTask> successTasks = new ArrayList<>();
        for (ToEquipmentTask task : taskList) {
            DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(task.getDeviceNo());
            if (deviceInfo == null) {
                continue;
            }
            List<String> 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<ToEquipmentTask> allTasks) {
        Map<String, String> 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<String, String> entry : filePathMap.entrySet()) {
            String[] parts = entry.getKey().split("_");
            if (parts.length >= 2) {
                String filePath = parts[0];
                String fileEncodeName = parts[1];
                List<ToEquipmentTask> remainingTasks = this.selectTaskByNameAndSyncFlag(
                        filePath,
                        fileEncodeName,
                        1
                );
                if (remainingTasks == null || remainingTasks.isEmpty()) {
                    FileUtil.deleteNcFile(entry.getValue());
                }
            }
        }
    }
    @Override
    public List<ToEquipmentTask> selectTaskSyncFlag(Integer syncFlag) {
        if (!ValidateUtil.validateInteger(syncFlag)) {
            return null;
        }
        return super.lambdaQuery()
                .eq(ToEquipmentTask::getSyncFlag, syncFlag)
                .list();
    }
    @Override
    public List<ToEquipmentTask> 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();
    }
}