CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.ld.igds.n2.service;
 
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.DeviceStatus;
import com.ld.igds.constant.DeviceType;
import com.ld.igds.io.request.ExeDevice;
import com.ld.igds.models.DicTrigger;
import com.ld.igds.models.N2IntelTask;
import com.ld.igds.n2.N2ModelTag;
import com.ld.igds.n2.N2Util;
import com.ld.igds.n2.dto.N2AutoData;
import com.ld.igds.n2.dto.N2MacData;
import com.ld.igds.n2.dto.N2Param;
import com.ld.igds.n2.mapper.N2ServiceMapper;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.*;
 
@Slf4j
@Component
public class CoreN2ServiceImpl implements CoreN2Service {
 
 
    @Autowired
    private N2ServiceMapper n2ServiceMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private HMacConfService hMacConfService;
 
    @Override
    public void updateN2MacBySerId(N2MacData data) {
        hMacConfService.updateMac(data);
    }
 
    @Override
    public List<N2AutoData> listAutoData(N2Param param) {
        return n2ServiceMapper.listAutoData(param);
    }
 
    @Override
    public List<ExeDevice> listAutoConfData(N2Param param) {
        if (null == param.getAutoId()) {
            log.error("没有获取到模式的信息,查询失败");
            return null;
        }
 
        List<ExeDevice> list = n2ServiceMapper.listAutoDataConf(param);
        if (null == list || list.isEmpty())
            return null;
 
        // 调整数据,只获取气调相关设备
        List<ExeDevice> result = new ArrayList<>();
        for (ExeDevice device : list) {
 
            if (DeviceType.TYPE_04.getCode().equals(device.getType())) {
                result.add(device);
            }
            if (DeviceType.TYPE_07.getCode().equals(device.getType())) {
                result.add(device);
            }
            if (DeviceType.TYPE_09.getCode().equals(device.getType())) {
                result.add(device);
            }
            if (DeviceType.TYPE_0D.getCode().equals(device.getType())) {
                result.add(device);
            }
 
        }
        return result;
    }
 
    @Override
    public N2AutoData queryAutoDataById(N2Param param) {
        if (StringUtils.isEmpty(param.getAutoId())) {
            return null;
        }
        N2AutoData result = n2ServiceMapper.queryAutoDataById(param);
        if (null == result) {
            return null;
        }
        result.setListConf(this.listAutoConfData(param));
        return result;
    }
 
    @Override
    public N2AutoData queryAutoDataByModeTag(N2Param param) {
        if (StringUtils.isEmpty(param.getModeTag())) {
            return null;
        }
 
        List<N2AutoData> list = n2ServiceMapper.queryAutoDataByModeTag(param);
 
        if (null == list || list.isEmpty()) return null;
 
        N2AutoData result = list.get(0);
        param.setAutoId(result.getId());
        result.setListConf(this.listAutoConfData(param));
        return result;
    }
 
    @Override
    public Map<String, List<ExeDevice>> getExeDeviceByAutoConf(
            List<ExeDevice> listConf, String closeTag) {
        if (null == listConf || listConf.isEmpty())
            return null;
 
        Map<String, List<ExeDevice>> result = new HashMap<>();
        for (ExeDevice device : listConf) {
 
            // 屏蔽需要操作的设备信息
            if (null == device.getTargetStatus()) {
                continue;
            }
 
            if (DeviceStatus.CLOSE.getCode().equals(device.getTargetStatus())) {
                continue;
            }
            if (DeviceStatus.F_CLOSE.getCode().equals(device.getTargetStatus())) {
                continue;
            }
            if (DeviceStatus.W_CLOSE.getCode().equals(device.getTargetStatus())) {
                continue;
            }
 
            // 需要操作的设备,在执行关闭时候执行反向动作
            if (Constant.YN_Y.equals(closeTag)) {
                if (DeviceStatus.OPEN.getCode()
                        .equals(device.getTargetStatus())) {
                    device.setTargetStatus(DeviceStatus.CLOSE.getCode());
                }
                if (DeviceStatus.W_OPEN.getCode().equals(
                        device.getTargetStatus())) {
                    device.setTargetStatus(DeviceStatus.W_CLOSE.getCode());
                }
                if (DeviceStatus.F_OPEN.getCode().equals(
                        device.getTargetStatus())) {
                    device.setTargetStatus(DeviceStatus.F_CLOSE.getCode());
                    if (DeviceType.TYPE_0C.getCode().equals(device.getType())
                            || DeviceType.TYPE_02.getCode().equals(
                            device.getType())) {
                        device.setTargetStatus(DeviceStatus.W_CLOSE.getCode());
                    }
                }
                if (DeviceStatus.F_OPEN_F.getCode().equals(
                        device.getTargetStatus())) {
                    device.setTargetStatus(DeviceStatus.F_CLOSE.getCode());
                    if (DeviceType.TYPE_0C.getCode().equals(device.getType())
                            || DeviceType.TYPE_02.getCode().equals(
                            device.getType())) {
                        device.setTargetStatus(DeviceStatus.W_CLOSE.getCode());
                    }
                }
            }
 
            // 执行分组
            if (null == result.get(device.getSerId())) {
                result.put(device.getSerId(), new ArrayList<>());
            }
            result.get(device.getSerId()).add(device);
        }
        return result;
    }
 
