Просмотр исходного кода

新增 导出识别图片列表接口

zhaiyifei 1 год назад
Родитель
Сommit
b522a76f1d

+ 18 - 21
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/controller/IotPestrecogController.java

@@ -2,28 +2,28 @@ package com.yunfeiyun.agmp.iotm.device.pest.controller;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.yunfeiyun.agmp.common.annotation.Log;
 import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.core.controller.BaseController;
 import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
 import com.yunfeiyun.agmp.common.core.page.PageDomain;
 import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
 import com.yunfeiyun.agmp.common.core.page.TableSupport;
+import com.yunfeiyun.agmp.common.enums.BusinessType;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
 import com.yunfeiyun.agmp.iot.common.service.MongoService;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestRecogPeriodReqVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogGroupByNameResVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogListResVo;
+import com.yunfeiyun.agmp.iotm.device.pest.domain.*;
 import com.yunfeiyun.agmp.iotm.device.pest.service.IIotPestService;
 import com.yunfeiyun.agmp.iotm.device.pest.service.IIotPestrecogService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.security.access.prepost.PreAuthorize;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 
 /**
@@ -68,21 +68,18 @@ public class IotPestrecogController extends BaseController {
         IPage<IotPestrecogListResVo> listPage = iIotPestrecogService.selectIotPestrecogList(reqVo, pageDomain);
         return getDataTable(listPage);
     }
-//
-//    /**
-//     * 导出识别数据列表
-//     */
-//    @PreAuthorize("@ss.hasPermi('iot:pestrecog:data:export')")
-//    @Log(title = "设备数据", businessType = BusinessType.EXPORT)
-//    @PostMapping("/data/export")
-//    public void dataExport(HttpServletResponse response, @RequestBody IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException {
-//
-//
-//        List<IotPestrecogExportResVo> list = iIotPestrecogService.exportIotPestrecogList(reqVo);
-//        //List<IotPestrecogResVo> list = getIotPestrecogResVoList2(records);
-//        ExcelUtil<IotPestrecogExportResVo> util = new ExcelUtil<>(IotPestrecogExportResVo.class);
-//        util.exportExcel(response, list, "识别数据");
-//    }
+
+    /**
+     * 导出识别数据列表
+     */
+    @PreAuthorize("@ss.hasPermi('iot:pestrecog:data:export')")
+    @Log(title = "设备数据", businessType = BusinessType.EXPORT)
+    @PostMapping("/data/export")
+    public void dataExport(HttpServletResponse response, @RequestBody IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException {
+        List<IotPestrecogListExportResVo> list = iIotPestrecogService.exportIotPestrecogList(reqVo);
+        ExcelUtil<IotPestrecogListExportResVo> util = new ExcelUtil<>(IotPestrecogListExportResVo.class);
+        util.exportExcel(response, list, "识别数据");
+    }
 //
 //    /**
 //     * 根据害虫分组统计数量

+ 2 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/domain/IotPestRecogPeriodReqVo.java

@@ -65,4 +65,6 @@ public class IotPestRecogPeriodReqVo {
      * 标记类型 1 AI,0 人工
      */
     private String cbdrecogMarktype;
+
+    private List<String> cbdrecogBids;
 }

+ 62 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/domain/IotPestrecogListExportResVo.java

@@ -0,0 +1,62 @@
+package com.yunfeiyun.agmp.iotm.device.pest.domain;
+
+import com.yunfeiyun.agmp.common.annotation.Excel;
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+/**
+ * 害虫识别数据导出对象
+ *
+ * @author MZQ
+ * @date 2024-08-06
+ */
+@Data
+public class IotPestrecogListExportResVo
+{
+    private static final long serialVersionUID = 1L;
+
+    /** 设备编号 */
+    @Excel(name = "设备编号")
+    private String devCode;
+
+    /** 设备类型名称 */
+    @Excel(name = "设备类型")
+    private String devtypeName;
+
+    /** 识别结果 */
+    @Excel(name = "识别结果")
+    private String pestrecogResult;
+
+    /** 识别类型 */
+    @Excel(name = "识别类型", readConverterExp = "0=人工识别,1=AI识别")
+    private String cbdrecogMarktype;
+
+    /** 创建时间 */
+    @Excel(name = "识别时间")
+    private String pestrecogCreatedDate;
+
+    /** 识别图片地址 */
+    @Excel(name = "识别图片地址")
+    private String cbdrecogAddr;
+
+    /** 原图地址 */
+    @Excel(name = "原图地址")
+    private String cbdimgAddr;
+
+    /** 温度 */
+    @Excel(name = "温度")
+    private BigDecimal pestrecogAt;
+
+    /** 湿度 */
+    @Excel(name = "湿度")
+    private BigDecimal pestrecogAh;
+
+    /** 设备地址 */
+    @Excel(name = "设备地址")
+    private String address;
+    /** 地址 */
+    @Excel(name = "经纬度")
+    private String lnglat;
+
+}

+ 2 - 5
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/service/IIotPestrecogService.java

@@ -5,10 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.yunfeiyun.agmp.common.core.page.PageDomain;
 import com.yunfeiyun.agmp.iot.common.domain.IotPestrecog;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestRecogPeriodReqVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogExportResVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogGroupByNameResVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogListResVo;
+import com.yunfeiyun.agmp.iotm.device.pest.domain.*;
 import com.yunfeiyun.agmp.iotm.device.xy.domain.IotXyinfoPestTotalDto;
 
 import java.util.List;
@@ -17,7 +14,7 @@ import java.util.Map;
 public interface IIotPestrecogService {
     public IPage<IotPestrecogListResVo> selectIotPestrecogList(IotPestRecogPeriodReqVo reqVo, PageDomain pageDomain) throws JsonProcessingException;
 
-    public List<IotPestrecogExportResVo> exportIotPestrecogList(IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException;
+    public List<IotPestrecogListExportResVo> exportIotPestrecogList(IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException;
 
     public List<IotPestrecogGroupByNameResVo> groupByName(IotDeviceDataListReqVo reqVo);
 

+ 82 - 120
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/service/impl/IotPestrecogServiceImpl.java

@@ -5,30 +5,32 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.yunfeiyun.agmp.common.core.page.PageDomain;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
-import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
 import com.yunfeiyun.agmp.iot.common.domain.*;
 import com.yunfeiyun.agmp.iot.common.domain.resvo.IoPestResVo;
+import com.yunfeiyun.agmp.iot.common.enums.EnumCbdMarkType;
 import com.yunfeiyun.agmp.iot.common.service.MongoService;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
 import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestRecogPeriodReqVo;
-import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogExportResVo;
 import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogGroupByNameResVo;
+import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogListExportResVo;
 import com.yunfeiyun.agmp.iotm.device.pest.domain.IotPestrecogListResVo;
 import com.yunfeiyun.agmp.iotm.device.pest.service.IIotPestService;
 import com.yunfeiyun.agmp.iotm.device.pest.service.IIotPestrecogService;
 import com.yunfeiyun.agmp.iotm.device.xy.domain.IotXyinfoPestTotalDto;
 import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.mongodb.core.aggregation.*;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
+@Slf4j
 @Service
 public class IotPestrecogServiceImpl implements IIotPestrecogService {
 
@@ -144,9 +146,8 @@ public class IotPestrecogServiceImpl implements IIotPestrecogService {
                 replaceRootOperation,
                 projectionOperation
         );
-        IPage<IotPestrecogListResVo> listPage = mongoService.aggregate(
-                IotCbdimg.class, aggregation, IotPestrecogListResVo.class, pageDomain
-        );
+        IPage<IotPestrecogListResVo> listPage = mongoService.aggregate(IotCbdimg.class, aggregation,
+                IotPestrecogListResVo.class, pageDomain);
         return listPage;
     }
 
@@ -156,150 +157,111 @@ public class IotPestrecogServiceImpl implements IIotPestrecogService {
      * @throws JsonProcessingException
      */
     @Override
-    public List<IotPestrecogExportResVo> exportIotPestrecogList(IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException {
+    public List<IotPestrecogListExportResVo> exportIotPestrecogList(IotPestRecogPeriodReqVo reqVo) throws JsonProcessingException {
         String devBid = reqVo.getDevBid();
         String startTime = reqVo.getStartTime();
         String endTime = reqVo.getEndTime();
         String cbdrecogMarktype = reqVo.getCbdrecogMarktype();
         String cbdrecogType = reqVo.getCbdrecogType();
         List<String> pestBids = reqVo.getPestBids();
+        List<String> cbdrecogBids = reqVo.getCbdrecogBids();
+        String cbdrecogAddrStr = ".cbdrecogAddr";
+        if(Objects.equals(cbdrecogMarktype, EnumCbdMarkType.HAND.getCode())){
+            cbdrecogAddrStr = ".cbdrecogManualaddr";
+        }
+
+        Criteria criteria = new Criteria()
+                .and("devBid").is(devBid)
+                .and("cbdimgDelstatus").is("0");
 
-        Criteria criteria = new Criteria().and("devBid").is(devBid);
-        if(StringUtils.isNotEmpty(cbdrecogMarktype)){
-            criteria.and("pestrecogMarktype").is(cbdrecogMarktype);
+        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
+            criteria = criteria.andOperator(
+                    Criteria.where("cbdrecog." + cbdrecogType + ".cbdrecogModifieddate").gte(startTime),
+                    Criteria.where("cbdrecog." + cbdrecogType + ".cbdrecogModifieddate").lte(endTime)
+            );
+        } else if (StringUtils.isNotEmpty(startTime)) {
+            criteria = criteria.and("cbdrecog." + cbdrecogType + ".cbdrecogModifieddate").gte(startTime);
+        } else if (StringUtils.isNotEmpty(endTime)) {
+            criteria = criteria.and("cbdrecog." + cbdrecogType + ".cbdrecogModifieddate").lte(endTime);
         }
-        List<IotPest> iotPestList = new ArrayList<>();
-        if(pestBids != null){
-            criteria.and("pestBusid").in(pestBids);
-            iotPestList = iotPestService.selectIotPestListBatchByBid(pestBids);
+        if(cbdrecogBids != null && !cbdrecogBids.isEmpty()){
+            criteria = criteria.and("cbdrecog." + cbdrecogType + ".cbdrecogBid").in(cbdrecogBids);
         }
-        Map<String, IotPest> iotPestMap = new HashMap<>();
-        if(iotPestList != null){
-            for(IotPest iotPest: iotPestList){
-                String pestId = iotPest.getPestId();
-                if(!iotPestMap.containsKey(pestId)){
-                    iotPestMap.put(pestId, iotPest);
-                }
-            }
+
+        if(pestBids != null && !pestBids.isEmpty()){
+            criteria = criteria.and("cbdrecog." + cbdrecogType + ".pestrecog." + cbdrecogMarktype + ".pestBusid").in(pestBids);
+        }else{
+            criteria = criteria.and("cbdrecog." + cbdrecogType + ".pestrecog." + cbdrecogMarktype + ".pestBusid").exists(true);
         }
 
         MatchOperation matchOperation = Aggregation.match(criteria);
 
         ProjectionOperation projectionOperation = Aggregation.project()
-                .andExclude("_id")
-                .and("$$ROOT").as("pr");
-
-        LookupOperation lookupOperationIotCbdpest = Aggregation.lookup(
-                "IotCbdpest", "pr.pestrecogBid", "pestrecogBid", "cp"
-        );
-        UnwindOperation unwindOperationCp = Aggregation.unwind("cp", true);
-
-        LookupOperation lookupOperationIotCbdrecog = Aggregation.lookup(
-                "IotCbdrecog", "cp.cbdrecogBid", "cbdrecogBid", "cr"
-        );
-        UnwindOperation unwindOperationCr = Aggregation.unwind("cr", true);
-
-        LookupOperation lookupOperationIotCbdimg = Aggregation.lookup(
-                "IotCbdimg", "cr.cbdimgBid", "cbdimgBid", "ci"
-        );
-        UnwindOperation unwindOperationCi = Aggregation.unwind("ci", true);
-
-        GroupOperation groupOperation = Aggregation.group("cr.cbdrecogBid")
-                .first("pr").as("pr")
-                .first("cr").as("cr")
-                .first("ci").as("ci")
-                .sum("pr.pestrecogNum").as("pestrecogNum");
-
-        Criteria criteria2 = new Criteria()
-                .and("ci.cbdimgDelstatus").is("0")
-                .and("cr.cbdrecogType").is(cbdrecogType)
-                .and("cr.cbdrecogMarktype").is(cbdrecogMarktype);
+                .and("cbdimgAddr").as("cbdimgAddr")
+                .and("cbdrecog." + cbdrecogType + ".cbdrecogBid").as("cbdrecogBid")
+                .and("cbdrecog." + cbdrecogType + cbdrecogAddrStr).as("cbdrecogAddr")
+                .and("cbdrecog." + cbdrecogType + ".pestrecog." + cbdrecogMarktype).as("pestrecog");
 
-        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
-            criteria2 = criteria2.andOperator(
-                    Criteria.where("cr.cbdrecogCreatedDate").gte(startTime),
-                    Criteria.where("cr.cbdrecogCreatedDate").lte(endTime)
-            );
-        } else if (StringUtils.isNotEmpty(startTime)) {
-            criteria2 = criteria2.and("cr.cbdrecogCreatedDate").gte(startTime);
-        } else if (StringUtils.isNotEmpty(endTime)) {
-            criteria2 = criteria2.and("cr.cbdrecogCreatedDate").lte(endTime);
-        }
-        MatchOperation matchOperation2 = Aggregation.match(criteria2);
-
-        ProjectionOperation projectionOperationResult = Aggregation.project("_id")
-                .and("pr.pestrecogBid").as("pestrecogBid")
-                .and("pr.devBid").as("devBid")
-                .and("pr.devtypeBid").as("devtypeBid")
-                .and("pr.pestrecogAt").as("pestrecogAt")
-                .and("pr.pestrecogAh").as("pestrecogAh")
-                .and("pr.pestrecogLng").as("pestrecogLng")
-                .and("pr.pestrecogLat").as("pestrecogLat")
-                .and("pr.pestrecogProvince").as("pestrecogProvince")
-                .and("pr.pestrecogCity").as("pestrecogCity")
-                .and("pr.pestrecogDistrict").as("pestrecogDistrict")
-                .and("pr.pestBusid").as("pestBusid")
+        UnwindOperation unwindOperation = Aggregation.unwind("pestrecog", false);
 
-                .and("pestrecogNum").as("pestrecogNum")
-                .and("cr.cbdrecogCreatedDate").as("pestrecogCreatedDate")
-                .and("cr.cbdrecogAddr").as("cbdrecogAddr")
-                .and("cr.cbdrecogManualaddr").as("cbdrecogManualaddr")
-                .and("cr.cbdrecogMarktype").as("cbdrecogMarktype")
-                .and("cr.cbdrecogManualmark").as("cbdrecogManualmark")
-                .and("cr.cbdrecogMachinemark").as("cbdrecogMachinemark")
-                .and("cr.cbdrecogBid").as("cbdrecogBid")
-                .and("cr.cbdrecogResult").as("cbdrecogResult")
-                .and("cr.cbdrecogResultManual").as("cbdrecogResultManual")
-
-                .and("cr.cbdrecogCreatedDate").as("cbdrecogCreatedDate")
-                .and("ci.cbdimgBid").as("cbdimgBid")
-                .and("ci.cbdimgPestnum").as("cbdimgPestnum")
-                .and("ci.cbdimgRecognum").as("cbdimgRecognum")
-                .and("ci.cbdimgAddr").as("cbdimgAddr");
-
-        SortOperation sortOperation = Aggregation.sort(Sort.Direction.DESC, "cbdrecogCreatedDate");
+        Map<String, String> pestrecogNumMap = new HashMap<>();
+        pestrecogNumMap.put("$toString", "$pestrecog.pestrecogNum");
+        Map<String, Object> pestInfoMap = new HashMap<>();
+        pestInfoMap.put("pestName", "$pestrecog.pestName");
+        pestInfoMap.put("pestrecogNum", pestrecogNumMap);
+
+        GroupOperation groupOperation = Aggregation.group("cbdrecogBid")
+                .last("cbdimgAddr").as("cbdimgAddr")
+                .last("cbdrecogAddr").as("cbdrecogAddr")
+                .last("pestrecog.pestrecogAt").as("pestrecogAt")
+                .last("pestrecog.pestrecogAh").as("pestrecogAh")
+                .last("pestrecog.pestrecogCreatedDate").as("pestrecogCreatedDate")
+                .push(pestInfoMap).as("pestInfoList");
+
+        ProjectionOperation projectionOperationResult = Aggregation.project()
+                .and("cbdrecogBid").as("cbdrecogBid")
+                .and("cbdimgAddr").as("cbdimgAddr")
+                .and("cbdrecogAddr").as("cbdrecogAddr")
+                .and("pestrecogAt").as("pestrecogAt")
+                .and("pestrecogAh").as("pestrecogAh")
+                .and("pestrecogCreatedDate").as("pestrecogCreatedDate")
+                .andExpression(" {'$trim': {'input': {'$reduce': {'input': '$pestInfoList','initialValue': '','in': {'$concat': {'$$value',  {'$concat': {'$$this.pestName', '$$this.pestrecogNum', '头,'}}}}}},'chars': ','}}").as("pestrecogResult");
+
+        SortOperation sortOperation = Aggregation.sort(Sort.Direction.DESC, "pestrecogCreatedDate");
 
         Aggregation aggregation = Aggregation.newAggregation(
                 matchOperation,
                 projectionOperation,
-                lookupOperationIotCbdpest,
-                unwindOperationCp,
-                lookupOperationIotCbdrecog,
-                unwindOperationCr,
-                lookupOperationIotCbdimg,
-                unwindOperationCi,
-                matchOperation2,
+                unwindOperation,
                 groupOperation,
                 projectionOperationResult,
                 sortOperation
         );
-        List<IotPestrecogExportResVo> iotPestrecogList = mongoService.aggregate(
-                IotPestrecog.class, aggregation, IotPestrecogExportResVo.class
+        log.info("查询iotPestrecogList的聚合语句为: " + aggregation);
+        List<IotPestrecogListExportResVo> iotPestrecogList = mongoService.aggregate(
+                IotCbdimg.class, aggregation, IotPestrecogListExportResVo.class
         );
-
-
-        //查询设备号
         IotDevice findDevice = iotDeviceService.selectIotDeviceByDevBid(devBid);
+        String address = findDevice.getDevProvince()+findDevice.getDevCity()+findDevice.getDevDistrict();
+        if(StringUtils.isNotEmpty(findDevice.getDevProvincealign()) && StringUtils.isNotEmpty(findDevice.getDevCityalign())){
+            address = findDevice.getDevProvincealign()+findDevice.getDevCityalign()+findDevice.getDevDistrictalign();
+        }
 
-        Map<String, IotPest> pestMap = iotPestService.selectIotPestMapAllInfo2();
-
-        for (IotPestrecogExportResVo item: iotPestrecogList) {
-
-            if(findDevice != null){
-                item.setDevCode(findDevice.getDevCode());
-            }
+        String lnglat = findDevice.getDevLng()+","+findDevice.getDevLat();
+        if(findDevice.getDevLngalign()!=null && findDevice.getDevLatalign()!=null){
+            lnglat = findDevice.getDevLngalign()+","+findDevice.getDevLatalign();
+        }
 
-            item.setPestrecogResult(parseRecogResult(item.getCbdrecogResult(),pestMap));
+        for(IotPestrecogListExportResVo vo : iotPestrecogList){
+            vo.setDevCode(findDevice.getDevCode());
+            vo.setDevtypeName(findDevice.getDevtypeName());
+            vo.setCbdrecogMarktype(cbdrecogMarktype);
+            vo.setAddress(address);
+            vo.setLnglat(lnglat);
+        }
 
-            if("0".equals(item.getCbdrecogMarktype())){
-                item.setCbdrecogAddr(item.getCbdrecogManualaddr());
-                item.setPestrecogResult(parseRecogResult(item.getCbdrecogResultManual(),pestMap));
-            }
-            item.setAddress(item.getPestrecogProvince()+item.getPestrecogCity()+item.getPestrecogDistrict());
-            item.setLnglat(item.getPestrecogLng()+","+item.getPestrecogLat());
+        //查询设备号
 
-            item.setDevtypeName(IotDeviceDictEnum.getNameByCode(item.getDevtypeBid()));
-        }
 
         return iotPestrecogList;
     }