瀏覽代碼

update content-service/disk CamController

reghao 6 月之前
父節點
當前提交
09dccba5f9

+ 25 - 17
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/controller/CamController.java

@@ -1,10 +1,11 @@
 package cn.reghao.tnb.content.app.disk.controller;
 
 import cn.reghao.jutil.jdk.result.Result;
-import cn.reghao.jutil.jdk.result.WebResult;
+import cn.reghao.jutil.web.WebResult;
 import cn.reghao.tnb.content.app.disk.model.dto.CamRecordDto;
+import cn.reghao.tnb.content.app.disk.model.vo.CamRecordDetail;
 import cn.reghao.tnb.content.app.disk.model.vo.CamRecordInfo;
-import cn.reghao.tnb.content.app.disk.model.vo.CamRecordVo;
+import cn.reghao.tnb.content.app.disk.model.vo.CamSearch;
 import cn.reghao.tnb.content.app.disk.model.vo.MarkDay;
 import cn.reghao.tnb.content.app.disk.service.CamRecordService;
 import cn.reghao.tnb.content.app.disk.service.CamService;
@@ -35,23 +36,30 @@ public class CamController {
         this.camRecordService = camRecordService;
     }
 
+    @Operation(summary = "摄像头 key-value 列表", description = "N")
+    @GetMapping("/kv")
+    public String getCamKeyValues() {
+        List<SelectOption> kvs = camService.getCamKeyValues();
+        return WebResult.success(kvs);
+    }
+
     @Operation(summary = "摄像头详情页面", description = "N")
     @GetMapping("/detail")
