learshaw 3 дней назад
Родитель
Сommit
0de6aa79d3

+ 101 - 28
ems-ui-cloud/src/views/devmgr/meterRead/index.vue

@@ -339,9 +339,9 @@ export default {
     increase() {
       const { meterReading, magnification, lastReading } = this.fillForm
       if (meterReading && magnification) {
-        const reading = parseFloat(meterReading) || 0
-        const last = parseFloat(lastReading) || 0
-        const mag = parseFloat(magnification) || 1
+        const reading = parseInt(meterReading) || 0
+        const last = parseInt(lastReading) || 0
+        const mag = parseInt(magnification) || 1
         return (reading - last) * mag
       }
       return 0
@@ -533,8 +533,8 @@ export default {
       // 默认选择当前月份(但用户可以修改)
       this.fillForm.selectedMonth = this.currentMonth
 
-      // 获取最新的抄表记录作为上次记录
-      this.updateLastReadingInfo()
+      // 获取最新的抄表记录作为上次记录(支持跨年份查询)
+      await this.updateLastReadingInfo()
 
       this.fillFormOpen = true
     },
@@ -566,7 +566,7 @@ export default {
     },
 
     // 处理月份选择变化
-    handleMonthChange(selectedMonth) {
+    async handleMonthChange(selectedMonth) {
       if (!selectedMonth) return
 
       this.monthWarning = ''
@@ -577,39 +577,59 @@ export default {
         return
       }
 
-      // 根据选择的月份更新上次抄表信息
-      this.updateLastReadingInfo(selectedMonth)
+      // 根据选择的月份更新上次抄表信息(支持跨年份查询)
+      await this.updateLastReadingInfo(selectedMonth)
     },
 
     // 更新上次抄表信息
-    updateLastReadingInfo(selectedMonth = null) {
+    async updateLastReadingInfo(selectedMonth = null) {
       const targetMonth = selectedMonth || this.fillForm.selectedMonth
-      if (!targetMonth || !this.recListForm.recList.length) {
-        // 没有历史记录时
+      if (!targetMonth) {
         this.fillForm.lastTime = ''
         this.fillForm.lastReading = ''
         this.fillForm.magnification = 1
         return
       }
 
-      this.findPreviousRecord(targetMonth)
-    },
+      // 先尝试在当前年份记录中查找
+      const lastRecord = this.findPreviousRecordInList(targetMonth, this.recListForm.recList)
+      if (lastRecord) {
+        this.fillForm.lastTime = lastRecord.meterTime
+        this.fillForm.lastReading = lastRecord.meterReading
+        if (!this.fillForm.magnification) {
+          this.fillForm.magnification = lastRecord.magnification || 1
+        }
+        return
+      }
 
-    // 查找指定月份之前的记录作为上次抄表信息
-    findPreviousRecord(targetMonth) {
-      if (!targetMonth || !this.recListForm.recList.length) {
+      // 如果当前年份没有找到,递归向前查询之前的年份
+      const targetYear = parseInt(targetMonth.substring(0, 4))
+      const prevRecord = await this.findPreviousRecordCrossYear(targetYear, targetMonth)
+      if (prevRecord) {
+        this.fillForm.lastTime = prevRecord.meterTime
+        this.fillForm.lastReading = prevRecord.meterReading
+        if (!this.fillForm.magnification) {
+          this.fillForm.magnification = prevRecord.magnification || 1
+        }
+      } else {
+        // 没找到任何记录
         this.fillForm.lastTime = ''
-        this.fillForm.lastReading = ''
+        this.fillForm.lastReading = '0'
         this.fillForm.magnification = this.fillForm.magnification || 1
-        return
+      }
+    },
+
+    // 在指定记录列表中查找目标月份之前的记录
+    findPreviousRecordInList(targetMonth, recordList) {
+      if (!recordList || recordList.length === 0) {
+        return null
       }
 
       // 查找目标月份之前最近的一条记录
-      const sortedRecords = [...this.recListForm.recList].sort((a, b) => {
+      const sortedRecords = [...recordList].sort((a, b) => {
         return b.meterMonth.localeCompare(a.meterMonth)
       })
 
-      let lastRecord = null
       for (const record of sortedRecords) {
         // 跳过当前正在修改的记录(如果是修改操作)
         if (!this.ifAdd && record.meterMonth === targetMonth) {
@@ -617,20 +637,71 @@ export default {
         }
 
         if (record.meterMonth < targetMonth) {
-          lastRecord = record
-          break
+          return record
+        }
+      }
+      return null
+    },
+
+    // 跨年份递归查询上一条记录
+    async findPreviousRecordCrossYear(year, targetMonth) {
+      // 查询指定年份的记录
+      try {
+        const response = await listMeterReadingByParam({
+          deviceCode: this.fillForm.deviceCode,
+          year: year.toString(),
+          orderFlag: 'desc'
+        })
+        const records = response.data || []
+
+        if (records.length > 0) {
+          // 在该年份中找到目标月份之前的记录
+          const lastRecord = this.findPreviousRecordInList(targetMonth, records)
+          if (lastRecord) {
+            return lastRecord
+          }
+          // 如果该年份的所有记录都大于目标月份(比如目标是202601,但2026年只有06月之后的记录)
+          // 则取该年份最早的一条记录
+          const sortedRecords = [...records].sort((a, b) => {
+            return a.meterMonth.localeCompare(b.meterMonth)
+          })
+          if (sortedRecords.length > 0 && sortedRecords[0].meterMonth < targetMonth) {
+            // 取该年份最大月份(最新的记录)
+            const maxRecord = [...records].sort((a, b) => {
+              return b.meterMonth.localeCompare(a.meterMonth)
+            })[0]
+            return maxRecord
+          }
         }
+
+        // 如果该年份没有记录,递归查询前一年
+        if (year > 2000) {
+          return await this.findPreviousRecordCrossYear(year - 1, targetMonth)
+        }
+        return null
+      } catch (error) {
+        console.error(`查询${year}年记录失败:`, error)
+        return null
+      }
+    },
+
+    // 查找指定月份之前的记录作为上次抄表信息(兼容旧方法,用于修改记录时)
+    findPreviousRecord(targetMonth) {
+      if (!targetMonth || !this.recListForm.recList.length) {
+        this.fillForm.lastTime = ''
+        this.fillForm.lastReading = ''
+        this.fillForm.magnification = this.fillForm.magnification || 1
+        return
       }
 
+      const lastRecord = this.findPreviousRecordInList(targetMonth, this.recListForm.recList)
       if (lastRecord) {
         this.fillForm.lastTime = lastRecord.meterTime
         this.fillForm.lastReading = lastRecord.meterReading
-        // 如果当前记录没有设置倍率,使用上次记录的倍率
         if (!this.fillForm.magnification) {
           this.fillForm.magnification = lastRecord.magnification || 1
         }
       } else {
-        // 如果没找到更早的记录,说明这是最早的记录
         this.fillForm.lastTime = ''
         this.fillForm.lastReading = '0'
         this.fillForm.magnification = this.fillForm.magnification || 1
@@ -653,7 +724,9 @@ export default {
         // 设置提交数据
         const submitData = {
           ...this.fillForm,
-          increase: this.increase,
+          meterReading: parseInt(this.fillForm.meterReading) || 0,
+          lastReading: parseInt(this.fillForm.lastReading) || 0,
+          increase: parseInt(this.increase) || 0,
           meterMonth: this.fillForm.selectedMonth, // 使用选择的月份
           meterTime: this.generateMeterTime(this.fillForm.selectedMonth) // 根据月份生成抄表时间
         }
@@ -715,8 +788,8 @@ export default {
 
     // 处理抄表示数输入
     handleMeterReadingInput(value) {
-      // 只允许输入数字和小数点
-      this.fillForm.meterReading = value.replace(/[^\d.]/g, '')
+      // 只允许输入整数
+      this.fillForm.meterReading = value.replace(/[^\d]/g, '')
     },
 
     // 验证抄表示数
@@ -749,7 +822,7 @@ export default {
     },
 
     // 格式化数字
-    formatNumber(value, decimals = 2) {
+    formatNumber(value, decimals = 0) {
       if (value === null || value === undefined || value === '') return '-'
       const num = parseFloat(value)
       if (isNaN(num)) return '-'

+ 0 - 3
ems-ui-cloud/src/views/mgr/powerstore.vue

@@ -564,9 +564,6 @@ export default {
       if (this.areaMap[areaCode]) {
         return this.areaMap[areaCode]
       }
-      // 根据已知的区域代码返回名称
-      if (areaCode === '321283124S3001') return '常泰北区'
-      if (areaCode === '321283124S3002') return '常泰南区'
       return areaCode
     },