南昌市物联网技防平台-后台
tangzy
2021-03-26 afda08833e460a40a2d4e509f9df9d809c6f8f31
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.jfpt.equipment.controller;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.omg.PortableInterceptor.INACTIVE;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.jfpt.catalog.entitly.catalog;
import org.springblade.jfpt.catalog.service.catalogService;
import org.springblade.jfpt.deploy.service.IDeployService;
import org.springblade.jfpt.equipment.entity.Equipment;
import org.springblade.jfpt.equipment.service.IEquipmentService;
import org.springblade.jfpt.equipment.vo.EquipmentVO;
import org.springblade.jfpt.equipment.vo.EquipmentVOS;
import org.springblade.jfpt.equipment.wrapper.EqWrapper;
import org.springblade.system.user.entity.User;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
 
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * 控制器
 *
 * @author BladeX
 * @since 2020-07-01
 */
@RestController
@AllArgsConstructor
@RequestMapping("equipment/equipment")
@Api(value = "", tags = "接口")
public class EquipmentController extends BladeController {
 
    private final IEquipmentService equipmentService;
    private final IDeployService iDeployService;
    private final org.springblade.jfpt.catalog.service.catalogService catalogService;
 
    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入equipment")
    public R<EquipmentVOS> detail(Equipment equipment, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        /*Equipment detail = equipmentService.getOne(Condition.getQueryWrapper(equipment));*/
 
        return R.data(equipmentService.selectInfo(equipment));
    }
 
    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入equipment")
    public R<IPage<Equipment>> list(Equipment equipment, Query query) {
        IPage<Equipment> pages = equipmentService.page(Condition.getPage(query), Condition.getQueryWrapper(equipment));
        return R.data(pages);
    }
 
    /**
     * 查询所有
     */
    @GetMapping("/listAll")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入equipment")
    public R<List<EquipmentVO>> listAll(Equipment equipment, Query query) throws ParseException {
        List<EquipmentVO> pages = equipmentService.listAll();
 
        for (int j = 0; j < pages.size(); j++) {
            String expireTime = pages.get(j).getExpireTime();
            //1.获取当前时间
            Date dd = new Date();
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String end = sim.format(dd);
            //把string类型转换为long类型的
            long st = sim.parse(expireTime).getTime();
            long en = sim.parse(end).getTime();
            //计算天数
            int day = (int) ((st - en) / 86400000);
            if (day <= 30) {
                pages.get(j).setType(1);
            } else {
                pages.get(j).setType(0);
            }
        }
 
 
        return R.data(pages);
    }
 
    /**
     * 自定义分页
     */
    @GetMapping("/pages")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入equipment")
    public R<IPage<EquipmentVO>> pages(EquipmentVO equipment, Query query, String pid, HttpServletResponse response) throws ParseException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");
 
        IPage<EquipmentVO> page = null;
        //清空全局参数
        childMenu = new ArrayList();
 
        if (pid == null || pid.equals("")) {
            //根据查询条件得到所有设备
            page = equipmentService.selectEquipmentPage(Condition.getPage(query), equipment, null);
        } else {
            List<Map<String, Object>> treedatas = catalogService.selectCatalogEqNUmber(null);
            //根据传入的id递归出该节点下所有的设备
            List<Map<String, Object>> childList = treeIdList(treedatas, pid);
 
            if (childList.size() > 0) {
                //根据查询条件得到所有设备
                page = equipmentService.selectEquipmentPage(Condition.getPage(query), equipment, childList);
            } else {
                List<Map<String, Object>> ces = catalogService.selectCatalogEqNUmber(pid);
                List list = new ArrayList();
                list.add(ces.get(0).get("eqid"));
                //根据查询条件得到所有设备
                page = equipmentService.selectEquipmentPage(Condition.getPage(query), equipment, list);
            }
        }
 
        if (page != null){
            for (int j = 0; j < page.getRecords().size(); j++) {
                String expireTime = page.getRecords().get(j).getExpireTime();
                //1.获取当前时间
                Date dd = new Date();
                SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String end = sim.format(dd);
                //把string类型转换为long类型的
 
                if (expireTime != null){
                    long st = sim.parse(expireTime).getTime();
                    long en = sim.parse(end).getTime();
                    //计算天数
                    int day = (int) ((st - en) / 86400000);
                    if (day <= 30) {
                        page.getRecords().get(j).setType(1);
                    } else {
                        page.getRecords().get(j).setType(0);
                    }
                }
            }
        }
 
