Browse Source

Merge remote-tracking branch 'origin/20200908' into 20200908

hym 4 years ago
parent
commit
c2646c5630

+ 1 - 1
meter-reading-common/src/main/java/com/huaxu/zoniot/repository/MeterReadRecordRepository.java

@@ -11,5 +11,5 @@ import org.springframework.data.mongodb.repository.MongoRepository;
  * @Version 1.0
  */
 public interface MeterReadRecordRepository extends MongoRepository<MeterReadRecord,Long> {
-
+    MeterReadRecord findByReadDateAndDeviceId(Integer readDate,Long deviceId);
 }

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

@@ -128,6 +128,7 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
                             log.info("insert meter read record to mongo failed ,record = {}",JSON.toJSONString(record));
                         }
                     }
+                    meterReadRecordListForMongo.clear();
                 }catch (Exception e){
                     e.printStackTrace();
                     log.error("Batch insert meter read record to mongo failed !",e);
@@ -342,10 +343,13 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
 //            }
             // 根据抄表日期及水表判断抄表记录是否存在
             MeterReadRecord record = findRecordByMeterIdAndReadDate(waterMeter,readDay);
+
             if(record == null){
+                log.info("can't find record , create record now ,meter = {} ",waterMeter.getDeviceId());
                 // 抄表记录不存在,则生成今日未抄表记录
                 record = buildUnReadRecord(waterMeter,readDay);
                 if(record != null){
+                    log.info("save record now ,record = {} ",JSON.toJSONString(record));
                     meterReadRecordMapper.insert(record);
                     // 同时也插入一份到mongo中
                     meterReadRecordRepository.insert(record);
@@ -387,15 +391,26 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
             meterReadRecordMapper.updateByPrimaryKeySelective(update) ;
             // 同时也将mongo抄表记录更新
             try {
-                record.setLastSendData(getMeasuringValueMap(measuringData));
-                update.setLastCost(todayCost);
-                record.setReadData(currentReading);
-                record.setLastValid(currentReading);
-                record.setReadStatus(Constants.READ);
-                record.setDateUpdate(new Date());
-                record.setReadTime(sendTime);
-                record.setUpdateBy(Constants.SYS_FLAG);
-                meterReadRecordRepository.save(record);
+                MeterReadRecord mongoRecord = findRecordByMeterIdAndReadDateFromMongo(waterMeter,readDay);
+                if(mongoRecord == null){
+                    log.warn("meter read record is not saving in mongo ,meter id ={}",waterMeter.getDeviceId());
+                    mongoRecord = buildUnReadRecord(waterMeter,readDay);
+                    meterReadRecordRepository.insert(mongoRecord);
+                }
+                else{
+                    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(record);
+                }
             }catch (Exception e){
                 e.printStackTrace();
                 log.error("Update meter record to mongo failed!",e);
@@ -473,6 +488,10 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
         return record;
     }
 
+    public MeterReadRecord findRecordByMeterIdAndReadDateFromMongo(WaterMeter waterMeter, Integer readDate){
+        MeterReadRecord record = meterReadRecordRepository.findByReadDateAndDeviceId(readDate, waterMeter.getDeviceId());
+        return record;
+    }
     public static void main(String[] args) {
 //        MeasuringData md1 = new MeasuringData();
 //        md1.setMeasuringCode("TIME");