Browse Source

mongo存储bug修复 PengDi@2021/1/21

pengdi@zoniot.com 4 years ago
parent
commit
1b02f1c167

+ 34 - 9
meter-reading-common/src/main/java/com/huaxu/zoniot/service/impl/MeterReadRecordServiceImpl.java

@@ -24,7 +24,11 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.StopWatch;
 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.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.Isolation;
 import org.springframework.transaction.annotation.Propagation;
@@ -57,6 +61,9 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
 
     @Autowired
     MeterReadRecordRepository meterReadRecordRepository ;
+
+    @Autowired
+    MongoTemplate  mongoTemplate ;
     /**
      * 根据任务生成未抄记录
      * @param jobTask
@@ -401,15 +408,33 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
                     if(!mongoRecord.getId().equals(record.getId())){
                         log.info("record id in mysql is not equals in mongo ,{},{}",record.getId(),mongoRecord.getId());
                     }
-                    mongoRecord.setLastSendData(getMeasuringValueMap(measuringData));
-                    mongoRecord.setLastCost(todayCost);
-                    mongoRecord.setReadData(currentReading);
-                    mongoRecord.setLastValid(currentReading);
-                    mongoRecord.setReadStatus(Constants.READ);
-                    mongoRecord.setDateUpdate(new Date());
-                    mongoRecord.setReadTime(sendTime);
-                    mongoRecord.setUpdateBy(Constants.SYS_FLAG);
-                    meterReadRecordRepository.save(mongoRecord);
+                    // 更新Mongo中的抄表记录
+                    Query condition = new Query();
+                    condition.addCriteria(Criteria.where("readDate").is(readDay));
+                    condition.addCriteria(Criteria.where("deviceId").is(waterMeter.getDeviceId()));
+                    condition.addCriteria(Criteria.where("status").is(1));
+
+                    Update param = new Update();
+                    param.set("lastSendData",getMeasuringValueMap(measuringData));
+                    param.set("lastCost",todayCost);
+                    param.set("readData",currentReading);
+                    param.set("lastValid",currentReading);
+                    param.set("readStatus",Constants.READ);
+                    param.set("readTime",sendTime);
+                    param.set("dateUpdate",new Date());
+                    param.set("updateBy",Constants.SYS_FLAG);
+
+                    mongoTemplate.updateFirst(condition,param,MeterReadRecord.class);
+//                  如下写法也不知道为啥会有问题
+//                    mongoRecord.setLastSendData(getMeasuringValueMap(measuringData));
+//                    mongoRecord.setLastCost(todayCost);
+//                    mongoRecord.setReadData(currentReading);
+//                    mongoRecord.setLastValid(currentReading);
+//                    mongoRecord.setReadStatus(Constants.READ);
+//                    mongoRecord.setDateUpdate(new Date());
+//                    mongoRecord.setReadTime(sendTime);
+//                    mongoRecord.setUpdateBy(Constants.SYS_FLAG);
+//                    meterReadRecordRepository.save(mongoRecord);
                 }
             }catch (Exception e){
                 e.printStackTrace();