吉安感知网项目-后端
rain
2026-01-29 5b23a8cb9294506d88330f0750f39b9a2cbabbdc
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
405
406
407
408
409
410
411
412
413
/*
 *      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.sxkj.fw.area.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.sxkj.fw.area.dto.FwAreaDivideDTO;
import org.sxkj.fw.area.dto.FwAreaDivideStatisticsDTO;
import org.sxkj.fw.area.entity.FwAreaDivideEntity;
import org.sxkj.fw.area.entity.FwAreaDivideExtEntity;
import org.sxkj.fw.area.excel.FwAreaDivideExcel;
import org.sxkj.fw.area.mapper.FwAreaDivideMapper;
import org.sxkj.fw.area.service.IFwAreaDivideExtService;
import org.sxkj.fw.area.service.IFwAreaDivideService;
import org.sxkj.fw.area.vo.FwAreaDivideExtVO;
import org.sxkj.fw.area.vo.FwAreaDivideStatisticsVO;
import org.sxkj.fw.area.vo.FwAreaDivideVO;
import org.sxkj.fw.area.vo.FwDefenseSceneVO;
import org.sxkj.system.cache.SysCache;
 
import java.util.*;
 
/**
 * 区域划分表 服务实现类
 *
 * @author lw
 * @since 2026-01-07
 */
