/*
|
* 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 org.springblade.core.secure.utils.AuthUtil;
|
import org.springblade.core.tool.utils.StringUtil;
|
import org.sxkj.common.utils.HeaderUtils;
|
import org.sxkj.fw.area.dto.FwDefenseZoneDTO;
|
import org.sxkj.fw.area.entity.FwAreaDivideEntity;
|
import org.sxkj.fw.area.entity.FwDefenseZoneEntity;
|
import org.sxkj.fw.area.vo.FwDefenseZoneVO;
|
import org.sxkj.fw.area.excel.FwDefenseZoneExcel;
|
import org.sxkj.fw.area.mapper.FwDefenseZoneMapper;
|
import org.sxkj.fw.area.service.IFwAreaDivideService;
|
import org.sxkj.fw.area.service.IFwDefenseZoneService;
|
import org.springframework.stereotype.Service;
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import org.springblade.core.mp.base.BaseServiceImpl;
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.Date;
|
|
/**
|
* 防区管理表 服务实现类
|
*
|
* @author lw
|
* @since 2026-01-08
|
*/
|
@Service
|
public class FwDefenseZoneServiceImpl extends BaseServiceImpl<FwDefenseZoneMapper, FwDefenseZoneEntity> implements IFwDefenseZoneService {
|
|
@Resource
|
private IFwAreaDivideService fwAreaDivideService;
|
|
@Override
|
public IPage<FwDefenseZoneVO> selectFwDefenseZonePage(IPage<FwDefenseZoneVO> page, FwDefenseZoneDTO fwDefenseZone) {
|
IPage<FwDefenseZoneVO> result = page.setRecords(baseMapper.selectFwDefenseZonePage(page, fwDefenseZone));
|
fillDefenseZoneStatistics(result.getRecords());
|
return result;
|
}
|
|
@Override
|
public FwDefenseZoneVO selectFwDefenseZoneDetail(Long id) {
|
FwDefenseZoneVO detail = baseMapper.selectFwDefenseZoneDetail(id);
|
if (detail != null) {
|
fillDefenseZoneStatistics(Collections.singletonList(detail));
|
}
|
return detail;
|
}
|
|
private void fillDefenseZoneStatistics(List<FwDefenseZoneVO> records) {
|
if (records == null || records.isEmpty()) {
|
return;
|
}
|
Set<Long> sceneIdSet = new HashSet<>();
|
Set<Long> areaIdSet = new HashSet<>();
|
Map<Long, List<Long>> zoneSceneMap = new HashMap<>();
|
Map<Long, List<Long>> zoneAreaMap = new HashMap<>();
|
for (FwDefenseZoneVO record : records) {
|
List<Long> sceneIds = parseIdList(record.getDefenseSceneIds());
|
if (record.getId() != null) {
|
zoneSceneMap.put(record.getId(), sceneIds);
|
}
|
sceneIdSet.addAll(sceneIds);
|
List<Long> areaIds = parseIdList(record.getAreaDivideIds());
|
if (record.getId() != null) {
|
zoneAreaMap.put(record.getId(), areaIds);
|
}
|
areaIdSet.addAll(areaIds);
|
}
|
if (sceneIdSet.isEmpty() && areaIdSet.isEmpty()) {
|
for (FwDefenseZoneVO record : records) {
|
record.setSceneCount(0);
|
record.setDeviceCount(0);
|
record.setAreaCount(0);
|
record.setZoneArea(BigDecimal.ZERO);
|
}
|
return;
|
}
|
Map<Long, BigDecimal> areaSizeMap = new HashMap<>();
|
Map<Long, List<Long>> areaDeviceMap = new HashMap<>();
|
if (!areaIdSet.isEmpty()) {
|
List<FwAreaDivideEntity> areaList = fwAreaDivideService.listByIds(areaIdSet);
|
for (FwAreaDivideEntity area : areaList) {
|
if (area == null || area.getId() == null) {
|
continue;
|
}
|
areaSizeMap.put(area.getId(), area.getAreaSize());
|
areaDeviceMap.put(area.getId(), parseIdList(area.getDeviceIds()));
|
}
|
}
|
|
for (FwDefenseZoneVO record : records) {
|
List<Long> sceneIds = zoneSceneMap.get(record.getId());
|
if (sceneIds == null) {
|
sceneIds = Collections.emptyList();
|
}
|
Set<Long> uniqueSceneIds = new HashSet<>(sceneIds);
|
record.setSceneCount(uniqueSceneIds.size());
|
List<Long> zoneAreaIds = zoneAreaMap.get(record.getId());
|
if (zoneAreaIds == null) {
|
zoneAreaIds = Collections.emptyList();
|
}
|
Set<Long> areaIds = new HashSet<>(zoneAreaIds);
|
Set<Long> deviceIds = new HashSet<>();
|
for (Long areaId : areaIds) {
|
List<Long> areaDeviceIds = areaDeviceMap.get(areaId);
|
if (areaDeviceIds != null) {
|
deviceIds.addAll(areaDeviceIds);
|
}
|
}
|
record.setAreaCount(areaIds.size());
|
record.setDeviceCount(deviceIds.size());
|
record.setZoneArea(sumAreaSize(areaIds, areaSizeMap));
|
}
|
}
|
|
private List<Long> parseIdList(String ids) {
|
if (StringUtil.isBlank(ids)) {
|
return Collections.emptyList();
|
}
|
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;
|
}
|
|
private BigDecimal sumAreaSize(Set<Long> areaIds, Map<Long, BigDecimal> areaSizeMap) {
|
if (areaIds == null || areaIds.isEmpty()) {
|
return BigDecimal.ZERO;
|
}
|
BigDecimal total = BigDecimal.ZERO;
|
for (Long areaId : areaIds) {
|
BigDecimal areaSize = areaSizeMap.get(areaId);
|
if (areaSize != null) {
|
total = total.add(areaSize);
|
}
|
}
|
if (BigDecimal.ZERO.compareTo(total) == 0) {
|
return BigDecimal.ZERO;
|
}
|
return total.setScale(2, RoundingMode.HALF_UP);
|
}
|
|
|
@Override
|
public List<FwDefenseZoneExcel> exportFwDefenseZone(Wrapper<FwDefenseZoneEntity> queryWrapper) {
|
List<FwDefenseZoneExcel> fwDefenseZoneList = baseMapper.exportFwDefenseZone(queryWrapper);
|
//fwDefenseZoneList.forEach(fwDefenseZone -> {
|
// fwDefenseZone.setTypeName(DictCache.getValue(DictEnum.YES_NO, FwDefenseZone.getType()));
|
//});
|
return fwDefenseZoneList;
|
}
|
|
@Override
|
public boolean save(FwDefenseZoneEntity fwDefenseZone) {
|
fillCreateFields(fwDefenseZone);
|
return baseMapper.insertWithGeom(fwDefenseZone) > 0;
|
}
|
|
@Override
|
public boolean updateById(FwDefenseZoneEntity fwDefenseZone) {
|
fillUpdateFields(fwDefenseZone);
|
return baseMapper.updateWithGeomById(fwDefenseZone) > 0;
|
}
|
|
@Override
|
public boolean saveOrUpdate(FwDefenseZoneEntity fwDefenseZone) {
|
if (fwDefenseZone.getId() == null) {
|
fillCreateFields(fwDefenseZone);
|
return baseMapper.insertWithGeom(fwDefenseZone) > 0;
|
} else {
|
fillUpdateFields(fwDefenseZone);
|
return baseMapper.updateWithGeomById(fwDefenseZone) > 0;
|
}
|
}
|
|
private void fillCreateFields(FwDefenseZoneEntity fwDefenseZone) {
|
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 = HeaderUtils.getAreaCode();
|
// if (StringUtil.isBlank(areaCode)) {
|
// areaCode = fwDefenseZone.getAreaCode();
|
// }
|
Date now = new Date();
|
fwDefenseZone.setCreateUser(userId);
|
fwDefenseZone.setUpdateUser(userId);
|
if (deptId != null) {
|
fwDefenseZone.setCreateDept(deptId);
|
}
|
// if (StringUtil.isNotBlank(areaCode)) {
|
// fwDefenseZone.setAreaCode(areaCode);
|
// }
|
fwDefenseZone.setCreateTime(now);
|
fwDefenseZone.setUpdateTime(now);
|
}
|
|
private void fillUpdateFields(FwDefenseZoneEntity fwDefenseZone) {
|
Long userId = AuthUtil.getUserId();
|
if (userId != null) {
|
fwDefenseZone.setUpdateUser(userId);
|
}
|
fwDefenseZone.setUpdateTime(new Date());
|
}
|
|
}
|