        return R.data(page);
    }
 
 
    /**
     * 自定义分页(部门挂接)
     */
    @GetMapping("/pageDept")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入equipment")
    public R<IPage<EquipmentVO>> pageDept(EquipmentVO equipment, Query query, String pid, HttpServletResponse response) throws ParseException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");
 
        IPage<EquipmentVO> page = equipmentService.selectDeptPages(Condition.getPage(query), equipment, pid);
 
        for (int j = 0; j < page.getRecords().size(); j++) {
            String expireTime = page.getRecords().get(j).getExpireTime();
            //1.获取当前时间
            Date dd = new Date();
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String end = sim.format(dd);
            //把string类型转换为long类型的
 
            if (expireTime != null){
                long st = sim.parse(expireTime).getTime();
                long en = sim.parse(end).getTime();
                //计算天数
                int day = (int) ((st - en) / 86400000);
                if (day <= 30) {
                    page.getRecords().get(j).setType(1);
                } else {
                    page.getRecords().get(j).setType(0);
                }
            }
        }
 
        return R.data(page);
    }
 
 
    //子节点
    private static List childMenu = new ArrayList();
 
    public static List treeMenuList(List<Map<String, Object>> menuList, String pid) {
        for (int i = 0; i < menuList.size(); i++) {
            //遍历出父id等于参数的id,add进子节点集合
 
            if (!pid.equals(0)) {
                if (menuList.get(i).get("pId").toString().equals(pid)) {
                    //递归遍历下一级
                    treeMenuList(menuList, menuList.get(i).get("id").toString());
                    childMenu.add(menuList.get(i));
                }
            }
 
        }
        return childMenu;
    }
 
    public static List treeIdList(List<Map<String, Object>> menuList, String pid) {
        for (int i = 0; i < menuList.size(); i++) {
            //遍历出父id等于参数的id,add进子节点集合
 
            if (!pid.equals(0)) {
                if (menuList.get(i).get("pId").toString().equals(pid)) {
                    //递归遍历下一级
                    treeIdList(menuList, menuList.get(i).get("id").toString());
                    if (menuList.get(i).get("eqid") != null && !menuList.get(i).get("eqid").equals("")) {
                        childMenu.add(menuList.get(i).get("eqid"));
                    }
                }
            }
 
        }
        return childMenu;
 
    }
 
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入equipment")
    public R save(@Valid @RequestBody EquipmentVOS equipment) {
        if (equipment.getParentId() == null && equipment.getId() == null) {
            equipment.setParentId(BladeConstant.TOP_PARENT_ID);
        }
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        equipment.setStime(df.format(day));
        equipmentService.s(equipment);
        String deviceNumber = equipment.getDeviceNumber();
        String deviceName = equipment.getDeviceName();
        String bType = "0";
        iDeployService.insterb(deviceNumber, deviceName, bType);
        return R.success("成功");
    }
 
    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入equipment")
    public R update(@Valid @RequestBody EquipmentVOS equipment) {
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        equipment.setStime(df.format(day));
        equipmentService.updateOne(equipment);
        return R.success("修改成功");
    }
 
    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入equipment")
    public R submit(@Valid @RequestBody EquipmentVO equipment) {
        if (equipment.getStime() != null && equipment.getStime().equals("")) {
            equipment.setStime(null);
        }
 
        if (equipment.getExpireTime() != null && equipment.getExpireTime().equals("")) {
            equipment.setExpireTime(null);
        }
 
        if (equipment.getHeartbeat() != null && equipment.getHeartbeat().equals("")) {
            equipment.setHeartbeat(null);
        }
 
//        String deviceName = equipment.getDeviceName();
//        String deviceNumber = equipment.getDeviceNumber();
//        String pId = equipment.getPid();
//        if (equipment.getId() == null){
//            catalogService.addCatalog(deviceName,deviceNumber,pId);
//        }else{
//            catalogService.updateCatalog(deviceName,deviceNumber,pId);
//        }
 
        System.out.println(equipment.getDeptId());
        return R.status(equipmentService.saveOrUpdate(equipment));
    }
 
    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(equipmentService.removeByIds(Func.toLongList(ids)));
    }
 
    /**
     * 获取树形结构
     */
    @GetMapping("/tree")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "树形结构", notes = "树形结构")
    public R<List<EquipmentVOS>> tree() {
        List<EquipmentVOS> tree = equipmentService.tree();
        return R.data(tree);
    }
 
 
    /**
     * 懒加载列表
     */
    @GetMapping("/lazy-list")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "fullName", value = "部门全称", paramType = "query", dataType = "string")
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "懒加载列表", notes = "传入dept")
    public R<List<EquipmentVOS>> lazyList(@ApiIgnore @RequestParam Map<String, Object> dept, Long parentId) {
        List<EquipmentVOS> list = equipmentService.lazyList(parentId, dept);
        return R.data(EqWrapper.build().listNodeLazyVO(list));
    }
 
 
    /**
     * 设备列表
     *
     * @param deviceType 设备类型
     * @return
     */
    @GetMapping("/selectList")
    public R<List<EquipmentVOS>> selectList(String deviceType, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        List<EquipmentVOS> list = equipmentService.selectList(deviceType);
        return R.data(list);
    }
 
    /**
     * 修改
     */
    @PostMapping("/updateexpireTime")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入equipment")
    public R updateexpireTime(String expireTime,String deviceNumber) {
        equipmentService.updateexpireTime(expireTime,deviceNumber);
        return R.success("修改成功");
    }
 
    /**
     * 设备故障修改
     */
    @PostMapping("/updateExstate")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入equipment")
    public R updateExstate(String state,String reason, String deviceNumber) {
        equipmentService.updateExstate(state,reason,deviceNumber);
        return R.success("修改成功");
    }
 
}