Files
gzth/attendance_iot/models/attendance_iot_user.py
T
sunjie 3a39e510dc 1. 增加 iot.user 的唯一性.
2. 增加批量拷贝功能
2025-07-24 16:04:36 +08:00

90 lines
3.7 KiB
Python

import json
import urllib
from odoo import api, fields, models, tools
from odoo.exceptions import ValidationError
class AttendanceIotUser(models.Model):
_name = 'attendance.iot.user'
attendance_iot_id = fields.Many2one('attendance.iot', string="设备号")
attendance_address = fields.Char(related='attendance_iot_id.attendance_address', string="设备地址")
employee_id = fields.Many2one('hr.employee', string="员工")
company_id = fields.Many2one(related='employee_id.company_id', string="公司")
id_valid = fields.Date(string="有效日期")
# 从 employee_id 获取.
image_1920 = fields.Many2one('ir.attachment', string="一寸照")
iot_user_id = fields.Char(string='设备上用户的编码')
_sql_constraints = [('unique','UNIQUE(attendance_iot_id,employee_id)','一台设备不允许上传相同的员工信息')]
def unlink(self):
if not self._context.get('iot_sync'):
# 下发删除信息
iot_id = self[0].attendance_iot_id.name
# 如果是主动下发的客户,则使用 employee_id.id 作为用户的编码
self.env['iot.message'].create([{
'name': '删除用户',
'topic': f'iot/device/{iot_id}',
'device_sn': iot_id,
'message_data': json.dumps({
'cmd': 'delMultiUser',
'user_ids': [x.iot_user_id if x.iot_user_id else f'bd{x.employee_id.mobile_phone}' for x in self]
}, ensure_ascii=False)
}])
return super(AttendanceIotUser, self).unlink()
@api.model_create_multi
def create(self, value_list):
res = super(AttendanceIotUser, self).create(value_list)
# 来自设备的同步,不要再写回设备
if self._context.get('iot_sync'):
return res
for r in res:
# 创建下发信息
# 头像, 压缩的图片 data:image/jpeg;base64,
#
add_value = {
'cmd': 'addUser',
'user_id': f'{r.employee_id.mobile_phone}',
'name': r.employee_id.name
}
if r.employee_id.compress_image:
face_template = urllib.parse.quote(
f"data:image/jpeg;base64,{r.employee_id.compress_image.decode('utf-8')}", encoding='utf-8')
add_value.update({'face_template': face_template})
self.env['iot.message'].create([{
'name': '增加用户',
'topic': f'iot/device/{r.attendance_iot_id.name}',
'device_sn': r.attendance_iot_id.name,
'message_data': json.dumps(add_value, ensure_ascii=False)
}])
return res
def action_update_iot_info(self):
if self._context.get('iot_sync'):
return
for record in self:
# 下发信息
update_value = {
'cmd': 'editUser',
'edit_mode': 1,
'user_id': f'bd{record.employee_id.mobile_phone}',
'name': record.employee_id.name
}
if record.employee_id.compress_image:
face_template = urllib.parse.quote(
f"data:image/jpeg;base64,{record.employee_id.compress_image.decode('utf-8')}", encoding='utf-8')
update_value.update({'face_template': face_template})
self.env['iot.message'].create([{
'name': '修改用户信息',
'topic': f'iot/device/{record.attendance_iot_id.name}',
'device_sn': record.attendance_iot_id.name,
'message_data': json.dumps(update_value, ensure_ascii=False)
}])