lyh
2026-03-12 e5bd30e774dba285e0e22bbfa5e4e0d936d88e6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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<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();
    }
}