|
|
@@ -0,0 +1,505 @@
|
|
|
+package com.yunfeiyun.agmp.iotm.device.pest.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
+import com.yunfeiyun.agmp.common.constant.ResConstants;
|
|
|
+import com.yunfeiyun.agmp.common.framework.oss.cloud.OSSFactory;
|
|
|
+import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.SecurityUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
+import com.yunfeiyun.agmp.common.utils.bean.BeanUtils;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.mq.IotActionEnums;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.*;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.reqvo.IotCbdrecogAgainReqVo;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.reqvo.IotCbdrecogReqVo;
|
|
|
+import com.yunfeiyun.agmp.iot.common.enums.EnumCbdRecogType;
|
|
|
+import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.IotWeatherService;
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
+import com.yunfeiyun.agmp.iot.common.util.tmn.CustomerIdUtil;
|
|
|
+import com.yunfeiyun.agmp.iotm.device.pest.domain.IotCbdrecogResVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.device.pest.service.IIotCbdrecogService;
|
|
|
+import com.yunfeiyun.agmp.iotm.device.pest.service.IIotPestService;
|
|
|
+import com.yunfeiyun.agmp.iotm.mq.provider.IotsMqProviderService;
|
|
|
+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.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测报灯识别数据Service业务层处理
|
|
|
+ *
|
|
|
+ * @author 杨晓辉
|
|
|
+ * @date 2024-01-04
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IotCbdrecogServiceImpl implements IIotCbdrecogService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
+ @Resource
|
|
|
+ private IIotPestService iotPestService;
|
|
|
+ @Resource
|
|
|
+ private IIotDeviceService iotDeviceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotWeatherService iotWeatherService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MongoService mongoService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotsMqProviderService iotsMqProviderService;
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public int artificialUpdate(IotCbdrecogReqVo reqVo) {
|
|
|
+ String cbdrecogType = reqVo.getCbdrecogType();
|
|
|
+ String cbdrecogMarktype = "0";
|
|
|
+ String cbdimgBid = reqVo.getCbdimgBid();
|
|
|
+ String cbdrecogManualmark = reqVo.getCbdrecogManualmark();
|
|
|
+ String cbdrecogManualBase64 = reqVo.getCbdrecogManualBase64();
|
|
|
+ String devBid = reqVo.getDevBid();
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("cbdimgBid", cbdimgBid);
|
|
|
+ IotCbdimg iotCbdimg = (IotCbdimg) mongoService.findOne(IotCbdimg.class, map, null, null);
|
|
|
+
|
|
|
+ Criteria cri = Criteria.where("cbdimgBid").is(cbdimgBid)
|
|
|
+ .and("cbdrecogType").is(cbdrecogType)
|
|
|
+ .and("cbdrecogMarktype").is(cbdrecogMarktype);
|
|
|
+ IotCbdrecog iotCbdrecog = mongoTemplate.findOne(new Query(cri),
|
|
|
+ IotCbdrecog.class, "IotCbdrecog");
|
|
|
+ boolean isUpdate = true;
|
|
|
+ if (iotCbdrecog == null) {
|
|
|
+ Criteria cri2 = Criteria.where("cbdimgBid").is(cbdimgBid)
|
|
|
+ .and("cbdrecogType").is(cbdrecogType);
|
|
|
+ iotCbdrecog = mongoTemplate.findOne(new Query(cri2),
|
|
|
+ IotCbdrecog.class, "IotCbdrecog");
|
|
|
+ if (iotCbdrecog == null) {
|
|
|
+ iotCbdrecog = new IotCbdrecog();
|
|
|
+ iotCbdrecog.setCbdimgBid(cbdimgBid);
|
|
|
+ iotCbdrecog.setCbdrecogType(cbdrecogType);
|
|
|
+ iotCbdrecog.setCbdrecogDelstatus("0");
|
|
|
+ iotCbdrecog.setCbdrecogCreatedDate(iotCbdimg.getCbdimgCreatedDate());
|
|
|
+ iotCbdrecog.setCbdrecogModifieddate(iotCbdimg.getCbdimgCreatedDate());
|
|
|
+ iotCbdrecog.setCbdrecogCreator(SecurityUtils.getUserId());
|
|
|
+
|
|
|
+ }
|
|
|
+ iotCbdrecog.setCbdrecogBid(iotCbdrecog.getUUId());
|
|
|
+ iotCbdrecog.setCbdrecogMarktype(cbdrecogMarktype);
|
|
|
+ isUpdate = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(cbdrecogManualBase64)) {
|
|
|
+ String imgUrl = getPestRecogImgOssUrl(cbdrecogManualBase64, iotCbdrecog.getCbdrecogBid());
|
|
|
+ iotCbdrecog.setCbdrecogManualaddr(imgUrl);
|
|
|
+ }
|
|
|
+ iotCbdrecog.setCbdrecogManualmark(cbdrecogManualmark);
|
|
|
+
|
|
|
+ JSONArray recogArray = JSONArray.parseArray(cbdrecogManualmark);
|
|
|
+
|
|
|
+ List<JSONObject> points = new ArrayList<>();//包含了所有识别(标记),其中(已收录过的害虫的),提交上来的是pestid:标记信息,人工识别(?未收录过的害虫?前端是否判断后提交?),提交上来的是 ”害虫名称“:"标记信息"
|
|
|
+ List<JSONObject> points2 = new ArrayList<>();
|
|
|
+ if (recogArray != null) {
|
|
|
+ for (Object o : JSONArray.parseArray(cbdrecogManualmark)) {
|
|
|
+ points.add((JSONObject) o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //害虫信息
|
|
|
+ List<IotPest> iotPests = iotPestService.selectIotPestList(null);
|
|
|
+ //Map<String, String> pestMap = iotPests.stream().collect(Collectors.toMap(IotPest::getPestId, IotPest::getPestBid));
|
|
|
+ Map<String, String> pestMap = iotPests.stream().collect(Collectors.toMap(IotPest::getPestId, IotPest::getPestName));
|
|
|
+ Map<String, String> pestMap2 = iotPests.stream().collect(Collectors.toMap(IotPest::getPestId, IotPest::getPestBid));
|
|
|
+ //设备信息
|
|
|
+ IotDevice iotDevice = iotDeviceService.selectIotDeviceByDevBid(devBid);
|
|
|
+ //统计害虫数量(pestid->数量)
|
|
|
+ Map<String, Integer> pest = new HashMap<>();
|
|
|
+
|
|
|
+ //IotPestrecog存储
|
|
|
+ for (JSONObject point : points) {
|
|
|
+ points2.add(point);
|
|
|
+ //判断人工修正的是否有新的害虫名字,有的新的新增,并存储
|
|
|
+ for (String s : point.keySet()) {
|
|
|
+ if (pestMap.get(s) == null) {//未收录的
|
|
|
+ if (!pestMap.containsValue(s)) {//名字确实不存在的
|
|
|
+ IotPest iotPest = new IotPest();
|
|
|
+ iotPest.setPestBid(iotPest.getUUId());
|
|
|
+ iotPest.setPestName(s);
|
|
|
+ iotPest.setPestId(iotPest.getPestBid());//新产生的iotpest, pest id和pest bid一样
|
|
|
+ iotPest.setPestCreator(SecurityUtils.getUserId());
|
|
|
+ iotPest.setPestCreatedDate(DateUtils.dateTimeNow());
|
|
|
+ iotPest.setPestDelstatus("0");
|
|
|
+ iotPest.setPestInsertType("2");
|
|
|
+ iotPest.setCId(CustomerIdUtil.getCustomerId());
|
|
|
+ iotPestService.insertIotPest(iotPest);//新害虫名称入库
|
|
|
+
|
|
|
+ //pestMap.put(iotPest.getPestId(), iotPest.getPestBid());
|
|
|
+ pestMap.put(iotPest.getPestId(), iotPest.getPestName());
|
|
|
+ pestMap2.put(iotPest.getPestBid(), iotPest.getPestName());
|
|
|
+
|
|
|
+ JSONObject point2 = new JSONObject();
|
|
|
+ point2.put(iotPest.getPestId(), point.get(s));
|
|
|
+ points2.add(point2);
|
|
|
+ points2.remove(point);
|
|
|
+
|
|
|
+ //pest.put(iotPest.getPestId(), pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
+ pest.put(iotPest.getPestId(), pest.get(iotPest.getPestId()) == null ? 1 : pest.get(iotPest.getPestId()) + 1);
|
|
|
+ } else {
|
|
|
+ //名字-》pestid
|
|
|
+ String findPestId = "";
|
|
|
+ for (Map.Entry<String, String> entry : pestMap.entrySet()) {
|
|
|
+ if (s.equals(entry.getValue())) {
|
|
|
+ findPestId = entry.getKey();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pest.put(findPestId, pest.get(findPestId) == null ? 1 : pest.get(findPestId) + 1);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pest.put(s, pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //IotPestrecog进行存储
|
|
|
+ String province = iotDevice.getDevProvince();
|
|
|
+ String city = iotDevice.getDevCity();
|
|
|
+ String district = iotDevice.getDevDistrict();
|
|
|
+ BigDecimal lat = iotDevice.getDevLatalign() == null ? iotDevice.getDevLat() : iotDevice.getDevLatalign();
|
|
|
+ BigDecimal lng = iotDevice.getDevLngalign() == null ? iotDevice.getDevLng() : iotDevice.getDevLngalign();
|
|
|
+
|
|
|
+ JSONObject weatherInfo = iotWeatherService.getWeatherByAddress(province, city, district);
|
|
|
+ BigDecimal at = BigDecimal.valueOf(Long.parseLong("0"));
|
|
|
+ BigDecimal ah = BigDecimal.valueOf(Long.parseLong("0"));
|
|
|
+ if (weatherInfo != null) {
|
|
|
+ at = BigDecimal.valueOf(Double.parseDouble((String) weatherInfo.getOrDefault("tem", "0")));
|
|
|
+ ah = BigDecimal.valueOf(Double.parseDouble(((String) weatherInfo.getOrDefault("humidity", "0")).replace("%", "")));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<IotPestrecog> iotPestrecogList = new ArrayList<>();
|
|
|
+ long recogNum = 0L;
|
|
|
+ for (String s : pest.keySet()) {
|
|
|
+ long pestNum = pest.get(s).longValue();
|
|
|
+ recogNum += pestNum;
|
|
|
+
|
|
|
+ IotPestrecog iotPestrecog = new IotPestrecog();
|
|
|
+ iotPestrecog.setCId(iotDevice.getTid());
|
|
|
+ iotPestrecog.setPestrecogBid(iotPestrecog.getUUId());
|
|
|
+ iotPestrecog.setDevBid(iotDevice.getDevBid());
|
|
|
+ iotPestrecog.setDevtypeBid(iotDevice.getDevtypeBid());
|
|
|
+ iotPestrecog.setPestrecogAt(at);
|
|
|
+ iotPestrecog.setPestrecogAh(ah);
|
|
|
+ iotPestrecog.setPestrecogLng(lng);
|
|
|
+ iotPestrecog.setPestrecogLat(lat);
|
|
|
+ iotPestrecog.setPestrecogProvince(province);
|
|
|
+ iotPestrecog.setPestrecogCity(city);
|
|
|
+ iotPestrecog.setPestrecogDistrict(district);
|
|
|
+ //iotPestrecog.setPestBusid(pestMap.get(s));
|
|
|
+ iotPestrecog.setPestBusid(pestMap2.get(s));//这里按设计要存放pest bid
|
|
|
+ iotPestrecog.setPestrecogNum(pest.get(s).longValue());
|
|
|
+ iotPestrecog.setPestrecogMarktype(cbdrecogMarktype);
|
|
|
+ iotPestrecog.setPestrecogCreatedDate(iotCbdrecog.getCbdrecogCreatedDate());
|
|
|
+
|
|
|
+ iotPestrecogList.add(iotPestrecog);
|
|
|
+ }
|
|
|
+
|
|
|
+ iotCbdrecog.setCbdimgRecognum(recogNum);
|
|
|
+
|
|
|
+ Map<String, String> cbdpestMap = new HashMap<>();
|
|
|
+ cbdpestMap.put("cbdrecogBid", iotCbdrecog.getCbdrecogBid());
|
|
|
+ List<IotCbdpest> cbdpestList = mongoService.findAll(IotCbdpest.class, cbdpestMap);
|
|
|
+ Set<String> pestrecogBidSet = new HashSet<>();
|
|
|
+ Set<String> cbdpestBidSet = new HashSet<>();
|
|
|
+ for (IotCbdpest cbdpest : cbdpestList) {
|
|
|
+ pestrecogBidSet.add(cbdpest.getPestrecogBid());
|
|
|
+ cbdpestBidSet.add(cbdpest.getCbdpestBid());
|
|
|
+ }
|
|
|
+ List<String> pestrecogBidList = new ArrayList<>(pestrecogBidSet);
|
|
|
+ List<String> cbdpestBidList = new ArrayList<>(cbdpestBidSet);
|
|
|
+
|
|
|
+ if (pestrecogBidList.size() > 0) {
|
|
|
+ mongoService.removeAllByBatch("pestrecogBid", pestrecogBidList, "IotCbdpest");
|
|
|
+ }
|
|
|
+ if (cbdpestBidList.size() > 0) {
|
|
|
+ mongoService.removeAllByBatch("cbdpestBid", cbdpestBidList, "IotCbdpest");
|
|
|
+ }
|
|
|
+
|
|
|
+ //针对人工识别,设置cbdrecogResultManual和cbdrecogManualmark
|
|
|
+ String cbdrecogResultManual = "";
|
|
|
+ //遍历pest,拼接成字符串
|
|
|
+ for (String s : pest.keySet()) {
|
|
|
+ cbdrecogResultManual += s + "," + pest.get(s).longValue() + "#";
|
|
|
+ }
|
|
|
+ if (cbdrecogResultManual.endsWith("#")) {
|
|
|
+ cbdrecogResultManual = cbdrecogResultManual.substring(0, cbdrecogResultManual.length() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ iotCbdrecog.setCbdrecogResultManual(cbdrecogResultManual);
|
|
|
+
|
|
|
+ iotCbdrecog.setCbdrecogManualmark(JSONUtils.toJSONString(points2));
|
|
|
+
|
|
|
+ if (isUpdate) {
|
|
|
+ Map<String, Object> updateField = JSONUtils.toMap(JSONUtils.toJSONString(iotCbdrecog));
|
|
|
+ updateField.remove("id");
|
|
|
+ updateField.remove("uuid");
|
|
|
+
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ criteria.and("cbdrecogBid").is(iotCbdrecog.getCbdrecogBid());
|
|
|
+ Query updateQuery = new Query(criteria);
|
|
|
+ mongoService.updateOrCreate(IotCbdrecog.class, updateQuery, updateField);
|
|
|
+ } else {
|
|
|
+ mongoService.saveOne(iotCbdrecog);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (IotPestrecog iotPestrecog : iotPestrecogList) {
|
|
|
+ mongoService.saveOne(iotPestrecog);
|
|
|
+
|
|
|
+ // 保存 测报灯图片识别和虫情信息关联信息
|
|
|
+ IotCbdpest iotCbdpest = new IotCbdpest();
|
|
|
+ iotCbdpest.setCId(iotDevice.getTid());
|
|
|
+ iotCbdpest.setCbdpestBid(iotCbdpest.getUUId());
|
|
|
+ iotCbdpest.setPestrecogBid(iotPestrecog.getPestrecogBid());
|
|
|
+ iotCbdpest.setCbdrecogBid(iotCbdrecog.getCbdrecogBid());
|
|
|
+ mongoService.saveOne(iotCbdpest);
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int artificialReset(IotCbdrecogReqVo reqVo) {
|
|
|
+ String cbdrecogType = reqVo.getCbdrecogType();
|
|
|
+ String cbdrecogMarktype = "0";
|
|
|
+ String cbdimgBid = reqVo.getCbdimgBid();
|
|
|
+
|
|
|
+ Criteria cri = Criteria.where("cbdimgBid").is(cbdimgBid)
|
|
|
+ .and("cbdrecogType").is(cbdrecogType)
|
|
|
+ .and("cbdrecogMarktype").is(cbdrecogMarktype);
|
|
|
+ IotCbdrecog iotCbdrecog = mongoTemplate.findOne(new Query(cri),
|
|
|
+ IotCbdrecog.class, "IotCbdrecog");
|
|
|
+
|
|
|
+ if (iotCbdrecog == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ Map<String, String> cbdpestMap = new HashMap<>();
|
|
|
+ cbdpestMap.put("cbdrecogBid", iotCbdrecog.getCbdrecogBid());
|
|
|
+ List<IotCbdpest> cbdpestList = mongoService.findAll(IotCbdpest.class, cbdpestMap);
|
|
|
+ Set<String> pestrecogBidSet = new HashSet<>();
|
|
|
+ Set<String> cbdpestBidSet = new HashSet<>();
|
|
|
+ for (IotCbdpest cbdpest : cbdpestList) {
|
|
|
+ pestrecogBidSet.add(cbdpest.getPestrecogBid());
|
|
|
+ cbdpestBidSet.add(cbdpest.getCbdpestBid());
|
|
|
+ }
|
|
|
+ List<String> pestrecogBidList = new ArrayList<>(pestrecogBidSet);
|
|
|
+ List<String> cbdpestBidList = new ArrayList<>(cbdpestBidSet);
|
|
|
+
|
|
|
+ if (pestrecogBidList.size() > 0) {
|
|
|
+ mongoService.removeAllByBatch("pestrecogBid", pestrecogBidList, "IotCbdpest");
|
|
|
+ }
|
|
|
+ if (cbdpestBidList.size() > 0) {
|
|
|
+ mongoService.removeAllByBatch("cbdpestBid", cbdpestBidList, "IotCbdpest");
|
|
|
+ }
|
|
|
+ mongoService.removeAllByParam("cbdrecogBid", iotCbdrecog.getCbdrecogBid(), "IotCbdrecog");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param reqVo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IotCbdrecogResVo info(IotCbdrecogReqVo reqVo) {
|
|
|
+ String cbdrecogType = reqVo.getCbdrecogType();
|
|
|
+ String cbdrecogMarktype = reqVo.getCbdrecogMarktype();
|
|
|
+ String cbdimgBid = reqVo.getCbdimgBid();
|
|
|
+
|
|
|
+ Criteria cri = Criteria.where("cbdimgBid").is(cbdimgBid)
|
|
|
+ .and("cbdrecogType").is(cbdrecogType)
|
|
|
+ .and("cbdrecogMarktype").is(cbdrecogMarktype);
|
|
|
+ IotCbdrecog iotCbdrecog = mongoTemplate.findOne(new Query(cri),
|
|
|
+ IotCbdrecog.class, "IotCbdrecog");
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("cbdimgBid", cbdimgBid);
|
|
|
+ IotCbdimg iotCbdimg = (IotCbdimg) mongoService.findOne(IotCbdimg.class, map, null, null);
|
|
|
+
|
|
|
+ if (iotCbdrecog == null) {
|
|
|
+ Criteria cri2 = Criteria.where("cbdimgBid").is(cbdimgBid)
|
|
|
+ .and("cbdrecogType").is(cbdrecogType);
|
|
|
+ iotCbdrecog = mongoTemplate.findOne(new Query(cri2),
|
|
|
+ IotCbdrecog.class, "IotCbdrecog");
|
|
|
+ if (iotCbdrecog == null) {
|
|
|
+ iotCbdrecog = new IotCbdrecog();
|
|
|
+ iotCbdrecog.setCbdrecogCreatedDate(iotCbdimg.getCbdimgCreatedDate());
|
|
|
+ iotCbdrecog.setCbdimgRecognum(0L);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ IotCbdrecogResVo resVo = new IotCbdrecogResVo();
|
|
|
+ BeanUtils.copyProperties(iotCbdrecog, resVo);
|
|
|
+ if (cbdrecogType.equals(EnumCbdRecogType.A.getCode())) {
|
|
|
+ resVo.setCbdimgRecognum(iotCbdimg.getCbdimgRecognum());
|
|
|
+ } else {
|
|
|
+ resVo.setCbdimgRecognum(iotCbdrecog.getCbdimgRecognum());
|
|
|
+ }
|
|
|
+
|
|
|
+ resVo.setCbdimgAddr(iotCbdimg.getCbdimgAddr());
|
|
|
+ resVo.setCbdimgPestnum(iotCbdimg.getCbdimgPestnum());
|
|
|
+
|
|
|
+ return resVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotCbdrecogResVo> getCbdImgLimit(List<String> devIds, String startData, String endDate, Integer limitNum) {
|
|
|
+ String startTime = startData;
|
|
|
+ String endTime = endDate;
|
|
|
+
|
|
|
+ // 构建基本查询条件
|
|
|
+ Criteria criteria = new Criteria().and("cbdimgDelstatus").is("0");
|
|
|
+ criteria.and("devBid").in(devIds);
|
|
|
+
|
|
|
+ // 时间查询
|
|
|
+ if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
|
|
|
+ criteria.andOperator(
|
|
|
+ Criteria.where("cbdimgCreatedDate").gte(startTime),
|
|
|
+ Criteria.where("cbdimgCreatedDate").lte(endTime)
|
|
|
+ );
|
|
|
+ } else if (StringUtils.isNotEmpty(startTime)) {
|
|
|
+ criteria.and("cbdimgCreatedDate").gte(startTime);
|
|
|
+ } else if (StringUtils.isNotEmpty(endTime)) {
|
|
|
+ criteria.and("cbdimgCreatedDate").lte(endTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 匹配操作
|
|
|
+ AggregationOperation matchOperation = Aggregation.match(criteria);
|
|
|
+
|
|
|
+ // 连接IotCbdrecog
|
|
|
+ AggregationOperation lookupOperationIotCbdrecog = Aggregation.lookup(
|
|
|
+ "IotCbdrecog", "cbdimgBid", "cbdimgBid", "recogs"
|
|
|
+ );
|
|
|
+
|
|
|
+ // 展开recogs数组
|
|
|
+ AggregationOperation unwindOperationRecogs = Aggregation.unwind("recogs");
|
|
|
+
|
|
|
+ // 筛选recogs中特定类型和标记类型的记录
|
|
|
+ Criteria recogCriteria = new Criteria()
|
|
|
+ .and("recogs.cbdrecogMarktype").is("1");
|
|
|
+ AggregationOperation matchRecogOperation = Aggregation.match(recogCriteria);
|
|
|
+
|
|
|
+ // 重新投影字段,以适应IotCbdrecogResVo的结构
|
|
|
+ AggregationOperation projectOperation = Aggregation.project()
|
|
|
+ .and("recogs.cbdrecogBid").as("cbdrecogBid")
|
|
|
+ .and("recogs.cId").as("cId")
|
|
|
+ .and("cbdimgBid").as("cbdimgBid")
|
|
|
+ .and("recogs.cbdrecogType").as("cbdrecogType")
|
|
|
+ .and("recogs.cbdrecogAddr").as("cbdrecogAddr")
|
|
|
+ .and("recogs.cbdrecogManualaddr").as("cbdrecogManualaddr")
|
|
|
+ .and("recogs.cbdrecogResult").as("cbdrecogResult")
|
|
|
+ .and("recogs.cbdrecogMarktype").as("cbdrecogMarktype")
|
|
|
+ .and("recogs.cbdrecogManualmark").as("cbdrecogManualmark")
|
|
|
+ .and("recogs.cbdrecogMachinemark").as("cbdrecogMachinemark")
|
|
|
+ .and("recogs.cbdrecogCreator").as("cbdrecogCreator")
|
|
|
+ .and("recogs.cbdrecogModifier").as("cbdrecogModifier")
|
|
|
+ .and("recogs.cbdrecogModifieddate").as("cbdrecogModifieddate")
|
|
|
+ .and("recogs.cbdrecogCreatedDate").as("cbdrecogCreatedDate")
|
|
|
+ .and("recogs.cbdrecogDelstatus").as("cbdrecogDelstatus")
|
|
|
+ .and("cbdimgAddr").as("cbdimgAddr")
|
|
|
+ .and("cbdimgPestnum").as("cbdimgPestnum")
|
|
|
+ .and("recogs.cbdimgRecognum").as("cbdimgRecognum")
|
|
|
+ //.and("recogs.cbdrecogManualBase64").as("cbdrecogManualBase64")
|
|
|
+ .and("cbdimgCreatedDate").as("cbdimgCreatedDate")
|
|
|
+ .and("cbdimgModifieddate").as("cbdimgModifieddate");
|
|
|
+
|
|
|
+ // 添加排序操作
|
|
|
+ AggregationOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "cbdimgCreatedDate"));
|
|
|
+ // 添加限制操作
|
|
|
+ AggregationOperation limitOperation = null;
|
|
|
+ if (limitNum != null) {
|
|
|
+ limitOperation = Aggregation.limit(limitNum);
|
|
|
+ }
|
|
|
+ // 创建聚合
|
|
|
+ List<AggregationOperation> aggregationOperations = new ArrayList<>();
|
|
|
+ aggregationOperations.add(matchOperation);
|
|
|
+ aggregationOperations.add(lookupOperationIotCbdrecog);
|
|
|
+ aggregationOperations.add(unwindOperationRecogs);
|
|
|
+ aggregationOperations.add(matchRecogOperation);
|
|
|
+ aggregationOperations.add(projectOperation);
|
|
|
+ aggregationOperations.add(sortOperation);
|
|
|
+ if (limitOperation != null) {
|
|
|
+ aggregationOperations.add(limitOperation);
|
|
|
+ }
|
|
|
+
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(aggregationOperations);
|
|
|
+
|
|
|
+
|
|
|
+ // 执行聚合并转换结果
|
|
|
+ AggregationResults<IotCbdrecogResVo> results = mongoTemplate.aggregate(aggregation, "IotCbdimg", IotCbdrecogResVo.class);
|
|
|
+ return results.getMappedResults();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param reqVo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IotCbdrecogResVo againRecog(IotCbdrecogAgainReqVo reqVo) {
|
|
|
+ String cbdrecogType = reqVo.getCbdrecogType();
|
|
|
+ String cbdimgBid = reqVo.getCbdimgBid();
|
|
|
+ String devBid = reqVo.getDevBid();
|
|
|
+
|
|
|
+ if (EnumCbdRecogType.findEnumByCode(cbdrecogType) == null) {
|
|
|
+ throw new IotBizException(ErrorCode.FAILURE.getCode(), "识别类型错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ IotDevice iotDevice = iotDeviceService.selectIotDeviceByDevBid(devBid);
|
|
|
+ if (iotDevice == null) {
|
|
|
+ throw new IotBizException(ErrorCode.FAILURE.getCode(), "设备不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("cbdimgBid", cbdimgBid);
|
|
|
+ IotCbdimg iotCbdimg = (IotCbdimg) mongoService.findOne(IotCbdimg.class, map, null, null);
|
|
|
+ if (iotCbdimg == null) {
|
|
|
+ throw new IotBizException(ErrorCode.FAILURE.getCode(), "图片不存在");
|
|
|
+ }
|
|
|
+ iotsMqProviderService.sendBaseDataToIots(IotActionEnums.DEVICE_CBD_AGAIN_RECORD,reqVo,"测报灯重新识别");
|
|
|
+ //Object recogResult = iotmMqService.sendMsgAndReceive("againRecog", IotMqConstant.CBD_IMAGE_AGAIN_RECOGE, reqVo);
|
|
|
+ IotCbdrecogReqVo iotCbdrecogReqVo = new IotCbdrecogReqVo();
|
|
|
+ iotCbdrecogReqVo.setCbdimgBid(reqVo.getCbdimgBid());
|
|
|
+ iotCbdrecogReqVo.setCbdrecogMarktype("1");
|
|
|
+ iotCbdrecogReqVo.setCbdrecogType(reqVo.getCbdrecogType());
|
|
|
+ return info(iotCbdrecogReqVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getPestRecogImgOssUrl(String base64, String imgId) {
|
|
|
+ if (base64.contains(",")) {
|
|
|
+ base64 = base64.split(",")[1];
|
|
|
+ }
|
|
|
+ byte[] bytesFile = Base64.decode(base64);
|
|
|
+ return OSSFactory.build().uploadSuffix(bytesFile, "jpg", ResConstants.BizType.CBD_RECOGNITION_MARKERS_AI.getPath(), imgId);
|
|
|
+ }
|
|
|
+}
|