提交 fcc37794 authored 作者: 刘擎阳's avatar 刘擎阳

1.优化

上级 50774994
...@@ -30,6 +30,7 @@ class CmcIntegrationController(http.Controller): ...@@ -30,6 +30,7 @@ class CmcIntegrationController(http.Controller):
tracking_number = data.get('tracking_no') tracking_number = data.get('tracking_no')
event_type = data.get('event') event_type = data.get('event')
event_time_str = data.get('event_time') event_time_str = data.get('event_time')
event_time_local = data.get('event_time_local')
if not tracking_number or not event_type: if not tracking_number or not event_type:
return self._json_response({'status': 'error', 'message': 'Missing parameters'}) return self._json_response({'status': 'error', 'message': 'Missing parameters'})
...@@ -49,7 +50,8 @@ class CmcIntegrationController(http.Controller): ...@@ -49,7 +50,8 @@ class CmcIntegrationController(http.Controller):
'flight_dynamic_ids': [(0, 0, { 'flight_dynamic_ids': [(0, 0, {
'type': dynamic_type, 'type': dynamic_type,
'event_time_utc': event_time, 'event_time_utc': event_time,
'timezone': 'UTC', 'event_time_local': event_time_local,
# 'timezone': 'UTC',
'status': 'valid', 'status': 'valid',
})] })]
}) })
......
...@@ -1516,15 +1516,15 @@ class FreightFlightDynamic(models.Model): ...@@ -1516,15 +1516,15 @@ class FreightFlightDynamic(models.Model):
], string="类型", required=True) ], string="类型", required=True)
# Odoo 的 Datetime 字段在数据库层面默认就是以 0时区 (UTC) 存储的 # Odoo 的 Datetime 字段在数据库层面默认就是以 0时区 (UTC) 存储的
event_time_utc = fields.Datetime(string="发生时间(UTC)", default=fields.Datetime.now, required=True) event_time_utc = fields.Datetime(string="发生时间", default=fields.Datetime.now, required=True)
# 时区选择和计算本地时间 # 时区选择和计算本地时间
@api.model @api.model
def _tz_get(self): def _tz_get(self):
return [(x, x) for x in pytz.all_timezones] return [(x, x) for x in pytz.all_timezones]
timezone = fields.Selection(_tz_get, string='发生地时区', default='UTC', required=True) # timezone = fields.Selection(_tz_get, string='发生地时区', default='UTC', required=True)
event_time_local = fields.Char(string="发生地时间(含时区)", compute='_compute_local_time', store=True) event_time_local = fields.Char(string="发生地时间")
# 记录人,默认当前用户 # 记录人,默认当前用户
user_id = fields.Many2one('res.users', string="记录人", default=lambda self: self.env.user, readonly=True) user_id = fields.Many2one('res.users', string="记录人", default=lambda self: self.env.user, readonly=True)
...@@ -1535,42 +1535,62 @@ class FreightFlightDynamic(models.Model): ...@@ -1535,42 +1535,62 @@ class FreightFlightDynamic(models.Model):
('invalid', '无效') ('invalid', '无效')
], string="状态", default='valid', readonly=True) ], string="状态", default='valid', readonly=True)
@api.depends('event_time_utc', 'timezone') # @api.depends('event_time_utc', 'timezone')
def _compute_local_time(self): # def _compute_local_time(self):
"""根据 UTC 时间和选择的时区,计算并拼接出当地时间字符串""" # """根据 UTC 时间和选择的时区,计算并拼接出当地时间字符串"""
for record in self: # for record in self:
if record.event_time_utc and record.timezone: # if record.event_time_utc and record.timezone:
tz = pytz.timezone(record.timezone) # tz = pytz.timezone(record.timezone)
utc_dt = pytz.utc.localize(record.event_time_utc) # utc_dt = pytz.utc.localize(record.event_time_utc)
local_dt = utc_dt.astimezone(tz) # local_dt = utc_dt.astimezone(tz)
# 格式化为:2023-10-25 14:30:00 (Asia/Shanghai) # # 格式化为:2023-10-25 14:30:00 (Asia/Shanghai)
record.event_time_local = f"{local_dt.strftime('%Y-%m-%d %H:%M:%S')} ({record.timezone})" # record.event_time_local = f"{local_dt.strftime('%Y-%m-%d %H:%M:%S')} ({record.timezone})"
else: # else:
record.event_time_local = False # record.event_time_local = False
# @api.model
# def create(self, vals):
# """
# 拦截创建方法:
# 新增时,查找该提单下同类型的【有效】记录,将其置为【无效】。
# 确保只有最新的一条是【有效】的。
# """
# bill_id = vals.get('bill_id')
# dyn_type = vals.get('type')
#
# if bill_id and dyn_type:
# # 寻找同类型且状态为有效的老记录
# existing_valid_records = self.search([
# ('bill_id', '=', bill_id),
# ('type', '=', dyn_type),
# ('status', '=', 'valid')
# ])
# if existing_valid_records:
# existing_valid_records.write({'status': 'invalid'})
#
# # 强制将新创建的记录设为有效
# vals['status'] = 'valid'
# return super(FreightFlightDynamic, self).create(vals)
@api.model @api.model
def create(self, vals): def create(self, vals):
""" vals['status'] = 'valid'
拦截创建方法: record = super(FreightFlightDynamic, self).create(vals)
新增时,查找该提单下同类型的【有效】记录,将其置为【无效】。 if record.bill_id and record.type:
确保只有最新的一条是【有效】的。
"""
bill_id = vals.get('bill_id')
dyn_type = vals.get('type')
if bill_id and dyn_type:
# 寻找同类型且状态为有效的老记录
existing_valid_records = self.search([ existing_valid_records = self.search([
('bill_id', '=', bill_id), ('bill_id', '=', record.bill_id.id),
('type', '=', dyn_type), ('type', '=', record.type),
('status', '=', 'valid') ('status', '=', 'valid'),
('id', '!=', record.id) # 核心注意点:必须排除刚刚创建的这条新记录自己
]) ])
if existing_valid_records: if existing_valid_records:
existing_valid_records.write({'status': 'invalid'}) existing_valid_records.write({'status': 'invalid'})
if record.event_time_utc:
# 强制将新创建的记录设为有效 if record.type == 'departure':
vals['status'] = 'valid' record.bill_id.write({'actual_departure_time': record.event_time_utc})
return super(FreightFlightDynamic, self).create(vals) elif record.type == 'arrival':
record.bill_id.write({'actual_arrival_time': record.event_time_utc})
return record
def unlink(self): def unlink(self):
"""拦截删除方法:只能新增,不能删除""" """拦截删除方法:只能新增,不能删除"""
......
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
<tree string="动态记录" editable="top" delete="0" default_order="create_date desc"> <tree string="动态记录" editable="top" delete="0" default_order="create_date desc">
<field name="type"/> <field name="type"/>
<field name="event_time_utc"/> <field name="event_time_utc"/>
<field name="timezone"/> <!-- <field name="timezone"/>-->
<field name="event_time_local"/> <field name="event_time_local"/>
<field name="user_id"/> <field name="user_id"/>
<field name="create_date" string="创建时间" readonly="1"/> <field name="create_date" string="创建时间" readonly="1"/>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论