2026-11-16-user115-followup.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """用户115 高尿酸 后续修正
  4. ① 撤销 猪头肉(7522) 的 -50(口径:猪头肉为面肉,非动物内脏)→ 还原 -34 + 写逆转日志
  5. ② 服药任务 5369-5372:category 打卡任务→使用任务,action_type checkin→use;同步 plan_json
  6. """
  7. import pymysql, json, datetime
  8. DB = dict(host='mysql-internet-cn-north-1-23feae22680e4dfa.rds.jdcloud.com',
  9. port=3306, user='cfc', password='cfc@1314', database='zxyj', charset='utf8mb4')
  10. USER_ID, MEMBER_ID, FAMILY_ID, REPORT_ID = 115, 75, 119, 64
  11. PLAN_ID = 30
  12. MED_TASK_IDS = [5369, 5370, 5371, 5372]
  13. conn = pymysql.connect(**DB)
  14. cur = conn.cursor()
  15. def q(sql, args=None):
  16. cur.execute(sql, args)
  17. return cur.fetchall()
  18. def run(sql, args=None):
  19. cur.execute(sql, args)
  20. return cur.rowcount
  21. now = datetime.datetime.now()
  22. # ---------- ① 撤销猪头肉 -50 ----------
  23. rows = q('SELECT id, food_id, food_name, index_score FROM food_recommend_idx WHERE user_id=%s AND food_id=7522', (USER_ID,))
  24. for r in rows:
  25. idx_id, food_id, food_name, cur_score = r
  26. if cur_score == -84:
  27. run('UPDATE food_recommend_idx SET index_score=%s WHERE id=%s', (-34, idx_id))
  28. run('INSERT INTO food_recommend_idx_log '
  29. '(user_id, food_id, food_name, previous_index, new_index, health_report_id, change_reason, changed_at) '
  30. 'VALUES (%s,%s,%s,%s,%s,%s,%s,%s)',
  31. (USER_ID, food_id, food_name, cur_score, -34, REPORT_ID,
  32. '口径修正:猪头肉非动物内脏(面肉),撤销高尿酸-50调整', now))
  33. print('[1] 猪头肉 %s -> -34(已撤销)' % food_name)
  34. elif cur_score == -34:
  35. print('[1] 猪头肉已是 -34,无需撤销')
  36. else:
  37. print('[1] 猪头肉当前指数异常: %s,跳过' % cur_score)
  38. # ---------- ② 服药任务 → 使用任务 ----------
  39. if not MED_TASK_IDS:
  40. print('[2] 无服药任务ID,跳过')
  41. else:
  42. ph = ','.join(str(x) for x in MED_TASK_IDS)
  43. b = q('SELECT id, title, category, action_type FROM tasks WHERE id IN (%s)' % ph)
  44. if len(b) != 4:
  45. print('[2] 命中 %d 条服药任务(预期4):%s' % (len(b), [x[0] for x in b]))
  46. for tid, title, cat, act in b:
  47. run('UPDATE tasks SET category=%s, action_type=%s WHERE id=%s', ('使用任务', 'use', tid))
  48. print('[2] 任务 %s「%s」: %s/%s -> 使用任务/use' % (tid, title, cat, act))
  49. # 同步 plan_json
  50. pj = q('SELECT plan_json FROM health_plans WHERE id=%s', (PLAN_ID,))[0][0]
  51. root = pj if isinstance(pj, dict) else json.loads(pj)
  52. changed = 0
  53. for sec in root.get('sections', []):
  54. for t in sec.get('tasks', []):
  55. if t.get('title', '').startswith('服用:'):
  56. if t.get('action_type') != 'use':
  57. t['action_type'] = 'use'
  58. changed += 1
  59. if changed:
  60. run('UPDATE health_plans SET plan_json=%s, updated_at=%s WHERE id=%s',
  61. (json.dumps(root, ensure_ascii=False), now, PLAN_ID))
  62. print('[2] plan_json 已同步 %d 条服药任务 action_type -> use' % changed)
  63. else:
  64. print('[2] plan_json 无需变更')
  65. conn.commit()
  66. print('=== 完成 ===')
  67. conn.close()