huang
2025-10-31 1fed5e7bab3a8f6b9adbfcd3695e14a03d47677f
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.mes.controller;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mes.entity.PlcAddress;
import com.mes.service.PlcAddressService;
import com.mes.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * PLC地址映射配置控制器
 * 提供PLC地址映射配置的增删改查接口
 * 
 * @author huang
 * @date 2025/10/29
 */
@Slf4j
@RestController
@RequestMapping("address-mapping")
@Api(tags = "PLC地址映射配置管理")
public class PlcAddressController {
 
    @Resource
    private PlcAddressService plcAddressService;
 
    /**
     * 获取所有PLC地址映射配置
     */
    @GetMapping("/list")
    @ApiOperation("获取所有PLC地址映射配置")
    public Result<List<PlcAddress>> getAllMappings() {
        try {
            List<PlcAddress> mappings = plcAddressService.getAllMappings();
            return Result.success(mappings);
        } catch (Exception e) {
            log.error("获取PLC地址映射配置列表失败", e);
            return Result.error("获取PLC地址映射配置列表失败: " + e.getMessage());
        }
    }
 
    /**
     * 分页获取PLC地址映射配置
     */
    @GetMapping("/page")
    @ApiOperation("分页获取PLC地址映射配置")
    public Result<IPage<PlcAddress>> getMappingsByPage(
            @ApiParam("页码,从1开始") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页条数") @RequestParam(defaultValue = "10") int size,
            @ApiParam("项目标识,支持模糊查询") @RequestParam(required = false) String projectId,
            @ApiParam("PLC IP地址,支持模糊查询") @RequestParam(required = false) String plcIp) {
        try {
            IPage<PlcAddress> pageResult = plcAddressService.getMappingsByPage(page, size, projectId, plcIp);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("分页获取PLC地址映射配置失败", e);
            return Result.error("分页获取PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据ID获取PLC地址映射配置
     */
    @GetMapping("/detail")
    @ApiOperation("根据ID获取PLC地址映射配置")
    public Result<PlcAddress> getMappingById(
            @ApiParam("配置ID") @RequestParam Long id) {
        try {
            PlcAddress mapping = plcAddressService.getMappingById(id);
            if (mapping != null) {
                return Result.success(mapping);
            } else {
                return Result.error("未找到ID为 " + id + " 的PLC地址映射配置");
            }
        } catch (Exception e) {
            log.error("获取PLC地址映射配置失败,ID: " + id, e);
            return Result.error("获取PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据项目标识获取PLC地址映射配置
     */
    @GetMapping("/project")
    @ApiOperation("根据项目标识获取PLC地址映射配置")
    public Result<PlcAddress> getMappingByProjectId(
            @ApiParam("项目标识") @RequestParam String projectId) {
        try {
            PlcAddress mapping = plcAddressService.getMappingByProjectId(projectId);
            if (mapping != null) {
                return Result.success(mapping);
            } else {
                return Result.error("未找到项目标识为 " + projectId + " 的PLC地址映射配置");
            }
        } catch (Exception e) {
            log.error("获取PLC地址映射配置失败,项目标识: " + projectId, e);
            return Result.error("获取PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据项目ID获取项目配置(包含地址映射)
     * 对应前端配置获取需求
     */
    @GetMapping("/project/config")
    @ApiOperation("根据项目ID获取项目配置(包含地址映射)")
    public Result<Map<String, Object>> getProjectConfig(String projectId) {
        try {
            PlcAddress mapping = plcAddressService.getProjectConfig(projectId);
            
            // 组装输出,仅基于数据库实体
            Map<String, Object> result = new HashMap<>();
            result.put("projectId", projectId);
            result.put("dbArea", mapping != null ? mapping.getDbArea() : "DB1");
            result.put("beginIndex", mapping != null ? mapping.getBeginIndex() : 0);
            result.put("plcIp", mapping != null ? mapping.getPlcIp() : null);
            result.put("plcType", mapping != null ? mapping.getPlcType() : null);
            
            // 解析addressMapping JSON为Map
            Map<String, Integer> addressMap = new HashMap<>();
            if (mapping != null && mapping.getAddressMapping() != null && !mapping.getAddressMapping().trim().isEmpty()) {
                try {
                    addressMap = new com.fasterxml.jackson.databind.ObjectMapper()
                            .readValue(mapping.getAddressMapping(), new com.fasterxml.jackson.core.type.TypeReference<Map<String, Integer>>() {});
                } catch (Exception parseEx) {
                    log.warn("解析地址映射JSON失败: {}", mapping.getAddressMapping());
                }
            }
            result.put("addressMapping", addressMap);
            
            if (mapping != null) {
                result.put("id", mapping.getId());
                result.put("projectName", mapping.getProjectName());
                result.put("remarks", mapping.getRemarks());
            }
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取项目配置失败,项目标识: " + projectId, e);
            return Result.error("获取项目配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 创建新的PLC地址映射配置
     */
    @PostMapping
    @ApiOperation("创建新的PLC地址映射配置")
    public Result<PlcAddress> createMapping(
            @ApiParam("PLC地址映射配置") @RequestBody PlcAddress mapping) {
        try {
            // 参数验证
            if (mapping.getProjectId() == null || mapping.getProjectId().trim().isEmpty()) {
                return Result.error("项目标识不能为空");
            }
            if (mapping.getDbArea() == null || mapping.getDbArea().trim().isEmpty()) {
                return Result.error("DB块地址不能为空");
            }
           
            if (mapping.getBeginIndex() < 0) {
                mapping.setBeginIndex(0); // 如果为负数,设置为默认起始索引0
            }
 
            PlcAddress created = plcAddressService.saveMapping(mapping);
            log.info("创建PLC地址映射配置成功,ID: {}, 项目标识: {}", created.getId(), created.getProjectId());
            return Result.success(created);
        } catch (Exception e) {
            log.error("创建PLC地址映射配置失败", e);
            return Result.error("创建PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 更新PLC地址映射配置
     */
    @PutMapping("/update")
    @ApiOperation("更新PLC地址映射配置")
    public Result<PlcAddress> updateMapping(
            @ApiParam("配置ID") @RequestParam Long id,
            @ApiParam("更新的PLC地址映射配置") @RequestBody PlcAddress mapping) {
        try {
            // 参数验证
            if (mapping.getProjectId() == null || mapping.getProjectId().trim().isEmpty()) {
                return Result.error("项目标识不能为空");
            }
            if (mapping.getDbArea() == null || mapping.getDbArea().trim().isEmpty()) {
                return Result.error("DB块地址不能为空");
            }
 
            mapping.setId(id);
            PlcAddress updated = plcAddressService.updateMapping(mapping);
            if (updated != null) {
                log.info("更新PLC地址映射配置成功,ID: {}, 项目标识: {}", updated.getId(), updated.getProjectId());
                return Result.success(updated);
            } else {
                return Result.error("未找到ID为 " + id + " 的PLC地址映射配置");
            }
        } catch (Exception e) {
            log.error("更新PLC地址映射配置失败,ID: " + id, e);
            return Result.error("更新PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 根据项目ID更新配置
     * 对应前端:updateConfig
     */
    @PutMapping("/project/update")
    @ApiOperation("根据项目ID更新配置")
    public Result<PlcAddress> updateMappingByProjectId(
            @ApiParam("项目标识") @RequestParam String projectId,
            @ApiParam("更新的PLC地址映射配置") @RequestBody PlcAddress mapping) {
        try {
            // 参数验证
            if (mapping.getDbArea() == null || mapping.getDbArea().trim().isEmpty()) {
                return Result.error("DB块地址不能为空");
            }
 
            // 设置项目ID
            mapping.setProjectId(projectId);
            
            // 查找现有配置
            PlcAddress existing = plcAddressService.getMappingByProjectId(projectId);
            if (existing != null) {
                // 更新现有配置
                mapping.setId(existing.getId());
                PlcAddress updated = plcAddressService.updateMapping(mapping);
                log.info("根据项目ID更新PLC地址映射配置成功,项目标识: {}", projectId);
                return Result.success(updated);
            } else {
                // 创建新配置
                PlcAddress created = plcAddressService.saveMapping(mapping);
                log.info("根据项目ID创建PLC地址映射配置成功,项目标识: {}", projectId);
                return Result.success(created);
            }
        } catch (Exception e) {
            log.error("根据项目ID更新PLC地址映射配置失败,项目标识: " + projectId, e);
            return Result.error("更新配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 删除PLC地址映射配置
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除PLC地址映射配置")
    public Result<Void> deleteMapping(
            @ApiParam("配置ID") @RequestParam Long id) {
        try {
            boolean deleted = plcAddressService.deleteMapping(id);
            if (deleted) {
                log.info("删除PLC地址映射配置成功,ID: {}", id);
                return Result.success();
            } else {
                return Result.error("未找到ID为 " + id + " 的PLC地址映射配置");
            }
        } catch (Exception e) {
            log.error("删除PLC地址映射配置失败,ID: " + id, e);
            return Result.error("删除PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 批量删除PLC地址映射配置
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除PLC地址映射配置")
    public Result<Void> deleteMappings(
            @ApiParam("配置ID列表") @RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("ID列表不能为空");
            }
            
            int deletedCount = plcAddressService.deleteMappings(ids);
            log.info("批量删除PLC地址映射配置成功,删除数量: {}, ID列表: {}", deletedCount, ids);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除PLC地址映射配置失败,ID列表: " + ids, e);
            return Result.error("批量删除PLC地址映射配置失败: " + e.getMessage());
        }
    }
 
    /**
     * 测试PLC连接
     */
    @PostMapping("/test-connection")
    @ApiOperation("测试PLC连接")
    public Result<String> testConnection(
            @ApiParam("配置ID") @RequestParam Long id) {
        try {
            PlcAddress mapping = plcAddressService.getMappingById(id);
            if (mapping == null) {
                return Result.error("未找到ID为 " + id + " 的PLC地址映射配置");
            }
 
            boolean isConnected = plcAddressService.testConnection(mapping);
            if (isConnected) {
                String message = String.format("PLC连接测试成功 - 项目: %s, IP: %s, DB块: %s", 
                        mapping.getProjectId(), mapping.getPlcIp(), mapping.getDbArea());
                log.info(message);
                return Result.success(message);
            } else {
                String message = String.format("PLC连接测试失败 - 项目: %s, IP: %s, DB块: %s", 
                        mapping.getProjectId(), mapping.getPlcIp(), mapping.getDbArea());
                log.warn(message);
                return Result.error(message);
            }
        } catch (Exception e) {
            log.error("PLC连接测试失败,ID: " + id, e);
            return Result.error("PLC连接测试失败: " + e.getMessage());
        }
    }
 
    /**
     * 重新加载配置文件中的地址映射
     */
    @PostMapping("/reload-config")
    @ApiOperation("重新加载配置文件中的地址映射")
    public Result<String> reloadConfig() {
        try {
            plcAddressService.reloadConfigMappings();
            String message = "配置文件中的PLC地址映射已重新加载";
            log.info(message);
            return Result.success(message);
        } catch (Exception e) {
            log.error("重新加载配置文件失败", e);
            return Result.error("重新加载配置文件失败: " + e.getMessage());
        }
    }
}