sql_tools.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(sql_session, user_id, typesetting_name):
  78. sql = '''
  79. select typesetting from layout_typesetting lt
  80. where user_id ='{}' and name='{}';
  81. '''.format(user_id, typesetting_name)
  82. cursor = sql_session.execute(sql)
  83. lines = cursor.fetchall()
  84. if lines:
  85. result_list = lines[0][0]
  86. return result_list
  87. def get_ad_plan_typesetting(sql_session, user_id, typesetting_name):
  88. sql = '''
  89. select typesetting from ad_plan_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_undo_action(sql_session, user_id):
  98. # TODO:sql 里面添加doing,error状态的挑选
  99. sql = '''
  100. select action_type ,wechat_name ,service_name
  101. from action_record
  102. where user_id='{}' and status ='todo' ;
  103. '''.format(user_id)
  104. cursor = sql_session.execute(sql)
  105. lines = cursor.fetchall()
  106. result_list = [line for line in lines]
  107. return result_list
  108. def get_ad_status(sql_session, user_id):
  109. sql = '''
  110. select action_type ,wechat_name ,service_name,max(update_time ),max(create_time),status from action_record
  111. where user_id='{}'
  112. group by action_type ,wechat_name ,service_name,status ;
  113. '''.format(user_id)
  114. cursor = sql_session.execute(sql)
  115. lines = cursor.fetchall()
  116. result = [line for line in lines]
  117. return result
  118. def get_action_status(sql_session, user_id):
  119. # TODO:sql 里面添加doing,error状态的挑选
  120. sql = '''
  121. select count(*)
  122. from action_record
  123. where user_id='{}' and status ='doing' ;
  124. '''.format(user_id)
  125. cursor = sql_session.execute(sql)
  126. lines = cursor.fetchall()
  127. result = lines[0][0]
  128. return result
  129. def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name, wechat_name):
  130. print('get in action record ', service_name, wechat_name, res)
  131. status = 'done' if res['sucess'] else 'error'
  132. action_type = json.dumps({'action_type': action_type, 'object_name': object_name})
  133. print(action_type)
  134. update_res = action_table.update() \
  135. .where(action_table.c.service_name == service_name) \
  136. .where(action_table.c.wechat_name == wechat_name) \
  137. .where(action_table.c.user_id == user_id) \
  138. .where(action_table.c.action_type == action_type) \
  139. .values({
  140. action_table.c.status: status,
  141. action_table.c.result: res['result_info']
  142. })
  143. # values 添加两个列
  144. sql_session.execute(update_res)
  145. sql_session.commit()
  146. def check_layout_alive(sql_session, service_name, wechat_name, layout_name):
  147. sql = '''
  148. select count(*) from action_record ar
  149. where service_name='{service_name}'
  150. and wechat_name='{wechat_name}'
  151. and action_type like '%layout_create%'
  152. and action_type like '%{layout_name}%'
  153. and status='done'
  154. '''.format(service_name=service_name, wechat_name=wechat_name, layout_name=layout_name)
  155. print(sql)
  156. cursor = sql_session.execute(sql)
  157. num = cursor.fetchall()[0][0]
  158. return num
  159. def check_plan_alive(sql_session, service_name, wechat_name, plan_name):
  160. sql = '''
  161. select count(*) from action_record ar
  162. where service_name='{service_name}'
  163. and wechat_name='{wechat_name}'
  164. and action_type like '%ad_plan_create%'
  165. and action_type like '%{plan_name}%'
  166. and status='done'
  167. '''.format(service_name=service_name, wechat_name=wechat_name, plan_name=plan_name)
  168. print('plan ', sql)
  169. cursor = sql_session.execute(sql)
  170. num = cursor.fetchall()[0][0]
  171. return num
  172. def get_wechat_info(sql_session, user_id):
  173. sql = '''
  174. select service_name ,wechat_name from wechat_info
  175. where user_id ='{}' ;
  176. '''.format(user_id)
  177. cursor = sql_session.execute(sql)
  178. lines = cursor.fetchall()
  179. result_list = [line for line in lines]
  180. return result_list
  181. def get_wechat_cookies(sql_session, user_id):
  182. sql = '''
  183. select cookies from wechat_cookies where user_id='{}'
  184. '''.format(user_id)
  185. cursor = sql_session.execute(sql)
  186. lines = cursor.fetchall()
  187. print(type(lines), lines)
  188. if lines:
  189. return lines[0][0]