|
@@ -0,0 +1,530 @@
|
|
|
|
|
+package com.yunfeiyun.agmp.iotm.device.bzy.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
+import com.mongodb.client.result.UpdateResult;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.constant.ResConstants;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.framework.oss.cloud.OSSFactory;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.utils.SecurityUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.*;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.reqvo.IotBzydataReqVo;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
|
|
+import com.yunfeiyun.agmp.iot.common.util.tmn.CustomerIdUtil;
|
|
|
|
|
+import com.yunfeiyun.agmp.iotm.device.bzy.domain.IotYfBzyPolylineResVo;
|
|
|
|
|
+import com.yunfeiyun.agmp.iotm.device.bzy.service.IIotBzyrecogService;
|
|
|
|
|
+import com.yunfeiyun.agmp.iotm.device.bzy.service.IIotSporeService;
|
|
|
|
|
+import com.yunfeiyun.agmp.iotm.util.MongoUtil;
|
|
|
|
|
+import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
|
+import org.springframework.data.mongodb.core.aggregation.*;
|
|
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 测报灯识别数据Service业务层处理
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author 杨晓辉
|
|
|
|
|
+ * @date 2024-01-04
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class IotBzyrecogServiceImpl implements IIotBzyrecogService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private IIotSporeService iotPestService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private IIotDeviceService iotDeviceService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MongoService mongoService;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${iot.warnVer:2}")
|
|
|
|
|
+ private String warnVer;
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int artificialUpdate(IotBzyrecog iotCbdrecog) {
|
|
|
|
|
+ Criteria cri = Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid());
|
|
|
|
|
+ Query query2 = new Query(cri);
|
|
|
|
|
+ IotBzyimg iotCbdimg = mongoTemplate.findOne(query2, IotBzyimg.class, "IotBzyimg");
|
|
|
|
|
+ if (iotCbdimg == null) {
|
|
|
|
|
+ throw new IotBizException(IotErrorCode.FAILURE.getCode(), iotCbdrecog.getBzyimgBid() + "数据不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ IotBzyrecog iotCbdrecog1 = mongoTemplate.findOne(new Query(cri),
|
|
|
|
|
+ IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+ if (iotCbdrecog1 == null) {
|
|
|
|
|
+ //如果未查到,新增一条
|
|
|
|
|
+ iotCbdrecog1 = new IotBzyrecog();
|
|
|
|
|
+ iotCbdrecog1.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogBid(iotCbdrecog1.getUUId());
|
|
|
|
|
+ iotCbdrecog1.setBzyimgBid(iotCbdimg.getBzyimgBid());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogType("1");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogAddr("");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogManualaddr("");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogManualmark(iotCbdrecog.getBzyrecogManualmark());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogResult(iotCbdrecog.getBzyrecogResult());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogMarktype(iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogMachinemark("");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogCreator(SecurityUtils.getUserId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogModifier(SecurityUtils.getUserId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogCreatedDate(iotCbdimg.getBzyimgCreatedDate());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogModifieddate(iotCbdimg.getBzyimgModifieddate());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogDelstatus("0");
|
|
|
|
|
+
|
|
|
|
|
+ mongoTemplate.insert(iotCbdrecog1, "IotBzyrecog");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Query query = new Query(Criteria.where("_id").is(iotCbdrecog1.getId()));
|
|
|
|
|
+ Update update = new Update();
|
|
|
|
|
+ update.set("bzyrecogMarktype", iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ String bzyrecogManualmark = iotCbdrecog.getBzyrecogManualmark();
|
|
|
|
|
+ update.set("bzyrecogManualmark", bzyrecogManualmark);
|
|
|
|
|
+
|
|
|
|
|
+ String recogBid = iotCbdrecog1.getId();
|
|
|
|
|
+ JSONArray recogArray = JSONArray.parseArray(bzyrecogManualmark);
|
|
|
|
|
+
|
|
|
|
|
+ List<JSONObject> points = new ArrayList<>();
|
|
|
|
|
+ // 虫子数量
|
|
|
|
|
+ long recordNum = 0;
|
|
|
|
|
+ List<JSONObject> points2 = new ArrayList<>();
|
|
|
|
|
+ if (recogArray != null) {
|
|
|
|
|
+ for (Object o : JSONArray.parseArray(bzyrecogManualmark)) {
|
|
|
|
|
+ points.add((JSONObject) o);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //害虫信息
|
|
|
|
|
+ List<IotSpore> iotPests = iotPestService.selectIotSporeList(null);
|
|
|
|
|
+ Map<String, String> pestMap = iotPests.stream().collect(Collectors.toMap(IotSpore::getSporeId, IotSpore::getSporeBid));
|
|
|
|
|
+ //设备信息
|
|
|
|
|
+ IotDevice resVo = iotDeviceService.selectIotDeviceByDevBid(iotCbdimg.getDevBid());
|
|
|
|
|
+ //统计害虫数量
|
|
|
|
|
+ Map<String, Integer> pest = new HashMap<>();
|
|
|
|
|
+ recordNum = points.size();
|
|
|
|
|
+ //IotSporerecog存储
|
|
|
|
|
+ for (JSONObject point : points) {
|
|
|
|
|
+ points2.add(point);
|
|
|
|
|
+ //判断人工修正的是否有新的害虫名字,有的新的新增,并存储
|
|
|
|
|
+ for (String s : point.keySet()) {
|
|
|
|
|
+ if (pestMap.get(s) == null) {
|
|
|
|
|
+ IotSpore iotPest = new IotSpore();
|
|
|
|
|
+ iotPest.setSporeBid(iotPest.getUUId());
|
|
|
|
|
+ iotPest.setSporeName(s);
|
|
|
|
|
+ iotPest.setSporeId(iotPest.getSporeBid());
|
|
|
|
|
+ iotPest.setSporeCreator(SecurityUtils.getUserId());
|
|
|
|
|
+ iotPest.setSporeCreatedDate(DateUtils.dateTimeNow());
|
|
|
|
|
+ iotPest.setSporeDelstatus("0");
|
|
|
|
|
+ iotPest.setSporeInsertType("2");
|
|
|
|
|
+ iotPestService.insertIotSpore(iotPest);
|
|
|
|
|
+ pestMap.put(iotPest.getSporeId(), iotPest.getSporeBid());
|
|
|
|
|
+ JSONObject point2 = new JSONObject();
|
|
|
|
|
+ point2.put(iotPest.getSporeId(), point.get(s));
|
|
|
|
|
+ points2.add(point2);
|
|
|
|
|
+ points2.remove(point);
|
|
|
|
|
+ pest.put(iotPest.getSporeId(), pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ pest.put(s, pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String bzyrecogManualBase64 = iotCbdrecog.getBzyrecogManualBase64();
|
|
|
|
|
+ if (StringUtils.isNotEmpty(bzyrecogManualBase64)) {
|
|
|
|
|
+ String imgUrl = getSporeRecogImgOssUrl(bzyrecogManualBase64, recogBid);
|
|
|
|
|
+ update.set("bzyrecogManualaddr", imgUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //IotSporerecog进行存储
|
|
|
|
|
+ List<IotSporerecog> iotPestrecogs = new ArrayList<>();
|
|
|
|
|
+ List<IotBzyspore> iotCbdpests = new ArrayList<>();
|
|
|
|
|
+ for (String s : pest.keySet()) {
|
|
|
|
|
+ IotSporerecog iotPestrecog = new IotSporerecog();
|
|
|
|
|
+ iotPestrecog.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotPestrecog.setDevBid(iotCbdimg.getDevBid());
|
|
|
|
|
+ iotPestrecog.setSporeBusid(pestMap.get(s));
|
|
|
|
|
+ iotPestrecog.setDevtypeBid(resVo.getDevtypeBid());
|
|
|
|
|
+ iotPestrecog.setSporerecogBid(iotPestrecog.getUUId());
|
|
|
|
|
+ iotPestrecog.setSporerecogMarktype(iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ iotPestrecog.setSporerecogNum(pest.get(s).longValue());
|
|
|
|
|
+ iotPestrecog.setSporerecogCreatedDate(DateUtils.dateTimeNow());
|
|
|
|
|
+ iotPestrecogs.add(iotPestrecog);
|
|
|
|
|
+
|
|
|
|
|
+ IotBzyspore iotCbdpest = new IotBzyspore();
|
|
|
|
|
+ iotCbdpest.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotCbdpest.setBzysporeBid(iotCbdpest.getUUId());
|
|
|
|
|
+ iotCbdpest.setSporerecogBid(iotPestrecog.getSporerecogBid());
|
|
|
|
|
+ iotCbdpest.setBzyrecogBid(iotCbdrecog1.getBzyrecogBid());
|
|
|
|
|
+ iotCbdpests.add(iotCbdpest);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //删除过去的人工修正的数据
|
|
|
|
|
+ List<IotBzyspore> cbdpests = mongoTemplate.find(new Query(Criteria.where("bzyrecogBid").is(iotCbdrecog1.getBzyrecogBid())), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+ List<String> pestrecogBids = new ArrayList<>();
|
|
|
|
|
+ for (IotBzyspore cbdpest : cbdpests) {
|
|
|
|
|
+ pestrecogBids.add(cbdpest.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
|
|
+ criteria.and("sporerecogBid").in(pestrecogBids);
|
|
|
|
|
+ criteria.and("sporerecogMarktype").is("0");
|
|
|
|
|
+ Query query1 = new Query(criteria);
|
|
|
|
|
+ //删除该图片过去的人工修正的虫情识别数据
|
|
|
|
|
+ List<IotSporerecog> iotPestrecogs1 = mongoTemplate.findAllAndRemove(query1, IotSporerecog.class, "IotSporerecog");
|
|
|
|
|
+ List<String> pestrecogBids2 = new ArrayList<>();
|
|
|
|
|
+ for (IotSporerecog iotPestrecog : iotPestrecogs1) {
|
|
|
|
|
+ pestrecogBids2.add(iotPestrecog.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+ //删除该图片过去的测报灯虫情信息关联对象数据
|
|
|
|
|
+ mongoTemplate.remove(new Query(Criteria.where("sporerecogBid").in(pestrecogBids2)), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+
|
|
|
|
|
+ UpdateResult updateResult = mongoTemplate.updateFirst(query, update, IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+ mongoTemplate.insert(iotPestrecogs, "IotSporerecog");
|
|
|
|
|
+ mongoTemplate.insert(iotCbdpests, "IotBzyspore");
|
|
|
|
|
+ // 图片标记数量
|
|
|
|
|
+ // 图片标记数量为 0
|
|
|
|
|
+ Query queryPic = new Query(Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid()));
|
|
|
|
|
+ Update updatePic = new Update();
|
|
|
|
|
+ updatePic.set("bzyimgRecognum", recordNum);
|
|
|
|
|
+ updatePic.set("bzyimgModifieddate", DateUtils.dateTimeNow());
|
|
|
|
|
+ UpdateResult updateIotBzyimgResult = mongoTemplate.updateFirst(queryPic, updatePic, IotBzyimg.class, "IotBzyimg");
|
|
|
|
|
+
|
|
|
|
|
+ return (int) updateResult.getModifiedCount();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int artificialReset(IotBzyrecog iotCbdrecog) {
|
|
|
|
|
+ Criteria cri = Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid());
|
|
|
|
|
+ IotBzyrecog iotCbdrecog1 = mongoTemplate.findOne(new Query(cri),
|
|
|
|
|
+ IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+ if (iotCbdrecog1 == null) {
|
|
|
|
|
+ throw new BizException(ErrorCode.INVALID_PARAMETER.getCode(), "该图片未有识别记录");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Query query = new Query(Criteria.where("_id").is(iotCbdrecog1.getId()));
|
|
|
|
|
+ Update update = new Update();
|
|
|
|
|
+ update.set("bzyrecogMarktype", iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ update.set("bzyrecogManualmark", null);
|
|
|
|
|
+ update.set("bzyrecogDelstatus", "1");
|
|
|
|
|
+
|
|
|
|
|
+ UpdateResult updateResult = mongoTemplate.updateFirst(query, update, IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 图片标记数量为 0
|
|
|
|
|
+ Query queryPic = new Query(Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid()));
|
|
|
|
|
+ Update updatePic = new Update();
|
|
|
|
|
+ updatePic.set("bzyimgRecognum", 0);
|
|
|
|
|
+ updatePic.set("bzyimgModifieddate", DateUtils.dateTimeNow());
|
|
|
|
|
+ UpdateResult updateIotBzyimgResult = mongoTemplate.updateFirst(queryPic, updatePic, IotBzyimg.class, "IotBzyimg");
|
|
|
|
|
+ //删除过去的人工修正的数据
|
|
|
|
|
+ List<IotBzyspore> cbdpests = mongoTemplate.find(new Query(Criteria.where("bzyrecogBid").is(iotCbdrecog1.getBzyrecogBid())), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+ List<String> pestrecogBids = new ArrayList<>();
|
|
|
|
|
+ for (IotBzyspore cbdpest : cbdpests) {
|
|
|
|
|
+ pestrecogBids.add(cbdpest.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
|
|
+ criteria.and("sporerecogBid").in(pestrecogBids);
|
|
|
|
|
+ criteria.and("sporerecogMarktype").is("0");
|
|
|
|
|
+ Query query1 = new Query(criteria);
|
|
|
|
|
+ //删除该图片过去的人工修正的虫情识别数据
|
|
|
|
|
+ List<IotSporerecog> iotPestrecogs1 = mongoTemplate.findAllAndRemove(query1, IotSporerecog.class, "IotSporerecog");
|
|
|
|
|
+ List<String> pestrecogBids2 = new ArrayList<>();
|
|
|
|
|
+ for (IotSporerecog iotPestrecog : iotPestrecogs1) {
|
|
|
|
|
+ pestrecogBids2.add(iotPestrecog.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+ //删除该图片过去的测报灯虫情信息关联对象数据
|
|
|
|
|
+ mongoTemplate.remove(new Query(Criteria.where("sporerecogBid").in(pestrecogBids2)), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+ return (int) updateResult.getModifiedCount();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<IotYfBzyPolylineResVo> polyline(IotBzydataReqVo reqVo) {
|
|
|
|
|
+
|
|
|
|
|
+ String devBid = reqVo.getDevBid();
|
|
|
|
|
+ if (StringUtils.isEmpty(devBid)) {
|
|
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备标识不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ String startTime = reqVo.getStartTime();
|
|
|
|
|
+ String endTime = reqVo.getEndTime();
|
|
|
|
|
+ String unit = "day";
|
|
|
|
|
+ Criteria criteria = new Criteria().and("devBid").is(devBid);
|
|
|
|
|
+ //必须设置起止时间
|
|
|
|
|
+ if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)){
|
|
|
|
|
+ //限制 起止时间
|
|
|
|
|
+ criteria = criteria.andOperator(
|
|
|
|
|
+ Criteria.where("bzydataCreatedDate").gte(startTime),
|
|
|
|
|
+ Criteria.where("bzydataCreatedDate").lte(endTime)
|
|
|
|
|
+ );
|
|
|
|
|
+ //选择 数据粒度
|
|
|
|
|
+ unit = MongoUtil.getDateTruncUnit(startTime, endTime);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "查询时间不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ;
|
|
|
|
|
+ /**
|
|
|
|
|
+ * $mtch {"devBid": devBid, "$add": [{"bzydataCreatedDate": {"$gte": startDate}}, {"bzydataCreatedDate": {"$lte": endDate}}]}
|
|
|
|
|
+ */
|
|
|
|
|
+ MatchOperation matchOperation = Aggregation.match(criteria);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * $project
|
|
|
|
|
+ * {
|
|
|
|
|
+ * "ah": "$bzydataContent.ah",
|
|
|
|
|
+ * "time": "$bzydataCreatedDate"
|
|
|
|
|
+ * }
|
|
|
|
|
+ */
|
|
|
|
|
+ ProjectionOperation projectionOperation = Aggregation.project()
|
|
|
|
|
+ .and("bzydataContent.ah").as("ah")
|
|
|
|
|
+ .and("bzydataContent.at").as("at")
|
|
|
|
|
+ .and("bzydataContent.pre_temp").as("pre_temp")
|
|
|
|
|
+ .and("bzydataCreatedDate").as("time");
|
|
|
|
|
+ /**
|
|
|
|
|
+ * $project
|
|
|
|
|
+ * {
|
|
|
|
|
+ * "at": {$convert: {input: '$at', to: 'double', onError: -99, onNull: -99}},
|
|
|
|
|
+ * "time": {$dateTrunc: {date: {$toDate: '$createDate'}, unit: 'minute'}}
|
|
|
|
|
+ * }
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ ProjectionOperation projectionOperation2 = Aggregation.project()
|
|
|
|
|
+ .andExpression("{$convert: {input: '$at', to: 'double', onError: -99, onNull: -99}}").as("at")
|
|
|
|
|
+ .andExpression("{$convert: {input: '$ah', to: 'double', onError: -99, onNull: -99}}").as("ah")
|
|
|
|
|
+ .andExpression("{$convert: {input: '$pre_temp', to: 'double', onError: -99, onNull: -99}}").as("pre_temp")
|
|
|
|
|
+ .andExpression("{$dateTrunc: {date: {$toDate: '$time'}, unit:'" + unit + "'}}").as("time");
|
|
|
|
|
+
|
|
|
|
|
+ Criteria criteria2 = new Criteria()
|
|
|
|
|
+ .and("at").gt(-99)
|
|
|
|
|
+ .and("ah").gt(-99)
|
|
|
|
|
+ .and("pre_temp").gt(-99);
|
|
|
|
|
+ MatchOperation matchOperation2 = Aggregation.match(criteria2);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * {
|
|
|
|
|
+ * "$group" : {
|
|
|
|
|
+ * "_id" : "$createDate", "ah" : { "$avg" : "$ah"}, "at" : { "$avg" : "$at"}, "pre_temp" : { "$avg" : "$pre_temp"}, "cv" : { "$avg" : "$cv"}, "ct" : { "$sum" : "$ct"}
|
|
|
|
|
+ * }
|
|
|
|
|
+ * }
|
|
|
|
|
+ */
|
|
|
|
|
+ GroupOperation groupOperation = Aggregation.group("time")
|
|
|
|
|
+ .avg("ah").as("ah")
|
|
|
|
|
+ .avg("at").as("at")
|
|
|
|
|
+ .avg("pre_temp").as("pre_temp");
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * { "$project" : { "ct" : 1, "pre_temp" : { "$trunc" : ["$pre_temp", 3]}, "time" : { "$dateToString" : { "format" : "%Y-%m-%d %H:%M:%S", "date" : "$_id"}}}}
|
|
|
|
|
+ */
|
|
|
|
|
+ ProjectionOperation projectionOperation3 = Aggregation.project()
|
|
|
|
|
+ .andExpression("{$trunc: {'$pre_temp', 1}}").as("pre_temp")
|
|
|
|
|
+ .andExpression("{$trunc: {'$ah', 1}}").as("ah")
|
|
|
|
|
+ .andExpression("{$trunc: {'$at', 1}}").as("at")
|
|
|
|
|
+ .andExpression("{$dateToString: {format: '%Y-%m-%d %H:%M:%S', date: '$_id'}}").as("time");
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * { "$sort" : { "time" : 1}}
|
|
|
|
|
+ */
|
|
|
|
|
+ SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "time");
|
|
|
|
|
+
|
|
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
|
|
+ matchOperation,
|
|
|
|
|
+ projectionOperation,
|
|
|
|
|
+ projectionOperation2,
|
|
|
|
|
+ matchOperation2,
|
|
|
|
|
+ groupOperation,
|
|
|
|
|
+ projectionOperation3,
|
|
|
|
|
+ sortOperation
|
|
|
|
|
+ );
|
|
|
|
|
+ List<IotYfBzyPolylineResVo> iotYfBzyPolylineResVoList = mongoService.aggregate(
|
|
|
|
|
+ IotBzydata.class, aggregation, IotYfBzyPolylineResVo.class
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ return iotYfBzyPolylineResVoList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getSporeRecogImgOssUrl(String base64, String imgId) {
|
|
|
|
|
+ if (base64.contains(",")) {
|
|
|
|
|
+ base64 = base64.split(",")[1];
|
|
|
|
|
+ }
|
|
|
|
|
+ byte[] bytesFile = Base64.decode(base64);
|
|
|
|
|
+ return OSSFactory.build().uploadSuffix(bytesFile, "jpg", ResConstants.BizType.BZY_RECOGNITION_MARKERS_AI.getPath(), imgId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int diseaseUpdate(IotBzyrecog iotCbdrecog) {
|
|
|
|
|
+ //TODO 改写成病害识别 共用原有的孢子表,通过一个类型字段区分是孢子还是病害 :1、 IotBzyrecog BzyrecogType 1为孢子,2为病害
|
|
|
|
|
+ Criteria cri = Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid());
|
|
|
|
|
+ Query query2 = new Query(cri);
|
|
|
|
|
+ IotBzyimg iotCbdimg = mongoTemplate.findOne(query2, IotBzyimg.class, "IotBzyimg");
|
|
|
|
|
+ if (iotCbdimg == null) {
|
|
|
|
|
+ throw new IotBizException(IotErrorCode.FAILURE.getCode(), iotCbdrecog.getBzyimgBid() + "数据不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ IotBzyrecog iotCbdrecog1 = mongoTemplate.findOne(new Query(cri),
|
|
|
|
|
+ IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+ if (iotCbdrecog1 == null) {
|
|
|
|
|
+ //如果未查到,新增一条
|
|
|
|
|
+ iotCbdrecog1 = new IotBzyrecog();
|
|
|
|
|
+ iotCbdrecog1.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogBid(iotCbdrecog1.getUUId());
|
|
|
|
|
+ iotCbdrecog1.setBzyimgBid(iotCbdimg.getBzyimgBid());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogType("2");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogAddr(iotCbdimg.getBzyimgAddr());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogManualaddr("");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogManualmark(iotCbdrecog.getBzyrecogManualmark());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogResult(iotCbdrecog.getBzyrecogResult());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogMarktype(iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogMachinemark("");
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogCreator(SecurityUtils.getUserId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogModifier(SecurityUtils.getUserId());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogCreatedDate(iotCbdimg.getBzyimgCreatedDate());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogModifieddate(iotCbdimg.getBzyimgModifieddate());
|
|
|
|
|
+ iotCbdrecog1.setBzyrecogDelstatus("0");
|
|
|
|
|
+
|
|
|
|
|
+ mongoTemplate.insert(iotCbdrecog1, "IotBzyrecog");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Query query = new Query(Criteria.where("_id").is(iotCbdrecog1.getId()));
|
|
|
|
|
+ Update update = new Update();
|
|
|
|
|
+ update.set("bzyrecogMarktype", iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ String bzyrecogManualmark = iotCbdrecog.getBzyrecogManualmark();
|
|
|
|
|
+ update.set("bzyrecogManualmark", bzyrecogManualmark);
|
|
|
|
|
+
|
|
|
|
|
+ String recogBid = iotCbdrecog1.getId();
|
|
|
|
|
+ JSONArray recogArray = JSONArray.parseArray(bzyrecogManualmark);
|
|
|
|
|
+
|
|
|
|
|
+ List<JSONObject> points = new ArrayList<>();
|
|
|
|
|
+ // 统计数量
|
|
|
|
|
+ long recordNum = 0;
|
|
|
|
|
+ List<JSONObject> points2 = new ArrayList<>();
|
|
|
|
|
+ if (recogArray != null) {
|
|
|
|
|
+ for (Object o : JSONArray.parseArray(bzyrecogManualmark)) {
|
|
|
|
|
+ points.add((JSONObject) o);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //病害信息
|
|
|
|
|
+ List<IotSpore> iotPests = iotPestService.selectIotSporeList(null);
|
|
|
|
|
+ Map<String, String> pestMap = iotPests.stream().collect(Collectors.toMap(IotSpore::getSporeName, IotSpore::getSporeBid));
|
|
|
|
|
+
|
|
|
|
|
+ //设备信息
|
|
|
|
|
+ IotDevice resVo = iotDeviceService.selectIotDeviceByDevBid(iotCbdimg.getDevBid());
|
|
|
|
|
+ //统计病害数量
|
|
|
|
|
+ Map<String, Integer> pest = new HashMap<>();
|
|
|
|
|
+ recordNum = points.size();
|
|
|
|
|
+ //IotSporerecog存储
|
|
|
|
|
+ for (JSONObject point : points) {
|
|
|
|
|
+ points2.add(point);
|
|
|
|
|
+ //判断人工修正的是否有新的病害名字,有的新的新增,并存储
|
|
|
|
|
+ for (String s : point.keySet()) {
|
|
|
|
|
+ if (pestMap.get(s) == null) {
|
|
|
|
|
+
|
|
|
|
|
+ //新增病害
|
|
|
|
|
+ IotSpore iotPest = new IotSpore();
|
|
|
|
|
+ iotPest.setCId(CustomerIdUtil.getCustomerId());
|
|
|
|
|
+ iotPest.setSporeBid(iotPest.getUUId());
|
|
|
|
|
+ iotPest.setSporeName(s);
|
|
|
|
|
+ iotPest.setSporeId(iotPest.getSporeBid());
|
|
|
|
|
+ iotPest.setSporeCreator(SecurityUtils.getUserId());
|
|
|
|
|
+ iotPest.setSporeCreatedDate(DateUtils.dateTimeNow());
|
|
|
|
|
+ iotPest.setSporeDelstatus("0");
|
|
|
|
|
+ iotPest.setSporeInsertType("2");
|
|
|
|
|
+ iotPestService.insertIotSpore(iotPest);
|
|
|
|
|
+
|
|
|
|
|
+ pestMap.put(iotPest.getSporeName(), iotPest.getSporeBid());
|
|
|
|
|
+ JSONObject point2 = new JSONObject();
|
|
|
|
|
+ point2.put(iotPest.getSporeId(), point.get(s));
|
|
|
|
|
+ points2.add(point2);
|
|
|
|
|
+ points2.remove(point);
|
|
|
|
|
+ pest.put(iotPest.getSporeName(), pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ pest.put(s, pest.get(s) == null ? 1 : pest.get(s) + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String bzyrecogManualBase64 = iotCbdrecog.getBzyrecogManualBase64();
|
|
|
|
|
+ if (StringUtils.isNotEmpty(bzyrecogManualBase64)) {
|
|
|
|
|
+ String imgUrl = getSporeRecogImgOssUrl(bzyrecogManualBase64, recogBid);
|
|
|
|
|
+ update.set("bzyrecogManualaddr", imgUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //IotSporerecog进行存储
|
|
|
|
|
+ List<IotSporerecog> iotPestrecogs = new ArrayList<>();
|
|
|
|
|
+ List<IotBzyspore> iotCbdpests = new ArrayList<>();
|
|
|
|
|
+ for (String s : pest.keySet()) {
|
|
|
|
|
+ IotSporerecog iotPestrecog = new IotSporerecog();
|
|
|
|
|
+ iotPestrecog.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotPestrecog.setDevBid(iotCbdimg.getDevBid());
|
|
|
|
|
+ iotPestrecog.setSporeBusid(pestMap.get(s));
|
|
|
|
|
+ iotPestrecog.setDevtypeBid(resVo.getDevtypeBid());
|
|
|
|
|
+ iotPestrecog.setSporerecogBid(iotPestrecog.getUUId());
|
|
|
|
|
+ iotPestrecog.setSporerecogMarktype(iotCbdrecog.getBzyrecogMarktype());
|
|
|
|
|
+ iotPestrecog.setSporerecogNum(pest.get(s).longValue());
|
|
|
|
|
+ iotPestrecog.setSporerecogCreatedDate(DateUtils.dateTimeNow());
|
|
|
|
|
+ iotPestrecogs.add(iotPestrecog);
|
|
|
|
|
+
|
|
|
|
|
+ IotBzyspore iotCbdpest = new IotBzyspore();
|
|
|
|
|
+ iotCbdpest.setCId(iotCbdimg.getCId());
|
|
|
|
|
+ iotCbdpest.setBzysporeBid(iotCbdpest.getUUId());
|
|
|
|
|
+ iotCbdpest.setSporerecogBid(iotPestrecog.getSporerecogBid());
|
|
|
|
|
+ iotCbdpest.setBzyrecogBid(iotCbdrecog1.getBzyrecogBid());
|
|
|
|
|
+ iotCbdpests.add(iotCbdpest);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //删除过去的人工修正的数据
|
|
|
|
|
+ List<IotBzyspore> cbdpests = mongoTemplate.find(new Query(Criteria.where("bzyrecogBid").is(iotCbdrecog1.getBzyrecogBid())), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+ List<String> pestrecogBids = new ArrayList<>();
|
|
|
|
|
+ for (IotBzyspore cbdpest : cbdpests) {
|
|
|
|
|
+ pestrecogBids.add(cbdpest.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
|
|
+ criteria.and("sporerecogBid").in(pestrecogBids);
|
|
|
|
|
+ criteria.and("sporerecogMarktype").is("0");
|
|
|
|
|
+ Query query1 = new Query(criteria);
|
|
|
|
|
+ //删除该图片过去的人工修正的虫情识别数据
|
|
|
|
|
+ List<IotSporerecog> iotPestrecogs1 = mongoTemplate.findAllAndRemove(query1, IotSporerecog.class, "IotSporerecog");
|
|
|
|
|
+ List<String> pestrecogBids2 = new ArrayList<>();
|
|
|
|
|
+ for (IotSporerecog iotPestrecog : iotPestrecogs1) {
|
|
|
|
|
+ pestrecogBids2.add(iotPestrecog.getSporerecogBid());
|
|
|
|
|
+ }
|
|
|
|
|
+ //删除该图片过去的测报灯虫情信息关联对象数据
|
|
|
|
|
+ mongoTemplate.remove(new Query(Criteria.where("sporerecogBid").in(pestrecogBids2)), IotBzyspore.class, "IotBzyspore");
|
|
|
|
|
+
|
|
|
|
|
+ UpdateResult updateResult = mongoTemplate.updateFirst(query, update, IotBzyrecog.class, "IotBzyrecog");
|
|
|
|
|
+ mongoTemplate.insert(iotPestrecogs, "IotSporerecog");
|
|
|
|
|
+ mongoTemplate.insert(iotCbdpests, "IotBzyspore");
|
|
|
|
|
+ // 图片标记数量
|
|
|
|
|
+ // 图片标记数量为 0
|
|
|
|
|
+ Query queryPic = new Query(Criteria.where("bzyimgBid").is(iotCbdrecog.getBzyimgBid()));
|
|
|
|
|
+ Update updatePic = new Update();
|
|
|
|
|
+ updatePic.set("bzyimgRecognum", recordNum);
|
|
|
|
|
+ updatePic.set("bzyimgModifieddate", DateUtils.dateTimeNow());
|
|
|
|
|
+ UpdateResult updateIotBzyimgResult = mongoTemplate.updateFirst(queryPic, updatePic, IotBzyimg.class, "IotBzyimg");
|
|
|
|
|
+
|
|
|
|
|
+ return (int) updateResult.getModifiedCount();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|