@Service
@AllArgsConstructor
public class FwAreaDivideServiceImpl extends BaseServiceImpl<FwAreaDivideMapper, FwAreaDivideEntity>
    implements IFwAreaDivideService {
 
    private final IFwAreaDivideExtService fwAreaDivideExtService;
 
    @Override
    public IPage<FwAreaDivideVO> selectFwAreaDividePage(IPage<FwAreaDivideVO> page, FwAreaDivideDTO fwAreaDivide) {
        List<Long> deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        fwAreaDivide.setDeptList(deptList);
 
        List<FwAreaDivideVO> areaDivideList = baseMapper.selectFwAreaDividePage(page, fwAreaDivide);
 
        // 获取面数据
        if (areaDivideList != null && !areaDivideList.isEmpty()) {
            enrichWithExtensionData(areaDivideList);
        }
 
        return page.setRecords(areaDivideList);
    }
 
    @Override
    public List<FwAreaDivideVO> selectFwAreaDivideList(Integer filterSelected, Long sceneId) {
        List<FwAreaDivideVO> areaDivideList = baseMapper.selectFwAreaDivideList(filterSelected, sceneId);
        // 获取面数据
        if (areaDivideList != null && !areaDivideList.isEmpty()) {
            enrichWithExtensionData(areaDivideList);
        }
        return areaDivideList;
    }
 
    /**
     * 为区域划分列表填充扩展表数据
     *
     * @param areaDivideList 区域划分列表
     */
    private void enrichWithExtensionData(List<FwAreaDivideVO> areaDivideList) {
        if (areaDivideList == null || areaDivideList.isEmpty()) {
            return;
        }
 
        areaDivideList.stream()
            .filter(area -> area.getId() != null) // 过滤掉id为空的对象
            .forEach(area -> {
                List<FwAreaDivideExtVO> extList = fwAreaDivideExtService.getByAreaDivideId(area.getId());
                area.setFwAreaDivideExtList(extList);
            });
    }
 
    @Override
    public Map<Long, String> selectSceneNameByDeviceIds(List<Long> deviceIds) {
        Map<Long, String> result = new HashMap<>();
        if (deviceIds == null || deviceIds.isEmpty()) {
            return result;
        }
        Set<Long> deviceIdSet = new HashSet<>();
        for (Long deviceId : deviceIds) {
            if (deviceId != null) {
                deviceIdSet.add(deviceId);
            }
        }
        if (deviceIdSet.isEmpty()) {
            return result;
        }
        List<FwAreaDivideVO> areaList = baseMapper.selectFwAreaDivideListByDeviceIds(new ArrayList<>(deviceIdSet));
        if (areaList == null || areaList.isEmpty()) {
            for (Long deviceId : deviceIdSet) {
                result.put(deviceId, "/");
            }
            return result;
        }
 
        Map<Long, Set<Long>> deviceAreaMap = new HashMap<>();
        Set<Long> areaIdSet = new HashSet<>();
        for (FwAreaDivideVO area : areaList) {
            if (area == null || area.getId() == null) {
                continue;
            }
            List<Long> areaDeviceIds = parseIdList(area.getDeviceIds());
            if (areaDeviceIds.isEmpty()) {
                continue;
            }
            for (Long deviceId : areaDeviceIds) {
                if (!deviceIdSet.contains(deviceId)) {
                    continue;
                }
                deviceAreaMap.computeIfAbsent(deviceId, key -> new HashSet<>()).add(area.getId());
            }
            areaIdSet.add(area.getId());
        }
        if (areaIdSet.isEmpty()) {
            for (Long deviceId : deviceIdSet) {
                result.put(deviceId, "/");
            }
            return result;
        }
 
        List<FwDefenseSceneVO> sceneList = baseMapper.selectFwDefenseSceneListByAreaIds(new ArrayList<>(areaIdSet));
        Map<Long, Set<String>> areaSceneNameMap = new HashMap<>();
        if (sceneList != null && !sceneList.isEmpty()) {
            for (FwDefenseSceneVO scene : sceneList) {
                if (scene == null) {
                    continue;
                }
                String sceneName = scene.getSceneName();
                if (StringUtil.isBlank(sceneName)) {
                    continue;
                }
                List<Long> areaIds = parseIdList(scene.getAreaDivideIds());
                for (Long areaId : areaIds) {
                    if (!areaIdSet.contains(areaId)) {
                        continue;
                    }
                    areaSceneNameMap.computeIfAbsent(areaId, key -> new HashSet<>()).add(sceneName);
                }
            }
        }
 
        for (Long deviceId : deviceIdSet) {
            Set<Long> areaIds = deviceAreaMap.get(deviceId);
            if (areaIds == null || areaIds.isEmpty()) {
                result.put(deviceId, "/");
                continue;
            }
            Set<String> sceneNames = new HashSet<>();
            for (Long areaId : areaIds) {
                Set<String> names = areaSceneNameMap.get(areaId);
                if (names != null) {
                    sceneNames.addAll(names);
                }
            }
            if (sceneNames.isEmpty()) {
                result.put(deviceId, "/");
            } else {
                result.put(deviceId, String.join(",", sceneNames));
            }
        }
        return result;
    }
 
    @Override
    public boolean existsAreaByDeviceId(Long deviceId) {
        if (deviceId == null) {
            return false;
        }
        Integer count = baseMapper.selectAreaCountByDeviceId(deviceId);
        return count != null && count > 0;
    }
 
    @Override
    public IPage<FwAreaDivideStatisticsVO> selectFwAreaDivideStatisticsPage(IPage<FwAreaDivideStatisticsVO> page,
                                                                            FwAreaDivideStatisticsDTO fwAreaDivideStatistics) {
        List<Long> deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        fwAreaDivideStatistics.setDeptList(deptList);
        return page.setRecords(baseMapper.selectFwAreaDivideStatisticsPage(page, fwAreaDivideStatistics));
    }
 
    @Override
    public FwAreaDivideStatisticsVO selectFwAreaDivideStatisticsDetail(
        FwAreaDivideStatisticsDTO fwAreaDivideStatistics) {
        return baseMapper.selectFwAreaDivideStatisticsDetail(fwAreaDivideStatistics);
    }
 
    @Override
    public FwAreaDivideVO selectFwAreaDivideDetail(Long id) {
        FwAreaDivideVO detail = baseMapper.selectFwAreaDivideDetail(id);
        if (detail == null || detail.getId() == null) {
            return detail;
        }
        List<FwAreaDivideExtVO> extList = fwAreaDivideExtService.getByAreaDivideId(detail.getId());
        detail.setFwAreaDivideExtList(extList);
        return detail;
    }
 
    @Override
    public List<FwAreaDivideExcel> exportFwAreaDivide(Wrapper<FwAreaDivideEntity> queryWrapper) {
        List<FwAreaDivideExcel> fwAreaDivideList = baseMapper.exportFwAreaDivide(queryWrapper);
        // fwAreaDivideList.forEach(fwAreaDivide -> {
        // fwAreaDivide.setTypeName(DictCache.getValue(DictEnum.YES_NO,
        // FwAreaDivide.getType()));
        // });
        return fwAreaDivideList;
    }
 
    @Override
    public boolean save(FwAreaDivideEntity fwAreaDivide) {
        fillCreateFields(fwAreaDivide);
        return baseMapper.insertWithGeom(fwAreaDivide) > 0;
    }
 
    @Override
    public boolean updateById(FwAreaDivideEntity fwAreaDivide) {
        fillUpdateFields(fwAreaDivide);
        return baseMapper.updateWithGeomById(fwAreaDivide) > 0;
    }
 
    @Override
    public boolean saveOrUpdate(FwAreaDivideEntity fwAreaDivide) {
        if (fwAreaDivide.getId() == null) {
            fillCreateFields(fwAreaDivide);
            return baseMapper.insertWithGeom(fwAreaDivide) > 0;
        } else {
            fillUpdateFields(fwAreaDivide);
            return baseMapper.updateWithGeomById(fwAreaDivide) > 0;
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateWithExt(FwAreaDivideEntity fwAreaDivide, List<FwAreaDivideExtEntity> fwAreaDivideExtList) {
        // 主表与扩展面数据保持同一事务,扩展面列表为空时仅保存主表
        if (fwAreaDivide == null) {
            return false;
        }
        if (fwAreaDivideExtList != null && !fwAreaDivideExtList.isEmpty()) {
            String areaTypeKeys = buildAreaTypeKeys(fwAreaDivideExtList);
            if (StringUtil.isNotBlank(areaTypeKeys)) {
                fwAreaDivide.setAreaTypeKeys(areaTypeKeys);
            }
        }
        boolean isCreate = fwAreaDivide.getId() == null;
        boolean mainResult = saveOrUpdate(fwAreaDivide);
        if (!mainResult) {
            return false;
        }
        if (fwAreaDivideExtList == null || fwAreaDivideExtList.isEmpty()) {
            return true;
        }
        List<FwAreaDivideExtEntity> extList = new ArrayList<>();
        for (FwAreaDivideExtEntity ext : fwAreaDivideExtList) {
            if (ext == null) {
                continue;
            }
            ext.setAreaDivideId(fwAreaDivide.getId());
            if (StringUtil.isBlank(ext.getAreaCode()) && StringUtil.isNotBlank(fwAreaDivide.getAreaCode())) {
                ext.setAreaCode(fwAreaDivide.getAreaCode());
            }
            fillCreateFieldsExt(ext);
            extList.add(ext);
        }
        if (extList.isEmpty()) {
            return true;
        }
        if (!isCreate) {
            // 更新时先清理该主表下历史扩展面数据,再整体写入
            QueryWrapper<FwAreaDivideExtEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(FwAreaDivideExtEntity::getAreaDivideId, fwAreaDivide.getId());
            fwAreaDivideExtService.remove(wrapper);
        }
        if (!fwAreaDivideExtService.saveBatchWithGeom(extList)) {
            throw new RuntimeException("保存区域扩展面数据失败");
        }
        return true;
    }
 
    private void fillCreateFields(FwAreaDivideEntity fwAreaDivide) {
        Long userId = AuthUtil.getUserId();
        String deptIdStr = AuthUtil.getDeptId();
        Long deptId = null;
        if (StringUtil.isNotBlank(deptIdStr)) {
            try {
                deptId = Long.valueOf(deptIdStr);
            } catch (NumberFormatException ignored) {
                deptId = null;
            }
        }
        // String areaCode = fwAreaDivide.getAreaCode();
        // if (StringUtil.isBlank(areaCode)) {
        // areaCode = HeaderUtils.getAreaCode();
        // }
        Date now = new Date();
        fwAreaDivide.setCreateUser(userId);
        fwAreaDivide.setUpdateUser(userId);
        if (deptId != null) {
            fwAreaDivide.setCreateDept(deptId);
        }
        // if (StringUtil.isNotBlank(areaCode)) {
        // fwAreaDivide.setAreaCode(areaCode);
        // }
        fwAreaDivide.setCreateTime(now);
        fwAreaDivide.setUpdateTime(now);
    }
 
    private void fillUpdateFields(FwAreaDivideEntity fwAreaDivide) {
        Long userId = AuthUtil.getUserId();
        if (userId != null) {
            fwAreaDivide.setUpdateUser(userId);
        }
        fwAreaDivide.setUpdateTime(new Date());
    }
 
    private void fillCreateFieldsExt(FwAreaDivideExtEntity fwAreaDivideExt) {
        Long userId = AuthUtil.getUserId();
        String deptIdStr = AuthUtil.getDeptId();
        Long deptId = null;
        if (StringUtil.isNotBlank(deptIdStr)) {
            try {
                deptId = Long.valueOf(deptIdStr);
            } catch (NumberFormatException ignored) {
                deptId = null;
            }
        }
        Date now = new Date();
        if (fwAreaDivideExt.getCreateUser() == null) {
            fwAreaDivideExt.setCreateUser(userId);
        }
        if (fwAreaDivideExt.getUpdateUser() == null) {
            fwAreaDivideExt.setUpdateUser(userId);
        }
        if (fwAreaDivideExt.getCreateDept() == null && deptId != null) {
            fwAreaDivideExt.setCreateDept(deptId);
        }
        if (fwAreaDivideExt.getCreateTime() == null) {
            fwAreaDivideExt.setCreateTime(now);
        }
        if (fwAreaDivideExt.getUpdateTime() == null) {
            fwAreaDivideExt.setUpdateTime(now);
        }
    }
 
    private String buildAreaTypeKeys(List<FwAreaDivideExtEntity> fwAreaDivideExtList) {
        if (fwAreaDivideExtList == null || fwAreaDivideExtList.isEmpty()) {
            return null;
        }
        Set<String> keySet = new LinkedHashSet<>();
        for (FwAreaDivideExtEntity ext : fwAreaDivideExtList) {
            if (ext == null) {
                continue;
            }
            String key = ext.getAreaTypeKey();
            if (StringUtil.isNotBlank(key)) {
                keySet.add(key);
            }
        }
        if (keySet.isEmpty()) {
            return null;
        }
        return String.join(",", keySet);
    }
 
    private List<Long> parseIdList(String ids) {
        if (StringUtil.isBlank(ids)) {
            return new ArrayList<>();
        }
        String[] idArray = ids.split(",");
        List<Long> idList = new ArrayList<>(idArray.length);
        for (String item : idArray) {
            if (item == null) {
                continue;
            }
            String value = item.trim();
            if (StringUtil.isBlank(value)) {
                continue;
            }
            try {
                idList.add(Long.valueOf(value));
            } catch (NumberFormatException ignored) {
            }
        }
        return idList;
    }
 
}