zxl
2026-03-20 a0b2af0203b00e1dfcb8b5977dd5019491533c26
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
package com.tievd.jyz.controller;
 
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tievd.cube.commons.annotations.AutoLog;
import com.tievd.cube.commons.annotations.DictApi;
import com.tievd.cube.commons.base.CubeController;
import com.tievd.cube.commons.base.Result;
import com.tievd.cube.commons.constant.CommonConst;
import com.tievd.cube.commons.mybatisplus.QueryGenerator;
import com.tievd.cube.commons.utils.SystemContextUtil;
import com.tievd.cube.modules.system.model.LoginUser;
import com.tievd.cube.modules.system.model.SysDepartTreeModel;
import com.tievd.cube.modules.system.service.ISysDepartService;
import com.tievd.jyz.cache.DeviceCache;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.dto.DeviceDTO;
import com.tievd.jyz.entity.Device;
import com.tievd.jyz.handler.BaseDataSyncHandler;
import com.tievd.jyz.handler.DeviceStatusHandler;
import com.tievd.jyz.service.IDeviceService;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
/**
 * Device
 *
 * @author cube
 * @version V2.0.0
 * @since 2023-02-27
 */
@Slf4j
@DictApi
@RestController
@RequestMapping("/jyz/device")
public class DeviceController extends CubeController<Device, IDeviceService> {
 
    @Autowired
    private IDeviceService deviceService;
 
    @Autowired
    private DeviceCache deviceCache;
 
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private BaseDataSyncHandler baseDataSyncHandler;
 
    /**
     * 分页列表查询
     */
    @GetMapping("/list")
    public Result<?> queryPageList(Device device,
                                   @RequestParam(defaultValue = "1") Integer pageNo,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Device> queryWrapper = QueryGenerator.initQueryWrapper(device, req.getParameterMap());
        Page<Device> page = new Page<>(pageNo, pageSize);
        IPage<Device> pageList = deviceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
 
    @GetMapping("/tables")
    public Result<?> tables(Device device,
                            @RequestParam(defaultValue = "1") Integer pageNo,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false) String searchKey,
                            @RequestParam(required = false) String parentCode,
                            @RequestParam(required = false) Byte status) {
 
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(searchKey)) {
            queryWrapper.like("name", searchKey);
        }
        if (StringUtils.isNotBlank(parentCode)) {
            queryWrapper.likeRight("org_code", parentCode);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        Page<Device> page = new Page<>(pageNo, pageSize);
        IPage<DeviceDTO> pageList = deviceService.tables(page, queryWrapper);
        Map<String, String> cameraStatusMap = DeviceStatusHandler.getCameraStatusMap();
        for (DeviceDTO deviceDTO : pageList.getRecords()) {
            String sn = deviceDTO.getSn();
            int onlineCount = 0;
            for (Map.Entry<String, String> entry : cameraStatusMap.entrySet()) {
                if (entry.getKey().contains(sn)) {
                    if (NumberUtil.parseInt(entry.getValue()) == SystemConstant.DEVICE_ONLINE) {
                        onlineCount++;
                    }
                }
            }
            deviceDTO.setCameraOnlineCount(onlineCount);
        }
        return Result.ok(pageList);
    }
 
    @GetMapping("/allList")
    public Result<?> allList(Device device, HttpServletRequest req) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>(device);
        List<Device> deviceList = deviceService.list(queryWrapper);
        return Result.ok(deviceList);
    }
 
    /**
     * 添加
     */
    @AutoLog("Device-添加")
    @PostMapping("/add")
    public Result<?> add(@RequestBody Device device) {
        device.setStatus(SystemConstant.DEVICE_OFFLINE);
        deviceService.save(device);
        deviceCache.init();
        return Result.ok();
    }
 
    /**
     * 同步网关基础数据
     * @param deviceSn 网关序列号
     * @return
     */
    @AutoLog("拉取/同步网关基础数据")
    @GetMapping("/syncData")
    public Result<?> syncData(@RequestParam String deviceSn) {
        //同步网关基础数据
        baseDataSyncHandler.pullData(SystemConstant.ALL_REQ_SOURCE,deviceSn);
        return Result.ok();
    }
 
    /**
     * 编辑
     */
    @AutoLog("Device-编辑")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody Device device) {
        deviceService.updateById(device);
        deviceCache.init();
        return Result.ok();
    }
 
    /**
     * 通过id删除
     */
    @AutoLog("Device-通过id删除")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id) {
        deviceService.deleteById(id);
        deviceCache.init();
        return Result.ok();
    }
 
    /**
     * 通过id查询
     */
    @GetMapping("/queryById")
    public Result<?> queryById(@RequestParam String id) {
        Device device = deviceService.getById(id);
        return Result.ok(device);
    }
 
    /**
     * 导出excel
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletRequest request, HttpServletResponse response, Device device) throws IOException {
        super.exportXls(request, response, device, "Device");
    }
 
    /**
     * 通过excel导入数据
     */
    @PostMapping("/importExcel")
    public Result<?> importExcel(HttpServletRequest request) throws Exception {
        Result<?> result = super.importExcel(request, Device.class);
        deviceCache.init();
        return result;
    }
 
    @GetMapping("/queryTreeList")
    public Result<?> queryTreeList(@RequestParam(required = false) boolean myself) {
        List<SysDepartTreeModel> list;
        if (myself) {
            LoginUser user = SystemContextUtil.currentLoginUser();
            list = sysDepartService.queryMyDeptTreeList(user.getDepartIds());
        } else {
            list = sysDepartService.queryTreeList();
        }
        deviceService.assembleDevice(list);
        return Result.ok(list);
    }
 
    @GetMapping("/searchBy")
    public Result<List<SysDepartTreeModel>> searchBy(@Parameter(name = "关键词") String keyWord, @Parameter(name = "我的部门下搜索") @RequestParam(required = false) String myDeptSearch) {
        //部门查询,myDeptSearch为1时为我的部门查询,登录用户为上级时查只查负责部门下数据
        LoginUser user = SystemContextUtil.currentLoginUser();
        String departIds = null;
        if (user.getUserIdentity() != null && user.getUserIdentity().equals(CommonConst.USER_IDENTITY_2)) {
            departIds = user.getDepartIds();
        }
        List<SysDepartTreeModel> treeList = sysDepartService.searchBy(keyWord, myDeptSearch, departIds);
        if (treeList == null || treeList.size() == 0) {
            return Result.error("未查询匹配数据!");
        }
        deviceService.assembleDevice(treeList);
        return Result.ok(treeList);
    }
}