sop.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import json
  2. from sqlite3 import Cursor
  3. from typing import TypedDict, Optional
  4. from config import conf
  5. """
  6. sop_task 相关Model和CURD方法
  7. """
  8. sop_task_status = {
  9. 1: "草稿",
  10. 2: "未开始",
  11. 3: "已开始",
  12. 4: "已结束",
  13. 5: "已禁用",
  14. }
  15. #标签类型:1好友,2群组,3企业微信联系人
  16. sop_task_type = {
  17. 1: "好友",
  18. 2: "群组",
  19. 3: "企业微信联系人",
  20. }
  21. class SopTaskModel(TypedDict):
  22. id: int
  23. sop_task_id: int
  24. created_at: int
  25. updated_at: int
  26. name: str
  27. bot_wxid_list: json
  28. status: int # 状态 1-草稿 2-未开始 3-已开始 4-已结束 5-禁用
  29. type: int # 标签类型:1-好友,2-群组,3-企业微信联系人
  30. wx_wxid: str
  31. token: str
  32. # 获取 sop_task 列表
  33. def sop_task_get_list(cur: Cursor, wx_wxid: str) -> list[SopTaskModel]:
  34. query = "SELECT * FROM sop_task WHERE wx_wxid=? and (status=? OR status=?) ORDER BY created_at DESC"
  35. cur.execute(query, (wx_wxid, 3, 5))
  36. results = cur.fetchall()
  37. msgs: list[SopTaskModel] = []
  38. for result in results:
  39. msg: SopTaskModel = {
  40. "id": result[0],
  41. "sop_task_id": result[1],
  42. "created_at": result[2],
  43. "updated_at": result[3],
  44. "name": result[4],
  45. "bot_wxid_list": result[5],
  46. "status": result[6],
  47. "type": result[7],
  48. "wx_wxid": result[8],
  49. "token": result[9],
  50. }
  51. msgs.append(msg)
  52. return msgs
  53. # 获取 sop_task 列表
  54. def sop_task_query(cur: Cursor, task_id: int) -> Optional[SopTaskModel]:
  55. query = "SELECT * FROM sop_task WHERE sop_task_id = ? ORDER BY created_at DESC LIMIT 1"
  56. cur.execute(query, (task_id,))
  57. result = cur.fetchone()
  58. # print(f"sop_task_query: {result}")
  59. if result is None:
  60. return None
  61. task: SopTaskModel = {
  62. "id": result[0],
  63. "sop_task_id": result[1],
  64. "created_at": result[2],
  65. "updated_at": result[3],
  66. "name": result[4],
  67. "bot_wxid_list": result[5],
  68. "status": result[6],
  69. "type": result[7],
  70. "wx_wxid": result[8],
  71. "token": result[9],
  72. }
  73. return task
  74. # 创建 sop_task 记录
  75. def sop_task_create(cur: Cursor, msg, wx_wxid):
  76. botWxidList = json.dumps(msg['botWxidList'], ensure_ascii=False)
  77. createdAt = int(msg['createdAt'])/1000
  78. updatedAt = int(msg['updatedAt'])/1000
  79. token = str(conf().get("token"))
  80. insert_query = "INSERT INTO sop_task (sop_task_id, created_at, updated_at, name, bot_wxid_list, status, type, wx_wxid, token) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
  81. insert_data = (msg['id'], createdAt, updatedAt, msg['name'], botWxidList, msg['status'], msg['type'], wx_wxid, token)
  82. cur.execute(insert_query, insert_data)
  83. return cur.lastrowid
  84. # 更新 sop_task 状态
  85. def sop_task_update_status(cur: Cursor, id: int, status: int):
  86. update_query = "UPDATE sop_task SET status = ? WHERE id = ?"
  87. update_data = (status, id)
  88. cur.execute(update_query, update_data)
  89. """
  90. sop_stage 相关Model和CURD方法
  91. """
  92. class SopStageModel(TypedDict):
  93. id: int
  94. created_at: int
  95. updated_at: int
  96. name: str
  97. status: int # 状态 1-正常 2-禁用
  98. task_id: int
  99. condition_type: int
  100. condition_operator: int
  101. condition_list: json
  102. action_message: json
  103. action_label_add: json
  104. action_label_del: json
  105. action_forward: json
  106. index_sort: int
  107. # 批量创建多个 sop_stage
  108. def sop_stage_create_many(cur: Cursor, msg_list: list[dict]):
  109. for msg in msg_list:
  110. conditionList = json.dumps(msg['conditionList'], ensure_ascii=False)
  111. actionMessage = json.dumps(msg['actionMessage'], ensure_ascii=False)
  112. actionLabelAdd = json.dumps(msg['actionLabelAdd'], ensure_ascii=False)
  113. actionLabelDel = json.dumps(msg['actionLabelDel'], ensure_ascii=False)
  114. actionForward = json.dumps(msg['actionForward'], ensure_ascii=False)
  115. insert_sql = 'INSERT INTO sop_stage (created_at, updated_at, name, status, task_id, condition_type, condition_operator, condition_list, action_message, action_label_add, action_label_del, action_forward, index_sort) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'
  116. insert_data = (int(msg['createdAt'])/1000, int(msg['updatedAt'])/1000, msg['name'],
  117. msg['status'], msg['taskId'], msg['conditionType'], msg['conditionOperator'],
  118. conditionList, actionMessage, actionLabelAdd, actionLabelDel,
  119. actionForward, msg['indexSort']
  120. )
  121. cur.execute(insert_sql, insert_data)
  122. # 获取阶段记录
  123. def get_stage(cursor: Cursor, organization_id: int):
  124. task_query = "SELECT * FROM sop_task WHERE status = 3 AND organization_id = ?"
  125. task_tuple = (organization_id,)
  126. cursor.execute(task_query, task_tuple)
  127. tasks = cursor.fetchall()
  128. # 遍历tasks,查询每个task下的stage
  129. stages = []
  130. for task in tasks:
  131. sql_query = "SELECT * FROM sop_stage WHERE task_id = ? AND status = 1"
  132. stage_tuple = (task['id'],)
  133. cursor.execute(sql_query, stage_tuple)
  134. stages += cursor.fetchall()
  135. return stages
  136. """
  137. sop_node 相关Model和CURD方法
  138. """
  139. class SopNodeModel(TypedDict):
  140. id: int
  141. created_at: int
  142. updated_at: int
  143. name: str
  144. stage_id: int
  145. parent_id: int
  146. status: int # 状态 1-正常 2-禁用
  147. condition_type: int
  148. condition_list: json
  149. no_reply_condition: int
  150. no_reply_unit: str
  151. action_message: json
  152. action_label_add: json
  153. action_label_del: json
  154. action_forward: json
  155. action_label_add_list: json
  156. action_label_del_list: json
  157. # 创建多个 sop_node
  158. def sop_node_create_many(cur: Cursor, msg_list: list[dict]):
  159. for msg in msg_list:
  160. conditionList = json.dumps(msg['conditionList'], ensure_ascii=False)
  161. actionMessage = json.dumps(msg['actionMessage'], ensure_ascii=False)
  162. actionLabelAdd = json.dumps(msg['actionLabelAdd'], ensure_ascii=False)
  163. actionLabelDel = json.dumps(msg['actionLabelDel'], ensure_ascii=False)
  164. actionForward = json.dumps(msg['actionForward'], ensure_ascii=False)
  165. actionLabelAddList = None if msg['actionLabelAddList'] is None else json.dumps(msg['actionLabelAddList'], ensure_ascii=False)
  166. actionLabelDelList = None if msg['actionLabelDelList'] is None else json.dumps(msg['actionLabelDelList'], ensure_ascii=False)
  167. sql = 'INSERT INTO sop_node (created_at, updated_at, name, stage_id, parent_id, status, condition_type, condition_list, no_reply_condition, no_reply_unit, action_message, action_label_add, action_label_del, action_forward, action_label_add_list, action_label_del_list) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
  168. data = (int(msg['createdAt'])/1000, int(msg['updatedAt'])/1000, msg['name'],
  169. msg['stageId'], msg['parentId'], msg['status'], msg['conditionType'],
  170. conditionList, msg['noReplyCondition'], msg['noReplyUnit'],
  171. actionMessage, actionLabelAdd, actionLabelDel, actionForward,
  172. actionLabelAddList, actionLabelDelList
  173. )
  174. cur.execute(sql, data)
  175. # 获取 sop_node 列表
  176. def sop_node_get_list_by_parent(cur: Cursor, parent_id: int, stage_id: Optional[int]) -> list[SopNodeModel]:
  177. if stage_id is not None:
  178. sql_query = "SELECT * FROM sop_node WHERE parent_id = ? AND stage_id = ?"
  179. cur.execute(sql_query, (parent_id, stage_id))
  180. results = cur.fetchall()
  181. else:
  182. sql_query = "SELECT * FROM sop_node WHERE parent_id = ?"
  183. cur.execute(sql_query, (parent_id,))
  184. results = cur.fetchall()
  185. msgs: list[SopNodeModel] = []
  186. for result in results:
  187. msg: SopNodeModel = {
  188. "id": result[0],
  189. "created_at": result[1],
  190. "updated_at": result[2],
  191. "name": result[3],
  192. "stage_id": result[4],
  193. "parent_id": result[5],
  194. "status": result[6],
  195. "condition_type": result[7],
  196. "condition_list": result[8],
  197. "no_reply_condition": result[9],
  198. "no_reply_unit": result[10],
  199. "action_message": result[11],
  200. "action_label_add": result[12],
  201. "action_label_del": result[13],
  202. "action_forward": result[14],
  203. "action_label_add_list": result[15],
  204. "action_label_del_list": result[16]
  205. }
  206. msgs.append(msg)
  207. return msgs
  208. # 获取 sop_node 列表
  209. def sop_node_get_list_by_condition(cur: Cursor) -> list[SopNodeModel]:
  210. sql_query = "SELECT * FROM sop_node WHERE no_reply_condition = 0"
  211. cur.execute(sql_query, ())
  212. results = cur.fetchall()
  213. msgs: list[SopNodeModel] = []
  214. for result in results:
  215. msg: SopNodeModel = {
  216. "id": result[0],
  217. "created_at": result[1],
  218. "updated_at": result[2],
  219. "name": result[3],
  220. "stage_id": result[4],
  221. "parent_id": result[5],
  222. "status": result[6],
  223. "condition_type": result[7],
  224. "condition_list": result[8],
  225. "no_reply_condition": result[9],
  226. "no_reply_unit": result[10],
  227. "action_message": result[11],
  228. "action_label_add": result[12],
  229. "action_label_del": result[13],
  230. "action_forward": result[14],
  231. "action_label_add_list": result[15],
  232. "action_label_del_list": result[16]
  233. }
  234. msgs.append(msg)
  235. return msgs