jinpengyong
2023-09-22 3271a0e7421aa7c55f475be8f24ad85b576108b8
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.Device;
import com.moral.api.entity.GovMonitorPoint;
import com.moral.api.entity.Organization;
import com.moral.api.entity.SysArea;
import com.moral.api.exception.BusinessException;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.GovMonitorPointMapper;
import com.moral.api.mapper.SysAreaMapper;
import com.moral.api.service.DeviceService;
import com.moral.api.service.GovMonitorPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.RegionCodeUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-09-09
 */
@Service
public class GovMonitorPointServiceImpl extends ServiceImpl<GovMonitorPointMapper, GovMonitorPoint> implements GovMonitorPointService {
 
    @Autowired(required = false)
    private GovMonitorPointMapper govMonitorPointMapper;
 
    @Autowired(required = false)
    private SysAreaMapper sysAreaMapper;
 
    @Autowired(required = false)
    private RedisTemplate redisTemplate;
 
    @Resource
    private OrganizationService organizationService;
 
    @Resource
    private DeviceService deviceService;
 
    @Autowired(required = false)
    private DeviceMapper deviceMapper;
 
    /*
     * 从redis获取设备信息
     * */
    private Map<String, Object> getGovMonitorPointInfoFromRedis(String id) {
        return (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.GOV_MONITOR_POINT, id);
    }
 
    /*
     * 设备信息存入redis
     */
    private void setGovMonitorPointInfoToRedis(String id, GovMonitorPoint govMonitorPointInfo) {
        redisTemplate.opsForHash().put(RedisConstants.GOV_MONITOR_POINT, id, govMonitorPointInfo);
    }
 
    /*
     * 从redis删除设备信息
     */
    private void delGovMonitorPointInfoFromRedis(String id) {
        redisTemplate.opsForHash().delete(RedisConstants.GOV_MONITOR_POINT, id);
    }
 