-    public String camListPage1(@RequestParam(value = "camId", required = false) Long camId,
-                               @RequestParam(value = "yearMonthDay", required = false) String yearMonthDay) {
-        List<SelectOption> kvs = camService.getCamList();
+    public String getCamDetail(CamSearch camSearch) {
+        List<SelectOption> kvs = camService.getCamKeyValues();
         if (kvs.isEmpty()) {
-            return WebResult.failWithMsg("no cam");
+            return WebResult.failWithMsg("No Cam");
         }
 
         String currentDayStr = LocalDate.now().toString();
+        String yearMonthDay = camSearch.getYearMonthDay();
         if (yearMonthDay != null) {
             currentDayStr = yearMonthDay;
         }
 
+        Long camId = camSearch.getCamId();
         if (camId == null) {
-            SelectOption selectOption = kvs.get(0);
-            camId = Long.parseLong(selectOption.getLabel());
+            camId = Long.parseLong(kvs.get(0).getLabel());
         }
 
         String currentMonthStr = currentDayStr.substring(0, currentDayStr.lastIndexOf("-"));
@@ -60,17 +68,17 @@ public class CamController {
         if (!dayRecords.isEmpty()) {
             CamRecordInfo camRecordInfo = dayRecords.get(0);
             long recordId = camRecordInfo.getRecordId();
-            CamRecordVo camRecordVo = camRecordService.getCamRecord(recordId);
-            if (camRecordVo != null) {
-                /*model.addAttribute("coverUrl", camRecordVo.getCoverUrl());
-                model.addAttribute("videoUrl", camRecordVo.getVideoUrl());*/
+            CamRecordDetail camRecordDetail = camRecordService.getCamRecord(recordId);
+            if (camRecordDetail != null) {
+                /*model.addAttribute("coverUrl", camRecordDetail.getCoverUrl());
+                model.addAttribute("videoUrl", camRecordDetail.getVideoUrl());*/
             }
         } else {
             String videoUrl = "";
-            CamRecordVo camRecordVo = camRecordService.getLatestRecord(camId);
-            if (camRecordVo != null) {
-                //videoUrl = camRecordVo.getVideoUrl();
-                /*model.addAttribute("coverUrl", camRecordVo.getCoverUrl());
+            CamRecordDetail camRecordDetail = camRecordService.getLatestRecord(camId);
+            if (camRecordDetail != null) {
+                //videoUrl = camRecordDetail.getVideoUrl();
+                /*model.addAttribute("coverUrl", camRecordDetail.getCoverUrl());
                 model.addAttribute("videoUrl", videoUrl);*/
             } else {
                 /*model.addAttribute("coverUrl", "");
@@ -103,7 +111,7 @@ public class CamController {
     @Operation(summary = "获取录像", description = "N")
     @GetMapping(value = "/record/url/{recordId}", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getCamRecord(@PathVariable("recordId") long recordId) {
-        CamRecordVo camRecord = camRecordService.getCamRecord(recordId);
+        CamRecordDetail camRecord = camRecordService.getCamRecord(recordId);
         if (camRecord != null) {
             return WebResult.success(camRecord);
         }

+ 1 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/db/mapper/CamRecordMapper.java

@@ -18,6 +18,7 @@ import java.util.List;
 public interface CamRecordMapper extends BaseMapper<CamRecord> {
     List<CamRecord> findByCamId(@Param("camId") long camId, @Param("page") Page page);
     List<RecordInfo> findByCamIdAndStartAt(@Param("camId") long camId, @Param("dayStr") String dayStr);
+    CamRecord findLatestRecord(long camId);
     CamRecord findByRecordId(long recordId);
     List<ResultCount> findGroupByYearMonth(@Param("camId") long camId, @Param("yearMonth") String yearMonth);
 }

+ 14 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/model/vo/CamDetail.java

@@ -0,0 +1,14 @@
+package cn.reghao.tnb.content.app.disk.model.vo;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-09-01 17:53:06
+ */
+public class CamDetail {
+    private long camId;
+    private String camName;
+    private String date;
+    private List<RecordInfo> recordInfoList;
+}

+ 2 - 3
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/model/vo/CamRecordVo.java → content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/model/vo/CamRecordDetail.java

@@ -11,10 +11,9 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 @AllArgsConstructor
 @Getter
-public class CamRecordVo {
+public class CamRecordDetail {
     private Long camId;
     private Long recordId;
     private String recordTime;
-    private String coverUrl;
-    private String videoUrl;
+    private CamUrl camUrl;
 }

+ 16 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/model/vo/CamSearch.java

@@ -0,0 +1,16 @@
+package cn.reghao.tnb.content.app.disk.model.vo;
+
+import lombok.Getter;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author reghao
+ * @date 2025-09-01 17:40:26
+ */
+@Getter
+public class CamSearch {
+    private Long camId;
+    private String yearMonth;
+    private String yearMonthDay;
+}

+ 4 - 4
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/model/vo/CamUrl.java

@@ -10,17 +10,17 @@ import lombok.Getter;
 public class CamUrl {
     private String urlType;
     private String videoUrl;
-    private boolean liveUrl;
+    private boolean live;
 
     public CamUrl(String videoUrl) {
         this.urlType = "mp4";
         this.videoUrl = videoUrl;
-        this.liveUrl = false;
+        this.live = false;
     }
 
-    public CamUrl(String videoUrl, boolean liveUrl) {
+    public CamUrl(String videoUrl, boolean live) {
         this.urlType = "flv";
         this.videoUrl = videoUrl;
-        this.liveUrl = liveUrl;
+        this.live = live;
     }
 }

+ 23 - 59
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/service/CamRecordService.java

@@ -5,7 +5,6 @@ import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.jutil.tool.id.SnowFlake;
-import cn.reghao.oss.sdk.model.dto.ObjectInfo;
 import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
 import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
 import cn.reghao.tnb.common.auth.UserContext;
@@ -20,7 +19,6 @@ import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Service;
 
 import java.time.LocalDateTime;
-import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -32,6 +30,7 @@ import java.util.stream.Collectors;
 public class CamRecordService {
     @DubboReference(check = false, timeout = 60_000)
     private OssService ossService;
+    private int channelCode = 107;
 
     private final SnowFlake idGenerator;
     private final CamDeviceMapper camDeviceMapper;
@@ -95,7 +94,7 @@ public class CamRecordService {
     }
 
     /**
-     * 一个天中的监控记录
+     * 获取某天的监控录像
      *
      * @param dayStr 2024-08-31 格式的字符串
      * @return
@@ -106,32 +105,13 @@ public class CamRecordService {
         return list.stream().map(CamRecordInfo::new).collect(Collectors.toList());
     }
 
-    public CamRecordVo getCamRecord(long recordId) {
-        try {
-            CamRecord camRecord = camRecordMapper.findByRecordId(recordId);
-            if (camRecord != null) {
-                String recordTime = DateTimeConverter.format(camRecord.getStartAt());
-                String coverUrl = camRecord.getCoverUrl();
-                String videoFileId = camRecord.getVideoFileId();
-                ObjectInfo diskFile = ossService.getObjectInfo(111, videoFileId);
-                if (diskFile != null) {
-                    long loginUser = UserContext.getUserId();
-                    /*long owner = diskFile.getOwner();
-                    if (loginUser == owner) {
-                        String videoUrl = String.format("/%s", diskFile.getObjectName());
-                        return new CamRecordVo(camRecord.getCamId(), recordId, recordTime, coverUrl, videoUrl);
-                    }*/
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return null;
+    public CamRecordDetail getCamRecord(long recordId) {
+        CamRecord camRecord = camRecordMapper.findByRecordId(recordId);
+        return camRecord == null ? null : getCamRecordDetail(camRecord);
     }
 
     /**
-     * 一个月中的哪些天有监控记录
+     * 某月中的哪些天有监控录像
      *
      * @param yearMonth 2024-08 格式的字符串
      * @return
@@ -146,51 +126,35 @@ public class CamRecordService {
                 .collect(Collectors.toList());
     }
 
-    private List<String> findByYearMonth(long camId, String yearMonth) {
-        /*Specification<CamRecord> specification = (root, query, cb) -> {
-            Predicate fp = cb.conjunction();
-            Predicate p1 = cb.equal(root.get("camId").as(Long.class), camId);
-            fp = cb.and(fp, p1);
-            return query.where(fp).groupBy(root.get("startAt")).getRestriction();
-        };
-        camRecordMapper.findAll(specification);*/
-        return Collections.emptyList();
-    }
-
-    @Deprecated
-    public CamRecordVo getLatestRecord(long camId) {
+    public CamRecordDetail getLatestRecord(long camId) {
+        long loginUser = UserContext.getUserId();
         CamDeviceQuery camDeviceQuery = new CamDeviceQuery.Builder()
                 .camId(camId)
+                .owner(loginUser)
                 .build();
-        Page page = new Page(1, 10);
+        Page page = new Page(1, 1);
         List<CamDevice> camDeviceList = camDeviceMapper.findCamDeviceQueryByPage(page, camDeviceQuery);
         CamDevice camDevice = camDeviceList.get(0);
         if (camDevice.getState()) {
-            String videoUrl = camDevice.getPullUrl();
-            CamUrl camUrl = new CamUrl(videoUrl, true);
+            String liveUrl = camDevice.getPullUrl();
+            CamUrl camUrl = new CamUrl(liveUrl, true);
         }
 
-        //PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "createTime"));
-        List<CamRecord> camRecordList = camRecordMapper.findByCamId(camId, page);
-        if (camRecordList.isEmpty()) {
-            return null;
-        }
+        CamRecord camRecord = camRecordMapper.findLatestRecord(camId);
+        return camRecord == null ? null : getCamRecordDetail(camRecord);
+    }
 
-        CamRecord camRecord = camRecordList.get(0);
+    private CamRecordDetail getCamRecordDetail(CamRecord camRecord) {
         String recordTime = DateTimeConverter.format(camRecord.getStartAt());
         long recordId = camRecord.getRecordId();
-        String coverUrl = camRecord.getCoverUrl();
         String videoFileId = camRecord.getVideoFileId();
-
-        /*ObjectInfo diskFile = ossService.getObjectInfo(111, videoFileId);
-        if (diskFile != null) {
-            long loginUser = UserContext.getUserId();
-            long owner = diskFile.getOwner();
-            if (loginUser == owner) {
-                String videoUrl = String.format("/%s", diskFile.getObjectName());
-                return new CamRecordVo(camRecord.getCamId(), recordId, recordTime, coverUrl, videoUrl);
-            }
-        }*/
+        try {
+            String signedUrl = ossService.getSignedUrlByUrl(channelCode, videoFileId);
+            CamUrl camUrl = new CamUrl(signedUrl);
+            return new CamRecordDetail(camRecord.getCamId(), recordId, recordTime, camUrl);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
 
         return null;
     }

+ 4 - 6
content/content-service/src/main/java/cn/reghao/tnb/content/app/disk/service/CamService.java

@@ -13,7 +13,7 @@ import cn.reghao.tnb.content.app.disk.model.po.CamDevice;
 import cn.reghao.tnb.content.app.disk.model.po.CamRecord;
 import cn.reghao.tnb.content.app.disk.model.po.UserActivity;
 import cn.reghao.tnb.content.app.disk.model.query.CamDeviceQuery;
-import cn.reghao.tnb.content.app.disk.model.vo.CamRecordVo;
+import cn.reghao.tnb.content.app.disk.model.vo.CamRecordDetail;
 import cn.reghao.tnb.content.app.geo.model.vo.SelectOption;
 import cn.reghao.tnb.content.app.util.RandomUtil;
 import org.springframework.stereotype.Service;
@@ -50,15 +50,13 @@ public class CamService {
         return Result.success();
     }
 
-    public List<SelectOption> getCamList() {
+    public List<SelectOption> getCamKeyValues() {
         long loginUser = UserContext.getUserId();
         CamDeviceQuery camDeviceQuery = new CamDeviceQuery.Builder()
                 .owner(loginUser)
                 .build();
         Page page = new Page(1, 100);
         List<CamDevice> camDeviceList = camDeviceMapper.findCamDeviceQueryByPage(page, camDeviceQuery);
-        int total = camDeviceList.size();
-
         return camDeviceList.stream().map(camDevice -> {
             long camId = camDevice.getCamId();
             String camName = camDevice.getCamName();
@@ -78,7 +76,7 @@ public class CamService {
         return PageList.pageList(pageNumber, pageSize, total, camDeviceList);
     }
 
-    public CamRecordVo getLatestRecord(long camId) {
+    public CamRecordDetail getLatestRecord(long camId) {
         int pageSize = 10;
         Page page = new Page(1, pageSize);
         //PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "createTime"));
@@ -94,7 +92,7 @@ public class CamService {
             long owner = diskFile.getOwner();
             if (loginUser == owner) {
                 String videoUrl = String.format("/%s", diskFile.getObjectName());
-                return new CamRecordVo(camRecord.getCamId(), recordId, recordTime, coverUrl, videoUrl);
+                return new CamRecordDetail(camRecord.getCamId(), recordId, recordTime, coverUrl, videoUrl);
             }
         }*/
 

+ 7 - 0
content/content-service/src/main/resources/mapper/disk/CamRecordMapper.xml

@@ -32,6 +32,13 @@
         from my_cam_record
         where cam_id=#{camId} and DATE_FORMAT(start_at,'%Y-%m-%d')=#{dayStr}
     </select>
+    <select id="findLatestRecord" resultType="cn.reghao.tnb.content.app.disk.model.po.CamRecord">
+        select *
+        from my_cam_record
+        where cam_id=#{camId}
+        order by start_at desc
+        limit 1
+    </select>
     <select id="findByRecordId" resultType="cn.reghao.tnb.content.app.disk.model.po.CamRecord">
         select *
         from my_cam_record