From 494ecebcc70f395eb5665aaa581b10d6fc4d7df0 Mon Sep 17 00:00:00 2001 From: XingQue Date: Wed, 29 Jul 2026 03:58:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=81=E7=A7=BB=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E8=BF=87=E6=88=B7=E7=BB=9F=E4=B8=80=20naive/aware=20=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=AF=94=E8=BE=83=EF=BC=8C=E9=81=BF=E5=85=8D=20confir?= =?UTF-8?q?m=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- jituan/services/club_migrate.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/jituan/services/club_migrate.py b/jituan/services/club_migrate.py index 8b77fa7..8a19873 100644 --- a/jituan/services/club_migrate.py +++ b/jituan/services/club_migrate.py @@ -307,7 +307,12 @@ def _preview_huiyuan(uid: str, club_id: str, role: str) -> List[dict]: for r in rows: name = (r.jieshao or '').strip() or name_map.get(r.huiyuan_id) or '会员' exp = _aware(r.daoqi_time) - exp_text = timezone.localtime(exp).strftime('%Y-%m-%d %H:%M') if exp else '' + if exp is None: + exp_text = '' + elif timezone.is_aware(exp): + exp_text = timezone.localtime(exp).strftime('%Y-%m-%d %H:%M') + else: + exp_text = exp.strftime('%Y-%m-%d %H:%M') out.append({ 'key': f'{role}.huiyuan:{r.huiyuan_id}', 'role': role, @@ -383,7 +388,7 @@ def _add_huiyuan_duration(*, to_uid: str, to_club: str, huiyuan_id: str, remain, """给目标用户对应会员加时长(已有则累加剩余,否则从现在起算)。""" from products.models import Huiyuangoumai - now = _now() + now = _now_aware() remain = remain if remain and remain.total_seconds() > 0 else None if remain is None: return None, {} @@ -427,13 +432,13 @@ def _add_huiyuan_duration(*, to_uid: str, to_club: str, huiyuan_id: str, remain, def _transfer_huiyuan(*, from_uid, to_uid, from_club, to_club, role) -> List[dict]: from products.models import Huiyuan, Huiyuangoumai - now = _now() + now = _now_aware() rows = list( Huiyuangoumai.objects.select_for_update().filter( yonghu_id=str(from_uid), club_id=from_club, huiyuan_zhuangtai=1, - daoqi_time__gt=now, + daoqi_time__gt=_now(), ) ) out = [] @@ -525,13 +530,25 @@ def _transfer_huiyuan(*, from_uid, to_uid, from_club, to_club, role) -> List[dic def _aware(dt): + """与 timezone.now() 对齐 aware/naive,避免混比炸 500。""" if dt is None: return None - if timezone.is_naive(dt): - return timezone.make_aware(dt, timezone.get_current_timezone()) + now_sample = timezone.now() + if timezone.is_aware(now_sample): + if timezone.is_naive(dt): + return timezone.make_aware(dt, timezone.get_current_timezone()) + return dt + # 项目未开 USE_TZ:一律 naive + if timezone.is_aware(dt): + return timezone.make_naive(dt, timezone.get_current_timezone()) return dt +def _now_aware(): + """业务比较用的当前时间(与 _aware 同一套时区规则)。""" + return _aware(_now()) or _now() + + def check_conditions(plan: ClubMigratePlan, from_uid: str) -> Tuple[bool, str]: """返回 (ok, msg)。""" cond = plan.condition_json or {}