    @Override
    public Map<String, Object> getDataByCondition(Map map) {
        Map<String, Object> resultMap = new HashMap<>();
        if (!map.containsKey("current") || !map.containsKey("size")) {
            resultMap.put("code", ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg", ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page<GovMonitorPoint> page = new Page<>(current, size);
        QueryWrapper<GovMonitorPoint> wrapper_Condition = new QueryWrapper<>();
        wrapper_Condition.eq("is_delete", Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(map.get("name"))) {
            wrapper_Condition.like("name", map.get("name").toString());
        }
        if (!ObjectUtils.isEmpty(map.get("stationLevel"))) {
            wrapper_Condition.like("station_level", map.get("stationLevel").toString());
        }
        wrapper_Condition.orderByDesc("create_time");
        Page resultPage = govMonitorPointMapper.selectPage(page, wrapper_Condition);
        int totalNumber = govMonitorPointMapper.selectCount(wrapper_Condition);
        List<GovMonitorPoint> govMonitorPoints = resultPage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String, Object>> govMonitorPointList = new ArrayList<>();
        for (GovMonitorPoint govMonitorPoint : govMonitorPoints) {
            Map govMonitorPointMap = JSON.parseObject(JSON.toJSONString(govMonitorPoint), Map.class);
            String createTime = SDF.format(govMonitorPoint.getCreateTime());
            String updateTime = SDF.format(govMonitorPoint.getUpdateTime());
            govMonitorPointMap.put("createTime", createTime);
            govMonitorPointMap.put("updateTime", updateTime);
            String position = "";
            if (!ObjectUtils.isEmpty(govMonitorPoint.getProvinceCode())) {
                QueryWrapper<SysArea> wrapper_province = new QueryWrapper<>();
                wrapper_province.eq("area_code", govMonitorPoint.getProvinceCode());
                SysArea sysArea_provice = sysAreaMapper.selectOne(wrapper_province);
                if (!ObjectUtils.isEmpty(sysArea_provice)) {
                    position = position + sysArea_provice.getAreaName();
                    if (!ObjectUtils.isEmpty(govMonitorPoint.getCityCode())) {
                        QueryWrapper<SysArea> wrapper_city = new QueryWrapper<>();
                        wrapper_city.eq("area_code", govMonitorPoint.getCityCode());
                        SysArea sysArea_city = sysAreaMapper.selectOne(wrapper_city);
                        if (!ObjectUtils.isEmpty(sysArea_city)) {
                            position = position + "/" + sysArea_city.getAreaName();
                            if (!ObjectUtils.isEmpty(govMonitorPoint.getAreaCode())) {
                                QueryWrapper<SysArea> wrapper_area = new QueryWrapper<>();
                                wrapper_area.eq("area_code", govMonitorPoint.getAreaCode());
                                SysArea sysArea_area = sysAreaMapper.selectOne(wrapper_area);
                                if (!ObjectUtils.isEmpty(sysArea_area)) {
                                    position = position + "/" + sysArea_area.getAreaName();
                                }
                            }
                        }
                    }
                }
            }
            govMonitorPointMap.put("position", position);
            govMonitorPointList.add(govMonitorPointMap);
        }
        resultMap.put("govMonitorPointList", govMonitorPointList);
        resultMap.put("totalNumber", totalNumber);
        resultMap.put("current", current);
        int totalPageNumber = totalNumber / size;
        if (totalNumber % size != 0) {
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber", totalPageNumber);
        return resultMap;
    }
 
    @Transactional
    @Override
    public void insert(GovMonitorPoint govMonitorPoint) {
        int count = govMonitorPointMapper.insert(govMonitorPoint);
        //数据插入redis
        setGovMonitorPointInfoToRedis(govMonitorPoint.getId().toString(), selectGovMonitorPointInfoById(govMonitorPoint.getId()));
        if (count > 0) {
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("添加了政府站点:").append("id:").append(govMonitorPoint.getId() + ";").append(govMonitorPoint.getName() + ";").append(":").append("guid:").append(govMonitorPoint.getGuid() + ";");
            LogUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        }
    }
 
    @Transactional
    @Override
    public void update(GovMonitorPoint govMonitorPoint) {
        Integer id = govMonitorPoint.getId();
        GovMonitorPoint oldGovMonitorPoint = govMonitorPointMapper.selectById(id);
        govMonitorPointMapper.updateById(govMonitorPoint);
        //删除redis
        delGovMonitorPointInfoFromRedis(id.toString());
        //更新redis
        setGovMonitorPointInfoToRedis(id.toString(), selectGovMonitorPointInfoById(id));
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了政府站点:").append("id:").append(govMonitorPoint.getId() + ";");
        if (!ObjectUtils.isEmpty(govMonitorPoint.getGuid())) {
            content.append("guid:").append(oldGovMonitorPoint.getGuid()).append("->").append(govMonitorPoint.getGuid()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getName())) {
            content.append("name:").append(oldGovMonitorPoint.getName()).append("->").append(govMonitorPoint.getName()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getLongitude())) {
            content.append("longitude:").append(oldGovMonitorPoint.getLongitude()).append("->").append(govMonitorPoint.getLongitude()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getLatitude())) {
            content.append("latitude:").append(oldGovMonitorPoint.getLatitude()).append("->").append(govMonitorPoint.getLatitude()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getProvinceCode())) {
            content.append("provinceCode:").append(oldGovMonitorPoint.getProvinceCode()).append("->").append(govMonitorPoint.getProvinceCode()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getCityCode())) {
            content.append("cityCode:").append(oldGovMonitorPoint.getCityCode()).append("->").append(govMonitorPoint.getCityCode()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getAreaCode())) {
            content.append("areaCode:").append(oldGovMonitorPoint.getAreaCode()).append("->").append(govMonitorPoint.getAreaCode()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getStationLevel())) {
            content.append("stationLevel:").append(oldGovMonitorPoint.getStationLevel()).append("->").append(govMonitorPoint.getStationLevel()).append(";");
        }
        if (!ObjectUtils.isEmpty(govMonitorPoint.getDesc())) {
            content.append("desc:").append(oldGovMonitorPoint).append("->").append(govMonitorPoint.getDesc()).append(";");
        }
        LogUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }
 
    @Override
    @Transactional
    public void updateList(Integer id, String guid) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        QueryWrapper<GovMonitorPoint> wrapper_govMonitorPoint = new QueryWrapper<>();
        wrapper_govMonitorPoint.eq("is_delete", Constants.NOT_DELETE);
        wrapper_govMonitorPoint.eq("id", id);
        List<GovMonitorPoint> govMonitorPoints = govMonitorPointMapper.selectList(wrapper_govMonitorPoint);
        if (govMonitorPoints.size() == 0) {
            throw new BusinessException("站点不存在!");
        }
        GovMonitorPoint govMonitorPoint = govMonitorPoints.get(0);
        String oldGuid = govMonitorPoint.getGuid();
        LambdaQueryChainWrapper<Device> wrapper = deviceService.lambdaQuery();
        wrapper.eq(Device::getIsDelete,0);
        wrapper.eq(Device::getGuid,oldGuid);
        List<Device> list = wrapper.list();
        list.forEach(it->it.setGuid(guid));
 
        deviceService.updateBatchById(list);
 
        govMonitorPoint.setGuid(guid);
        govMonitorPointMapper.updateById(govMonitorPoint);
        //删除redis
        delGovMonitorPointInfoFromRedis(id.toString());
        //更新redis
        setGovMonitorPointInfoToRedis(id.toString(), selectGovMonitorPointInfoById(id));
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了政府站点:").append("id:").append(govMonitorPoint.getId() + ";");
        if (!ObjectUtils.isEmpty(govMonitorPoint.getGuid())) {
            content.append("guid:").append(oldGuid).append("->").append(guid).append(";");
        }
        LogUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
        for(Device d : list){
            executorService.submit(()->{
                redisTemplate.opsForHash().delete(RedisConstants.DEVICE, d.getMac());
                Map<String, Object> deviceInfo = deviceService.selectDeviceInfoById(d.getId());
                redisTemplate.opsForHash().put(RedisConstants.DEVICE, d.getMac(), deviceInfo);
 
            });
        }
        // 关闭线程池
        executorService.shutdown();
    }
 
    @Override
    public void delete(Integer id) {
        UpdateWrapper<GovMonitorPoint> wrapper_delete = new UpdateWrapper<>();
        wrapper_delete.eq("id", id).set("is_delete", Constants.DELETE);
        govMonitorPointMapper.update(null, wrapper_delete);
        //删除redis
        delGovMonitorPointInfoFromRedis(id.toString());
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("删除了设备:").append("id:").append(id).append(";");
        LogUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
    }
 
    @Override
    public GovMonitorPoint selectGovMonitorPointInfoById(int id) {
        GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectById(id);
        return govMonitorPoint;
    }
 
    @Override
    public List<Map<String, Object>> selectGovMonitorPoints(String regionCode) {
        QueryWrapper<GovMonitorPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("guid", "name").eq("is_delete", Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(regionCode)) {
            String regionName = RegionCodeUtils.regionCodeConvertToName(Integer.parseInt(regionCode));
            queryWrapper.eq(regionName, regionCode);
        }
        return govMonitorPointMapper.selectMaps(queryWrapper);
    }
 
    @Override
    public List<GovMonitorPoint> selectGovMonitorPointsByOrgid(Map map) {
        //根据组织id获取子组织
        List<Organization> organizations = organizationService.getAllChildrenOrganization(Integer.parseInt(map.get("organization_id").toString()));
        Set<Integer> organization_ids = organizations.stream().map(organization -> organization.getId()).collect(Collectors.toSet());
        //先获取组织下所有设备
        QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
        wrapper_device.in("organization_id",organization_ids).eq("is_delete",Constants.NOT_DELETE);
        List<Device> devices = deviceMapper.selectList(wrapper_device);
        //用集合存放所有设备的id
        Set<String> guids = devices.stream().map(device -> device.getGuid()).collect(Collectors.toSet());
        //获取所有政府站点信息
        QueryWrapper<GovMonitorPoint> wrapper_govMonitorPoint = new QueryWrapper<>();
        wrapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE).in("guid",guids);
        List<GovMonitorPoint> govMonitorPointList = govMonitorPointMapper.selectList(wrapper_govMonitorPoint);
        return govMonitorPointList;
    }
}