|
|
@@ -0,0 +1,100 @@
|
|
|
+package com.zksy.drainage.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.zksy.base.enums.DistrictEnum;
|
|
|
+import com.zksy.base.video.VideoDeviceService;
|
|
|
+import com.zksy.common.exception.ServiceException;
|
|
|
+import com.zksy.common.utils.SecurityUtils;
|
|
|
+import com.zksy.drainage.domain.*;
|
|
|
+import com.zksy.drainage.mapper.*;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DrainageVideoService {
|
|
|
+ private final DrainageVideoMapper videos;
|
|
|
+ private final WaterloggingPointMapper points;
|
|
|
+ private final DrainageBindingLockMapper guard;
|
|
|
+ private final DrainageEquipmentBindingService binding;
|
|
|
+ private final VideoDeviceService devices;
|
|
|
+ public DrainageVideoService(DrainageVideoMapper videos,WaterloggingPointMapper points,DrainageBindingLockMapper guard,
|
|
|
+ DrainageEquipmentBindingService binding,VideoDeviceService devices) {
|
|
|
+ this.videos=videos;this.points=points;this.guard=guard;this.binding=binding;this.devices=devices;
|
|
|
+ }
|
|
|
+ public Page<DrainageVideoResource> page(int pageNum,int pageSize,String query,Integer district,String pointId,
|
|
|
+ Boolean unbound,Boolean favoriteOnly,String sourceType) {
|
|
|
+ if(district!=null&&!DistrictEnum.isValid(district))throw new ServiceException("无效设备片区");
|
|
|
+ Page<DrainageVideoResource> page=videos.page(new Page<>(Math.max(1,pageNum),Math.max(1,Math.min(100,pageSize))),
|
|
|
+ SecurityUtils.getUserId(),query,district,pointId,unbound,favoriteOnly,sourceType);
|
|
|
+ page.getRecords().forEach(this::describe);return page;
|
|
|
+ }
|
|
|
+ public List<DrainageVideoResource> all() {
|
|
|
+ List<DrainageVideoResource> rows=videos.all(SecurityUtils.getUserId());rows.forEach(this::describe);return rows;
|
|
|
+ }
|
|
|
+ private void describe(DrainageVideoResource row) {
|
|
|
+ row.setDistrictName(Optional.ofNullable(DistrictEnum.getDescByCode(row.getDistrict())).orElse("未设置片区"));
|
|
|
+ row.setTypeName("GB28181".equals(row.getSourceType())?"国标视频":"地址视频");
|
|
|
+ }
|
|
|
+ public List<Map<String,Object>> points() {return videos.points();}
|
|
|
+ public List<Map<String,Object>> tree() {
|
|
|
+ List<DrainageVideoResource> rows=all();
|
|
|
+ List<Map<String,Object>> roots=new ArrayList<>();
|
|
|
+ Map<String,Object> favorites=node("favorites","我的收藏",rows.stream().filter(r->Boolean.TRUE.equals(r.getFavorite())).collect(Collectors.toList()));
|
|
|
+ favorites.put("children",leaves("favorites",rows.stream().filter(r->Boolean.TRUE.equals(r.getFavorite())).collect(Collectors.toList())));
|
|
|
+ favorites.put("favoriteOnly",true);roots.add(favorites);
|
|
|
+ Map<String,List<DrainageVideoResource>> districts=rows.stream().collect(Collectors.groupingBy(
|
|
|
+ r->r.getDistrict()==null?"unknown":r.getDistrict().toString(),LinkedHashMap::new,Collectors.toList()));
|
|
|
+ districts.forEach((key,list)->{
|
|
|
+ Map<String,Object> root=node("district-"+key,list.get(0).getDistrictName(),list);root.put("district",list.get(0).getDistrict());
|
|
|
+ Map<String,List<DrainageVideoResource>> groups=list.stream().collect(Collectors.groupingBy(
|
|
|
+ r->r.getPointId()==null?"unbound":r.getPointId(),LinkedHashMap::new,Collectors.toList()));
|
|
|
+ List<Map<String,Object>> children=new ArrayList<>();
|
|
|
+ groups.forEach((pointId,items)->{
|
|
|
+ Map<String,Object> child=node("point-"+key+"-"+pointId,items.get(0).getPointName()==null?"未关联易积水点":items.get(0).getPointName(),items);
|
|
|
+ child.put("pointId",items.get(0).getPointId());child.put("district",list.get(0).getDistrict());child.put("unbound","unbound".equals(pointId));
|
|
|
+ child.put("children",leaves(child.get("id").toString(),items));
|
|
|
+ children.add(child);
|
|
|
+ });root.put("children",children);roots.add(root);
|
|
|
+ });return roots;
|
|
|
+ }
|
|
|
+ private Map<String,Object> node(String id,String label,List<DrainageVideoResource> rows) {
|
|
|
+ Map<String,Object> result=new LinkedHashMap<>();result.put("id",id);result.put("label",label);result.put("count",rows.size());
|
|
|
+ result.put("videoIds",rows.stream().map(DrainageVideoResource::getVideoId).distinct().collect(Collectors.toList()));return result;
|
|
|
+ }
|
|
|
+ private List<Map<String,Object>> leaves(String parent,List<DrainageVideoResource> rows) {
|
|
|
+ return rows.stream().map(row->node("video-"+parent+"-"+row.getVideoId(),row.getDeviceName(),Collections.singletonList(row))).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ @Transactional
|
|
|
+ public void associate(String videoId,String pointId) {
|
|
|
+ if(guard.lock()==null)throw new ServiceException("设备关联事务锁未初始化,请执行迁移");
|
|
|
+ devices.one(videoId);
|
|
|
+ List<WaterloggingPoint> all=points.selectList(null);
|
|
|
+ WaterloggingPoint target=null;
|
|
|
+ if(pointId!=null&&!pointId.isBlank()) {
|
|
|
+ target=all.stream().filter(p->pointId.equals(p.getId())).findFirst().orElseThrow(()->new ServiceException("易积水点不存在"));
|
|
|
+ for(WaterloggingPoint point:all) {
|
|
|
+ if(!pointId.equals(point.getId())&&ids(point.getEquipmentId()).contains(videoId))
|
|
|
+ throw new ServiceException("视频已关联其他易积水点,请先解除关联");
|
|
|
+ }
|
|
|
+ Map<String,String> occupied=binding.findBoundEquipment(DrainageEquipmentBindingService.TYPE_WATERLOGGING,pointId);
|
|
|
+ if(occupied.containsKey(videoId))throw new ServiceException("视频已被其他位置关联:"+occupied.get(videoId));
|
|
|
+ Set<String> merged=ids(target.getEquipmentId());merged.add(videoId);
|
|
|
+ videos.updatePoint(pointId,String.join(",",merged));
|
|
|
+ } else {
|
|
|
+ for(WaterloggingPoint point:all) {
|
|
|
+ Set<String> existing=ids(point.getEquipmentId());
|
|
|
+ if(existing.remove(videoId))videos.updatePoint(point.getId(),String.join(",",existing));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @Transactional
|
|
|
+ public void favorite(String videoId,boolean enabled) {
|
|
|
+ devices.one(videoId);
|
|
|
+ if(enabled)videos.favorite(SecurityUtils.getUserId(),videoId);else videos.unfavorite(SecurityUtils.getUserId(),videoId);
|
|
|
+ }
|
|
|
+ static Set<String> ids(String value) {
|
|
|
+ return value==null?new LinkedHashSet<>():Arrays.stream(value.split(",")).map(String::trim).filter(s->!s.isEmpty()).collect(Collectors.toCollection(LinkedHashSet::new));
|
|
|
+ }
|
|
|
+}
|