#!/usr/bin/env python3 # -*- coding: utf-8 -*- """用户115 高尿酸 后续修正 ① 撤销 猪头肉(7522) 的 -50(口径:猪头肉为面肉,非动物内脏)→ 还原 -34 + 写逆转日志 ② 服药任务 5369-5372:category 打卡任务→使用任务,action_type checkin→use;同步 plan_json """ import pymysql, json, datetime DB = dict(host='mysql-internet-cn-north-1-23feae22680e4dfa.rds.jdcloud.com', port=3306, user='cfc', password='cfc@1314', database='zxyj', charset='utf8mb4') USER_ID, MEMBER_ID, FAMILY_ID, REPORT_ID = 115, 75, 119, 64 PLAN_ID = 30 MED_TASK_IDS = [5369, 5370, 5371, 5372] conn = pymysql.connect(**DB) cur = conn.cursor() def q(sql, args=None): cur.execute(sql, args) return cur.fetchall() def run(sql, args=None): cur.execute(sql, args) return cur.rowcount now = datetime.datetime.now() # ---------- ① 撤销猪头肉 -50 ---------- rows = q('SELECT id, food_id, food_name, index_score FROM food_recommend_idx WHERE user_id=%s AND food_id=7522', (USER_ID,)) for r in rows: idx_id, food_id, food_name, cur_score = r if cur_score == -84: run('UPDATE food_recommend_idx SET index_score=%s WHERE id=%s', (-34, idx_id)) run('INSERT INTO food_recommend_idx_log ' '(user_id, food_id, food_name, previous_index, new_index, health_report_id, change_reason, changed_at) ' 'VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', (USER_ID, food_id, food_name, cur_score, -34, REPORT_ID, '口径修正:猪头肉非动物内脏(面肉),撤销高尿酸-50调整', now)) print('[1] 猪头肉 %s -> -34(已撤销)' % food_name) elif cur_score == -34: print('[1] 猪头肉已是 -34,无需撤销') else: print('[1] 猪头肉当前指数异常: %s,跳过' % cur_score) # ---------- ② 服药任务 → 使用任务 ---------- if not MED_TASK_IDS: print('[2] 无服药任务ID,跳过') else: ph = ','.join(str(x) for x in MED_TASK_IDS) b = q('SELECT id, title, category, action_type FROM tasks WHERE id IN (%s)' % ph) if len(b) != 4: print('[2] 命中 %d 条服药任务(预期4):%s' % (len(b), [x[0] for x in b])) for tid, title, cat, act in b: run('UPDATE tasks SET category=%s, action_type=%s WHERE id=%s', ('使用任务', 'use', tid)) print('[2] 任务 %s「%s」: %s/%s -> 使用任务/use' % (tid, title, cat, act)) # 同步 plan_json pj = q('SELECT plan_json FROM health_plans WHERE id=%s', (PLAN_ID,))[0][0] root = pj if isinstance(pj, dict) else json.loads(pj) changed = 0 for sec in root.get('sections', []): for t in sec.get('tasks', []): if t.get('title', '').startswith('服用:'): if t.get('action_type') != 'use': t['action_type'] = 'use' changed += 1 if changed: run('UPDATE health_plans SET plan_json=%s, updated_at=%s WHERE id=%s', (json.dumps(root, ensure_ascii=False), now, PLAN_ID)) print('[2] plan_json 已同步 %d 条服药任务 action_type -> use' % changed) else: print('[2] plan_json 无需变更') conn.commit() print('=== 完成 ===') conn.close()