lyh
10 天以前 30c9299fa9ffc8d3cd51d4a18b4c0b33c3ef8141
src/main/java/com/lxzn/activiti/service/impl/ToEquipmentTaskServiceImpl.java
@@ -13,15 +13,14 @@
import com.lxzn.nc.service.IDeviceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 {
@@ -30,94 +29,49 @@
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private IDeviceGroupService deviceGroupService;
    @Value("${fileNcGateway}")
    private  String fileNcGateway;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Scheduled(cron = "${taskSync}")
    public void updateNcFileFromTask() {
        List<ToEquipmentTask> taskList = this.selectTaskSyncFlag(1);
        if (taskList == null || taskList.isEmpty()) {
        List<ToEquipmentTask> list = selectTaskSyncFlag(1);
        if(list == null || list.isEmpty()) {
            return;
        }
        List<ToEquipmentTask> successTasks = new ArrayList<>();
        for (ToEquipmentTask task : taskList) {
        for (ToEquipmentTask task : list) {
            //获取文件夹位置
            DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(task.getDeviceNo());
            if (deviceInfo == null) {
            if(deviceInfo == null) {
                continue;
            }
            List<String> pathList = deviceGroupService.findListParentTreeAll(deviceInfo.getGroupId());
            if (pathList == null || pathList.isEmpty()) {
            List<String> strings =  deviceGroupService.findListParentTreeAll(deviceInfo.getGroupId());
            if (strings == null  || strings.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) {
            String path = StringUtils.join(strings.toArray(), "/");
            Boolean b =  FileUtil.copyFileUpName(task.getFilePath() + "/" + task.getFileEncodeName(),
                    fileNcGateway +"/" +  path +"/" + task.getDeviceNo() + "/send/" + task.getFileName(),task.getFileSuffix());
            if (b) {
                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());
                }
            }
        }
        super.saveOrUpdateBatch(list);
    }
    @Override
    public List<ToEquipmentTask> selectTaskSyncFlag(Integer syncFlag) {
        if (!ValidateUtil.validateInteger(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)) {
        List<ToEquipmentTask> list = super.lambdaQuery().eq(ToEquipmentTask::getSyncFlag, syncFlag).list();
       /* List<ToEquipmentTask> list = new ArrayList<>();
        ToEquipmentTask t = getById("1800797949617278977");
        list.add(t);*/
        if(list == null || list.isEmpty()) {
            return null;
        }
        return super.lambdaQuery()
                .eq(ToEquipmentTask::getSyncFlag, syncFlag)
                .eq(ToEquipmentTask::getFilePath, filePath)
                .eq(ToEquipmentTask::getFileEncodeName, fileEncodeName)
                .list();
        return list;
    }
}