sql_tools.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 is_delete=0 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='{}' and is_delete=0;
  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 delete_layout_typesetting_vir(sql_session, user_id, typesetting_name):
  98. sql = '''
  99. update layout_typesetting lt
  100. set is_delete=1
  101. where user_id ='{}' and name = '{}';
  102. '''.format(user_id, typesetting_name)
  103. print(sql)
  104. sql_session.execute(sql)
  105. sql_session.commit()
  106. def get_plan_typesetting_rough(sql_session, user_id, typesetting_name):
  107. sql = '''
  108. select typesetting,name,create_time,update_time from ad_plan_typesetting lt
  109. where user_id ='{}' and is_delete=0 and name like '%{}%';
  110. '''.format(user_id, typesetting_name)
  111. print(sql)
  112. cursor = sql_session.execute(sql)
  113. lines = cursor.fetchall()
  114. result_list = [line for line in lines]
  115. return result_list
  116. def get_ad_plan_typesetting(sql_session, user_id, typesetting_name):
  117. sql = '''
  118. select typesetting from ad_plan_typesetting lt
  119. where user_id ='{}' and name='{}' and is_delete=0;
  120. '''.format(user_id, typesetting_name)
  121. cursor = sql_session.execute(sql)
  122. lines = cursor.fetchall()
  123. if lines:
  124. result_list = lines[0][0]
  125. return result_list
  126. def delete_ad_plan_typesetting_vir(sql_session, user_id, typesetting_name):
  127. sql = '''
  128. update ad_plan_typesetting lt
  129. set is_delete=1
  130. where user_id ='{}' and name = '{}';
  131. '''.format(user_id, typesetting_name)
  132. print(sql)
  133. sql_session.execute(sql)
  134. sql_session.commit()
  135. def get_plan_record(sql_session, user_id):
  136. pass
  137. sql = '''
  138. select foo.*,wechat_info.wechat_id_info from (select typesetting_list.user_id , typesetting_list.name,
  139. record_list.service_name ,record_list.wechat_name,
  140. typesetting_list.create_time,record_list.status,
  141. typesetting_list.typesetting
  142. from
  143. ad_plan_typesetting as typesetting_list left join
  144. action_record as record_list
  145. on object_name=name
  146. where typesetting_list.user_id ='{user_id}'
  147. ) as foo left join wechat_info
  148. on foo.service_name =wechat_info.service_name and foo.wechat_name = wechat_info.wechat_name ;
  149. '''.format(user_id=user_id)
  150. cursor = sql_session.execute(sql)
  151. lines = cursor.fetchall()
  152. result = [line for line in lines]
  153. return result
  154. def get_undo_action(sql_session, user_id):
  155. # TODO:sql 里面添加doing,error状态的挑选
  156. sql = '''
  157. select action_type ,wechat_name ,service_name
  158. from action_record
  159. where user_id='{}' and status ='todo' ;
  160. '''.format(user_id)
  161. cursor = sql_session.execute(sql)
  162. lines = cursor.fetchall()
  163. result_list = [line for line in lines]
  164. return result_list
  165. def get_ad_status(sql_session, user_id):
  166. sql = '''
  167. select action_type ,wechat_name ,service_name,max(update_time ),max(create_time),status from action_record
  168. where user_id='{}'
  169. group by action_type ,wechat_name ,service_name,status ;
  170. '''.format(user_id)
  171. cursor = sql_session.execute(sql)
  172. lines = cursor.fetchall()
  173. result = [line for line in lines]
  174. return result
  175. def get_action_status(sql_session, user_id):
  176. # TODO:sql 里面添加doing,error状态的挑选
  177. sql = '''
  178. select count(*)
  179. from action_record
  180. where user_id='{}' and status ='doing' ;
  181. '''.format(user_id)
  182. cursor = sql_session.execute(sql)
  183. lines = cursor.fetchall()
  184. result = lines[0][0]
  185. return result
  186. def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name, wechat_name):
  187. print('get in action record ', service_name, wechat_name, res)
  188. status = 'done' if res['sucess'] else 'error'
  189. action_type = json.dumps({'action_type': action_type, 'object_name': object_name})
  190. print(action_type)
  191. update_res = action_table.update() \
  192. .where(action_table.c.service_name == service_name) \
  193. .where(action_table.c.wechat_name == wechat_name) \
  194. .where(action_table.c.user_id == user_id) \
  195. .where(action_table.c.action_type == action_type) \
  196. .values({
  197. action_table.c.status: status,
  198. action_table.c.result: res['result_info']
  199. })
  200. # values 添加两个列
  201. sql_session.execute(update_res)
  202. sql_session.commit()
  203. def check_layout_alive(sql_session, service_name, wechat_name, layout_name):
  204. sql = '''
  205. select count(*) from action_record ar
  206. where service_name='{service_name}'
  207. and wechat_name='{wechat_name}'
  208. and action_type like '%layout_create%'
  209. and action_type like '%{layout_name}%'
  210. and status='done'
  211. '''.format(service_name=service_name, wechat_name=wechat_name, layout_name=layout_name)
  212. print(sql)
  213. cursor = sql_session.execute(sql)
  214. num = cursor.fetchall()[0][0]
  215. return num
  216. def check_plan_alive(sql_session, service_name, wechat_name, plan_name):
  217. sql = '''
  218. select count(*) from action_record ar
  219. where service_name='{service_name}'
  220. and wechat_name='{wechat_name}'
  221. and action_type like '%ad_plan_create%'
  222. and action_type like '%{plan_name}%'
  223. and status='done'
  224. '''.format(service_name=service_name, wechat_name=wechat_name, plan_name=plan_name)
  225. print('plan ', sql)
  226. cursor = sql_session.execute(sql)
  227. num = cursor.fetchall()[0][0]
  228. return num
  229. def get_wechat_info(sql_session, user_id):
  230. sql = '''
  231. select service_name ,wechat_name from wechat_info
  232. where user_id ='{}' ;
  233. '''.format(user_id)
  234. cursor = sql_session.execute(sql)
  235. lines = cursor.fetchall()
  236. result_list = [line for line in lines]
  237. return result_list
  238. def get_wechat_cookies(sql_session, user_id):
  239. sql = '''
  240. select cookies from wechat_cookies where user_id='{}'
  241. '''.format(user_id)
  242. cursor = sql_session.execute(sql)
  243. lines = cursor.fetchall()
  244. print(type(lines), lines)
  245. if lines:
  246. return lines[0][0]