lihui001 3 سال پیش
والد
کامیت
0b08fa7267

+ 13 - 6
zoniot-rmcp/zoniot-rmcp-core/src/main/java/com/zcxk/rmcp/core/dao/mongo/MeterReadRecordDao.java

@@ -115,7 +115,7 @@ public class MeterReadRecordDao extends BaseDao<MeterReadRecord, String> impleme
     * @param lastCost:
     * @return long
     **/
-    public long updateReadData(long id, int readDate, BigDecimal readData,  BigDecimal lastCost){
+    public long updateReadData(long id, long deviceId, int readDate, BigDecimal readData,  BigDecimal lastCost){
         Query query = new Query();
         query.addCriteria(Criteria.where("id").is(id));
         query.addCriteria(Criteria.where("data.readDate").is(readDate));
@@ -127,15 +127,22 @@ public class MeterReadRecordDao extends BaseDao<MeterReadRecord, String> impleme
         update.set("data.$.createBy", UserUtil.getCurrentUser().getUsername());
         long total =  upsert(query,update).getModifiedCount();
         if (total > 0 ) {
-            Device device = deviceMapper.findById(id);
+            Device device = deviceMapper.findById(deviceId);
             if (device != null) {
                 LocalDateTime localDateTime = device.getLastReceiveTime();
-                int monthLength     = (localDateTime.getMonthValue() + "").length();
-                String monthValue   = monthLength ==  1 ? "0" + localDateTime.getMonthValue() : localDateTime.getMonthValue() + "";
-                int eqDate       = Integer.parseInt(localDateTime.getYear() + "" + monthValue + "" + localDateTime.getDayOfMonth());
+                if (localDateTime == null) {
+                    Device deviceUpdate = new Device();
+                    deviceUpdate.setId(deviceId);
+                    deviceUpdate.setReadData(readData.toPlainString());
+                    deviceMapper.updateByDeviceData(deviceUpdate);
+                    return total;
+                }
+                String month =  (localDateTime.getMonthValue() +"").length() == 1 ? "0" + localDateTime.getMonthValue() : localDateTime.getMonthValue() +"";
+                String days  =  (localDateTime.getDayOfMonth() +"").length() == 1 ? "0" + localDateTime.getDayOfMonth() : localDateTime.getDayOfMonth() +"";
+                int eqDate   =  Integer.parseInt(localDateTime.getYear() + "" + month + "" + days);
                 if (readDate >= eqDate) {
                     Device deviceUpdate = new Device();
-                    deviceUpdate.setId(id);
+                    deviceUpdate.setId(deviceId);
                     deviceUpdate.setReadData(readData.toPlainString());
                     deviceMapper.updateByDeviceData(deviceUpdate);
                 }

+ 8 - 13
zoniot-rmcp/zoniot-rmcp-web/src/main/java/com/zcxk/rmcp/web/service/impl/MeterReadRecordServiceImpl.java

@@ -68,26 +68,21 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
         if (readRecordVo.getData().getReadStatus().equals(ReadStatusEnum.READ.getMessage())) {
             throw BusinessException.builder(RmcpErrorEnum.RMCP_UPDATE_FAIL.getStatus(), "该设备已抄表,不能再更新读数。");
         }
-        long total;
         // 上一次读数
         Double prevReadData = 0D;
-        String queryDate    = dto.getReadDate().replaceAll("-","");
-        LocalDate localDate = DateUtil.parseLocalDate(new Date());
-        int monthLength     = (localDate.getMonthValue() + "").length();
-        String monthValue   = monthLength ==  1 ? "0" + localDate.getMonthValue() : localDate.getMonthValue() + "";
-        String eqDate       = localDate.getYear() + "" + monthValue + "" + localDate.getDayOfMonth();
-        if (!eqDate.equals(queryDate)) {
-            total = meterReadRecordDao.updateReadData(dto.getId(), Integer.parseInt(queryDate), dto.getReadData(), new BigDecimal("0"));
-        } else {
+        int queryDate    = Integer.parseInt(dto.getReadDate().replaceAll("-",""));
+        int currentDate  = DateUtil.parseInt(new Date(), 0);
+        BigDecimal lastCost = new BigDecimal("0");
+        if (queryDate == currentDate) {
             // 获取上一个记录
-            MeterReadRecordVo prevVo = findPrevRecord(dto.getId(), Integer.parseInt(queryDate));
-            if (prevVo != null) {
+            MeterReadRecordVo prevVo = findPrevRecord(dto.getId(), queryDate);
+            if (prevVo != null &&prevVo.getData().getReadData() != null) {
                 prevReadData = prevVo.getData().getReadData();
             }
             // 当前读数 - 上一次读数 = 当前用水量
-            BigDecimal lastCost  = BigDecimalUtils.subtract(dto.getReadData(), prevReadData);
-            total = meterReadRecordDao.updateReadData(dto.getId(), Integer.parseInt(queryDate), dto.getReadData(), lastCost);
+            lastCost  = BigDecimalUtils.subtract(dto.getReadData(), prevReadData);
         }
+        long total = meterReadRecordDao.updateReadData(dto.getId(), readRecordVo.getDeviceId(), queryDate, dto.getReadData(), lastCost);
         if (total == 0) {
             throw BusinessException.builder(RmcpErrorEnum.RMCP_UPDATE_FAIL);
         }