_insert_notify_config_menu.py 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. """Upsert 通知配置菜单 (Id=1310300010106) 到共享库。
  3. 做法:以 审批统计(Id=1310300010105) 记录为模板深拷贝,覆盖 Id / Title / Path / Name / Component / Icon / OrderNo。
  4. 然后将所有已授权给 105 的 Role / Tenant 复制到 106。
  5. """
  6. import pymysql
  7. conn = pymysql.connect(
  8. host='123.60.180.165', port=3306,
  9. user='aidopremote', password='1234567890aiDOP#',
  10. database='aidopdev', charset='utf8mb4', autocommit=True,
  11. )
  12. try:
  13. with conn.cursor(pymysql.cursors.DictCursor) as cur:
  14. cur.execute("SELECT 1 FROM SysMenu WHERE Id = 1310300010106")
  15. if cur.fetchone():
  16. print("menu exists, skip insert")
  17. else:
  18. cur.execute("SELECT * FROM SysMenu WHERE Id = 1310300010105")
  19. src = cur.fetchone()
  20. if not src:
  21. print("!! 源菜单 1310300010105 不存在,中止。先执行菜单种子修复脚本。")
  22. raise SystemExit(1)
  23. new_row = dict(src)
  24. new_row['Id'] = 1310300010106
  25. new_row['Title'] = '通知配置'
  26. new_row['Path'] = '/aidop/flowManage/flowNotifyConfig'
  27. new_row['Name'] = 'flowNotifyConfig'
  28. new_row['Component'] = '/approvalFlow/notifyConfig/index'
  29. new_row['Icon'] = 'ele-Bell'
  30. new_row['OrderNo'] = 150
  31. new_row['Remark'] = '审批流通知模板与渠道配置管理'
  32. cols = list(new_row.keys())
  33. placeholders = ", ".join(["%s"] * len(cols))
  34. col_list = ", ".join(f"`{c}`" for c in cols)
  35. sql = f"INSERT INTO SysMenu ({col_list}) VALUES ({placeholders})"
  36. cur.execute(sql, list(new_row.values()))
  37. print(f"menu inserted: 通知配置 Id=1310300010106")
  38. # Role 授权复制
  39. cur.execute("SELECT DISTINCT RoleId FROM SysRoleMenu WHERE MenuId = 1310300010105")
  40. role_ids = [r['RoleId'] for r in cur.fetchall()]
  41. print(f"roles with 审批统计 access: {role_ids}")
  42. for rid in role_ids:
  43. cur.execute("SELECT 1 FROM SysRoleMenu WHERE RoleId=%s AND MenuId=1310300010106", (rid,))
  44. if cur.fetchone():
  45. continue
  46. cur.execute("SELECT * FROM SysRoleMenu WHERE RoleId=%s AND MenuId=1310300010105 LIMIT 1", (rid,))
  47. src = cur.fetchone()
  48. if not src:
  49. continue
  50. new_row = dict(src)
  51. new_row['Id'] = None
  52. new_row['MenuId'] = 1310300010106
  53. # 使用 UUID_SHORT
  54. cols = [c for c in new_row.keys() if c != 'Id']
  55. placeholders = ", ".join(["%s"] * len(cols))
  56. col_list = ", ".join(f"`{c}`" for c in cols) + ", `Id`"
  57. sql = f"INSERT INTO SysRoleMenu ({col_list}) VALUES ({placeholders}, UUID_SHORT())"
  58. cur.execute(sql, [new_row[c] for c in cols])
  59. print(f" granted RoleMenu: Role={rid} -> Menu=106")
  60. # Tenant 授权复制
  61. cur.execute("SELECT DISTINCT TenantId FROM SysTenantMenu WHERE MenuId = 1310300010105")
  62. t_ids = [r['TenantId'] for r in cur.fetchall()]
  63. print(f"tenants with 审批统计 access: {t_ids}")
  64. for tid in t_ids:
  65. cur.execute("SELECT 1 FROM SysTenantMenu WHERE TenantId=%s AND MenuId=1310300010106", (tid,))
  66. if cur.fetchone():
  67. continue
  68. cur.execute("SELECT * FROM SysTenantMenu WHERE TenantId=%s AND MenuId=1310300010105 LIMIT 1", (tid,))
  69. src = cur.fetchone()
  70. if not src:
  71. continue
  72. new_row = dict(src)
  73. new_row['Id'] = None
  74. new_row['MenuId'] = 1310300010106
  75. cols = [c for c in new_row.keys() if c != 'Id']
  76. placeholders = ", ".join(["%s"] * len(cols))
  77. col_list = ", ".join(f"`{c}`" for c in cols) + ", `Id`"
  78. sql = f"INSERT INTO SysTenantMenu ({col_list}) VALUES ({placeholders}, UUID_SHORT())"
  79. cur.execute(sql, [new_row[c] for c in cols])
  80. print(f" granted TenantMenu: Tenant={tid} -> Menu=106")
  81. print("verify:")
  82. cur.execute("SELECT Id, Pid, Title, Path, Component FROM SysMenu WHERE Id=1310300010106")
  83. print(cur.fetchone())
  84. cur.execute("SELECT COUNT(*) AS n FROM SysRoleMenu WHERE MenuId=1310300010106")
  85. print("RoleMenu count:", cur.fetchone())
  86. cur.execute("SELECT COUNT(*) AS n FROM SysTenantMenu WHERE MenuId=1310300010106")
  87. print("TenantMenu count:", cur.fetchone())
  88. finally:
  89. conn.close()
  90. print("done")