sql_tools.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import json
  2. # def save_ad_layout_result(layout_info, table_layout):
  3. # insert_layout = table_layout.insert()
  4. # insert_layout = insert_layout.values \
  5. # (id=layout_info['id'],
  6. # userid=layout_info['userid'],
  7. # result=layout_info['result'],
  8. # layout=layout_info['layout'])
  9. # return insert_layout
  10. def save_wechat_cookies(wechat_cookies_info, table_wechat_cookies):
  11. insert_wechat_cookies = table_wechat_cookies.insert()
  12. insert_layout = insert_wechat_cookies.values \
  13. (user_id=wechat_cookies_info['user_id'],
  14. cookies=wechat_cookies_info['cookies']
  15. )
  16. return insert_layout
  17. def save_human_info(human_info, table_human):
  18. insert_human = table_human.insert()
  19. insert_human = insert_human.values \
  20. (service_name=human_info['service_name'],
  21. wechat_name=human_info['wechat_name'],
  22. human_info=human_info['human_info']
  23. )
  24. return insert_human
  25. def save_layout_typesetting_info(layout_typesetting_info, table_layout_typesetting):
  26. insert_layout_typesetting = table_layout_typesetting.insert()
  27. insert_layout_typesetting = insert_layout_typesetting.values \
  28. (typesetting=layout_typesetting_info['typesetting'],
  29. user_id=layout_typesetting_info['user_id'],
  30. name=layout_typesetting_info['name']
  31. )
  32. return insert_layout_typesetting
  33. def save_ad_plan_typesetting_info(ad_plan_typesetting_info, table_ad_plan_typesetting):
  34. # TODO:正常action_record 返回plan_name layout_name有问题
  35. insert_ad_plan_typesetting = table_ad_plan_typesetting.insert()
  36. insert_ad_plan_typesetting = insert_ad_plan_typesetting.values \
  37. (typesetting=ad_plan_typesetting_info['typesetting'],
  38. user_id=ad_plan_typesetting_info['user_id'],
  39. name=ad_plan_typesetting_info['name']
  40. )
  41. return insert_ad_plan_typesetting
  42. def save_wechat_info(wechat_info, table_wechat):
  43. insert_wechat = table_wechat.insert()
  44. insert_wechat = insert_wechat.values \
  45. (service_name=wechat_info['service_name'],
  46. wechat_name=wechat_info['wechat_name'],
  47. user_id=wechat_info['user_id']
  48. )
  49. return insert_wechat
  50. def save_action_record(action_record_info, table_action_record):
  51. # TODO:service_name ,wechat_name,action_type 设置全局唯一
  52. insert_action_record = table_action_record.insert()
  53. insert_action_record = insert_action_record.values \
  54. (service_name=action_record_info['service_name'],
  55. wechat_name=action_record_info['wechat_name'],
  56. user_id=action_record_info['user_id'],
  57. action_type=action_record_info['action_type'],
  58. status=action_record_info['status'],
  59. )
  60. return insert_action_record
  61. def delete_wechat_info(sql_session, user_id):
  62. sql = '''
  63. delete from wechat_info
  64. where user_id = '{}'
  65. '''.format(user_id)
  66. sql_session.execute(sql)
  67. sql_session.commit()
  68. def get_human_info(sql_session, service_name, wechat_name):
  69. sql = '''
  70. select human_info from human_info hi
  71. where service_name='{service_name}' and wechat_name='{wechat_name}' ;
  72. '''.format(service_name=service_name, wechat_name=wechat_name)
  73. cursor = sql_session.execute(sql)
  74. lines = cursor.fetchall()
  75. result_list = lines[0][0]
  76. return result_list
  77. def get_layout_typesetting_rough(sql_session, user_id, typesetting_name):
  78. sql = '''
  79. select typesetting,name,create_time,update_time from layout_typesetting lt
  80. where user_id ='{}' and name like '%{}%';
  81. '''.format(user_id, typesetting_name)
  82. print(sql)
  83. cursor = sql_session.execute(sql)
  84. lines = cursor.fetchall()
  85. result_list = [line for line in lines]
  86. return result_list
  87. def get_layout_typesetting(sql_session, user_id, typesetting_name):
  88. sql = '''
  89. select typesetting from layout_typesetting lt
  90. where user_id ='{}' and name='{}';
  91. '''.format(user_id, typesetting_name)
  92. cursor = sql_session.execute(sql)
  93. lines = cursor.fetchall()
  94. if lines:
  95. result_list = lines[0][0]
  96. return result_list
  97. def get_ad_plan_typesetting(sql_session, user_id, typesetting_name):
  98. sql = '''
  99. select typesetting from ad_plan_typesetting lt
  100. where user_id ='{}' and name='{}';
  101. '''.format(user_id, typesetting_name)
  102. cursor = sql_session.execute(sql)
  103. lines = cursor.fetchall()
  104. if lines:
  105. result_list = lines[0][0]
  106. return result_list
  107. def get_undo_action(sql_session, user_id):
  108. # TODO:sql 里面添加doing,error状态的挑选
  109. sql = '''
  110. select action_type ,wechat_name ,service_name
  111. from action_record
  112. where user_id='{}' and status ='todo' ;
  113. '''.format(user_id)
  114. cursor = sql_session.execute(sql)
  115. lines = cursor.fetchall()
  116. result_list = [line for line in lines]
  117. return result_list
  118. def get_ad_status(sql_session, user_id):
  119. sql = '''
  120. select action_type ,wechat_name ,service_name,max(update_time ),max(create_time),status from action_record
  121. where user_id='{}'
  122. group by action_type ,wechat_name ,service_name,status ;
  123. '''.format(user_id)
  124. cursor = sql_session.execute(sql)
  125. lines = cursor.fetchall()
  126. result = [line for line in lines]
  127. return result
  128. def get_action_status(sql_session, user_id):
  129. # TODO:sql 里面添加doing,error状态的挑选
  130. sql = '''
  131. select count(*)
  132. from action_record
  133. where user_id='{}' and status ='doing' ;
  134. '''.format(user_id)
  135. cursor = sql_session.execute(sql)
  136. lines = cursor.fetchall()
  137. result = lines[0][0]
  138. return result
  139. def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name, wechat_name):
  140. print('get in action record ', service_name, wechat_name, res)
  141. status = 'done' if res['sucess'] else 'error'
  142. action_type = json.dumps({'action_type': action_type, 'object_name': object_name})
  143. print(action_type)
  144. update_res = action_table.update() \
  145. .where(action_table.c.service_name == service_name) \
  146. .where(action_table.c.wechat_name == wechat_name) \
  147. .where(action_table.c.user_id == user_id) \
  148. .where(action_table.c.action_type == action_type) \
  149. .values({
  150. action_table.c.status: status,
  151. action_table.c.result: res['result_info']
  152. })
  153. # values 添加两个列
  154. sql_session.execute(update_res)
  155. sql_session.commit()
  156. def check_layout_alive(sql_session, service_name, wechat_name, layout_name):
  157. sql = '''
  158. select count(*) from action_record ar
  159. where service_name='{service_name}'
  160. and wechat_name='{wechat_name}'
  161. and action_type like '%layout_create%'
  162. and action_type like '%{layout_name}%'
  163. and status='done'
  164. '''.format(service_name=service_name, wechat_name=wechat_name, layout_name=layout_name)
  165. print(sql)
  166. cursor = sql_session.execute(sql)
  167. num = cursor.fetchall()[0][0]
  168. return num
  169. def check_plan_alive(sql_session, service_name, wechat_name, plan_name):
  170. sql = '''
  171. select count(*) from action_record ar
  172. where service_name='{service_name}'
  173. and wechat_name='{wechat_name}'
  174. and action_type like '%ad_plan_create%'
  175. and action_type like '%{plan_name}%'
  176. and status='done'
  177. '''.format(service_name=service_name, wechat_name=wechat_name, plan_name=plan_name)
  178. print('plan ', sql)
  179. cursor = sql_session.execute(sql)
  180. num = cursor.fetchall()[0][0]
  181. return num
  182. def get_wechat_info(sql_session, user_id):
  183. sql = '''
  184. select service_name ,wechat_name from wechat_info
  185. where user_id ='{}' ;
  186. '''.format(user_id)
  187. cursor = sql_session.execute(sql)
  188. lines = cursor.fetchall()
  189. result_list = [line for line in lines]
  190. return result_list
  191. def get_wechat_cookies(sql_session, user_id):
  192. sql = '''
  193. select cookies from wechat_cookies where user_id='{}'
  194. '''.format(user_id)
  195. cursor = sql_session.execute(sql)
  196. lines = cursor.fetchall()
  197. print(type(lines), lines)
  198. if lines:
  199. return lines[0][0]