Files
gzth/yuthon_property/wizard/property_meter_wizard.py
T
2025-09-20 18:06:32 +08:00

155 lines
5.8 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _, SUPERUSER_ID
from odoo.exceptions import ValidationError, UserError
from pypinyin import pinyin, Style
import xlrd
import base64
import datetime
from datetime import datetime
class PropertyMeterWizard(models.Model):
_name = 'property.meter.wizard'
excel_file = fields.Binary(string="上传Excel表")
excel_name = fields.Char(string="文件名")
def confirm(self):
"""
导入excel文件,按行读取数据并处理,将1-9月数据写入明细行
"""
if not self.excel_file:
raise ValidationError('请上传文件。')
# 解码并打开Excel文件
try:
book = xlrd.open_workbook(file_contents=base64.decodebytes(self.excel_file))
sh = book.sheet_by_index(0)
except Exception as e:
raise ValidationError(f'文件解析错误: {str(e)}')
# 按行处理数据
for rx in range(1, sh.nrows): # 跳过表头行
row = sh.row(rx)
last_read = 0.0 # 初始读数,第一个月的上次读数为0
# 处理1-9月份数据
for month in range(1, 10):
last_read = self.property_meter_data(row, last_read, month)
def property_meter_data(self, row, last_read, month):
"""
处理单月数据,查找主记录并创建/更新明细行
"""
# 从Excel行获取基础数据
epitope_values = row[0].value if len(row) > 0 else ''
unit_number = row[1].value if len(row) > 1 else ''
installation_location = row[2].value if len(row) > 2 else ''
name = row[3].value if len(row) > 3 else '' # 仪表编号(关键标识)
# 处理仪表倍率
gauge_magnification = None
if len(row) > 5 and row[5].value:
try:
gauge_magnification = float(row[5].value)
except ValueError:
gauge_magnification = 1 # 默认为1
# 处理最大位数
maximum_count = None
if len(row) > 6 and row[6].value:
try:
maximum_count = float(row[6].value)
except ValueError:
maximum_count = None
# 处理类型转换
type_dict = {'公共': 'public', '收费': 'fees', '': ''}
meter_type = type_dict.get(row[7].value if len(row) > 7 else '', '')
# 处理物业信息
property_id = None
if installation_location: # 使用安装位置匹配物业
property_search = self.env['yuthon.property'].search(
[('name', '=', installation_location)],
limit=1
)
if property_search:
property_id = property_search
# 确定当前读数列索引(1-8月正常,9月特殊处理)
current_read_index = month + 9 if month < 9 else 9
# 处理当月数据
meter_date = datetime(2025, month, 15) if month < 9 else None
current_read = None
if month < 9 and len(row) > current_read_index and row[current_read_index].value:
try:
current_read = float(row[current_read_index].value)
except ValueError:
current_read = None
# 查找或创建主表记录(按仪表编号)
archives = self.env['property.meter.archives'].search([('name', '=', name),
('is_push', '=', False)
], limit=1)
if not archives:
archives = self.env['property.meter.archives'].create({
'name': name,
'epitope_values': epitope_values,
'unit_number': unit_number,
'installation_location': installation_location,
'type': meter_type,
'gauge_magnification': gauge_magnification,
'maximum_count': maximum_count,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
})
else:
# 更新已有记录的部分字段
archives.write({
'epitope_values': epitope_values,
'unit_number': unit_number,
'installation_location': installation_location,
'type': meter_type,
'gauge_magnification': gauge_magnification,
'maximum_count': maximum_count,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
})
# 准备明细行数据
line_vals = {
'name': name,
'archives_id': archives.id,
'epitope_values': epitope_values,
'unit_number': unit_number,
'type': meter_type,
'gauge_magnification': gauge_magnification or 1,
'maximum_count': maximum_count,
'last_read': last_read,
'current_read': current_read,
'last_meter': meter_date,
'last_reading_time': meter_date,
'yuthon_property_ids': [(4, property_id.id)] if property_id else [],
}
self.env['meter.archives.line'].sudo().create(line_vals)
# 返回当前读数作为下个月的上次读数
return current_read if (month < 9 and current_read is not None) else last_read
# if property_id:
# clear_entries = [(5,)]
# new_meter_entry = (0, 0, {
# 'property_meter_archives_id': meter_id.id,
# })
# updated_meters = clear_entries + [new_meter_entry]
# property_id.write({
# 'is_electricity_meter': True,
# 'energy_meter_ids': updated_meters
# })
# if month < 9 and current_read is not None:
# return current_read
# else:
# return last_read