    @Override
    public String updateAuto(N2AutoData data) {
 
        if (null == data.getDelTag()) data.setDelTag(Constant.YN_N);
 
        if (Constant.YN_Y.equals(data.getDelTag())) {
            n2ServiceMapper.delAuto(data.getId());
            return "数据删除完成!";
        }
 
        data.setUpdateTime(new Date());
        data.setUpdateUser(ContextUtil.getLoginUserCName());
 
        if (StringUtils.isEmpty(data.getExeMode())) {
            data.setExeMode(N2Util.EXE_MODE_01);
        }
 
        if (StringUtils.isEmpty(data.getId())) {
            data.setId(ContextUtil.getUUID());
            n2ServiceMapper.addAuto(data);
 
            return "数据新增成功!";
        }
 
        n2ServiceMapper.updateAuto(data);
        return "数据更新完成!";
    }
 
    @Override
    public String updateAutoConf(String autoId, List<ExeDevice> deviceList) {
        // 根据模式ID删除原有的配置信息
        n2ServiceMapper.delAutoConfByAutoId(autoId);
 
        // 遍历设备信息,将目标状态= NONE的设备踢出
        for (ExeDevice device : deviceList) {
            if (StringUtils.isEmpty(device.getTargetStatus())
                    || DeviceStatus.ZERO.getCode().equals(
                    device.getTargetStatus())) {
                // do nothing
            } else {
                device.setAutoId(autoId);
                n2ServiceMapper.addAutoConf(device);
            }
        }
        return "配置更新成功!";
    }
 
    @Override
    public List<DicTrigger> listN2ModelTag(String allTag) {
        List<DicTrigger> list = new ArrayList<>();
 
        list.add(new DicTrigger(N2ModelTag.MODEL_01.getCode(), N2ModelTag.MODEL_01.getMsg()));
        list.add(new DicTrigger(N2ModelTag.MODEL_02.getCode(), N2ModelTag.MODEL_02.getMsg()));
        list.add(new DicTrigger(N2ModelTag.MODEL_03.getCode(), N2ModelTag.MODEL_03.getMsg()));
        list.add(new DicTrigger(N2ModelTag.MODEL_04.getCode(), N2ModelTag.MODEL_04.getMsg()));
 
        list.add(new DicTrigger(N2ModelTag.MODEL_30.getCode(), N2ModelTag.MODEL_30.getMsg()));
        list.add(new DicTrigger(N2ModelTag.MODEL_31.getCode(), N2ModelTag.MODEL_31.getMsg()));
 
        list.add(new DicTrigger(N2ModelTag.MODEL_99.getCode(), N2ModelTag.MODEL_99.getMsg()));
        return list;
    }
 
    @Override
    public void addIntelTaskCache(N2IntelTask task) {
        String key = N2Util.createIntelTaskKey(task);
        long time = N2Util.getIntelTaskTime(task);
 
        redisUtil.set(key, task, time);
    }
 
    @Override
    public List<N2IntelTask> listIntelTaskByCache(N2Param param) {
        if (null == param) param = new N2Param();
        String patten = N2Util.createIntelTaskKeyPatten();
 
        Set<String> keys = redisUtil.keys(patten);
 
        if (null == keys || keys.isEmpty()) return null;
 
        List<N2IntelTask> result = new ArrayList<>();
        N2IntelTask task;
        for (String key : keys) {
            task = (N2IntelTask) redisUtil.get(key);
            if (null == task) continue;
 
            if (null == param.getDepotId()) {
                result.add(task);
                continue;
            }
 
            //如果有仓库信息,只返回当前仓库相关的任务
            if (task.getDepotId().equals(param.getDepotId())) {
                result.add(task);
                continue;
            }
        }
        return result;
    }
 
 
}