sql_tools.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. for k, v in ad_plan_typesetting_info.items():
  36. print(k, v)
  37. insert_ad_plan_typesetting = table_ad_plan_typesetting.insert()
  38. insert_ad_plan_typesetting = insert_ad_plan_typesetting.values \
  39. (typesetting=ad_plan_typesetting_info['typesetting'],
  40. user_id=ad_plan_typesetting_info['user_id'],
  41. name=ad_plan_typesetting_info['name']
  42. )
  43. return insert_ad_plan_typesetting
  44. def save_wechat_info(wechat_info, table_wechat):
  45. insert_wechat = table_wechat.insert()
  46. insert_wechat = insert_wechat.values \
  47. (service_name=wechat_info['service_name'],
  48. wechat_name=wechat_info['wechat_name'],
  49. appid=wechat_info['appid'],
  50. wxname=wechat_info['wxname'],
  51. user_id=wechat_info['user_id']
  52. )
  53. return insert_wechat
  54. def save_action_record(action_record_info, table_action_record):
  55. # TODO:service_name ,wechat_name,action_type 设置全局唯一
  56. insert_action_record = table_action_record.insert()
  57. insert_action_record = insert_action_record.values \
  58. (service_name=action_record_info['service_name'],
  59. wechat_name=action_record_info['wechat_name'],
  60. user_id=action_record_info['user_id'],
  61. object_name=action_record_info['object_name'],
  62. action_type=action_record_info['action_type'],
  63. status=action_record_info['status'],
  64. task_name=action_record_info['task_name']
  65. )
  66. return insert_action_record
  67. def delete_wechat_info(sql_session, user_id):
  68. sql = '''
  69. delete from wechat_info
  70. where user_id = '{}'
  71. '''.format(user_id)
  72. sql_session.execute(sql)
  73. sql_session.commit()
  74. def get_human_info(sql_session, service_name, wechat_name):
  75. sql = '''
  76. select human_info from human_info hi
  77. where service_name='{service_name}' and wechat_name='{wechat_name}' ;
  78. '''.format(service_name=service_name, wechat_name=wechat_name)
  79. cursor = sql_session.execute(sql)
  80. lines = cursor.fetchall()
  81. result_list = lines[0][0]
  82. return result_list
  83. def get_layout_typesetting_rough(sql_session, user_id, typesetting_name):
  84. sql = '''
  85. select typesetting,name,create_time,update_time from layout_typesetting lt
  86. where user_id ='{}' and is_delete=0 and name like '%{}%';
  87. '''.format(user_id, typesetting_name)
  88. print(sql)
  89. cursor = sql_session.execute(sql)
  90. lines = cursor.fetchall()
  91. result_list = [line for line in lines]
  92. return result_list
  93. def get_layout_typesetting(sql_session, user_id, typesetting_name):
  94. sql = '''
  95. select typesetting from layout_typesetting lt
  96. where user_id ='{}' and is_delete=0 and name='{}' ;
  97. '''.format(user_id, typesetting_name)
  98. print(sql)
  99. cursor = sql_session.execute(sql)
  100. lines = cursor.fetchall()
  101. if lines:
  102. result_list = lines[0][0]
  103. return result_list
  104. def delete_layout_typesetting_vir(sql_session, user_id, typesetting_name):
  105. sql = '''
  106. update layout_typesetting lt
  107. set is_delete=1
  108. where user_id ='{}' and name = '{}';
  109. '''.format(user_id, typesetting_name)
  110. print(sql)
  111. sql_session.execute(sql)
  112. sql_session.commit()
  113. def get_plan_typesetting_rough(sql_session, user_id, typesetting_name):
  114. sql = '''
  115. select typesetting,name,create_time,update_time from ad_plan_typesetting lt
  116. where user_id ='{}' and name like '%{}%';
  117. '''.format(user_id, typesetting_name)
  118. print(sql)
  119. cursor = sql_session.execute(sql)
  120. lines = cursor.fetchall()
  121. result_list = [line for line in lines]
  122. return result_list
  123. def get_ad_plan_typesetting(sql_session, user_id, typesetting_name):
  124. sql = '''
  125. select typesetting from ad_plan_typesetting lt
  126. where user_id ='{}' and name='{}' ;
  127. '''.format(user_id, typesetting_name)
  128. cursor = sql_session.execute(sql)
  129. lines = cursor.fetchall()
  130. if lines:
  131. result_list = lines[0][0]
  132. return result_list
  133. def delete_ad_plan_typesetting_vir(sql_session, user_id, typesetting_name, wechat_name, service_name):
  134. sql = '''
  135. update action_record lt
  136. set is_delete=1
  137. where action_type='create_ad_plan' and
  138. user_id ='{user_id}' and object_name = '{name}' and
  139. wechat_name='{wechat_name}' and service_name='{service_name}';
  140. '''.format(user_id=user_id, name=typesetting_name,
  141. service_name=service_name, wechat_name=wechat_name)
  142. print(sql)
  143. sql_session.execute(sql)
  144. sql_session.commit()
  145. def get_plan_record(sql_session, user_id, service_name, wechat_name,
  146. status, plan_name):
  147. other_info = ''
  148. if service_name:
  149. other_info = other_info + '\n and service_name="{}" \n'.format(service_name)
  150. if wechat_name:
  151. other_info = other_info + '\n and wechat_name="{}" \n'.format(wechat_name)
  152. if status:
  153. other_info = other_info + '\n and status="{}" \n'.format(status)
  154. if plan_name:
  155. other_info = other_info + '\n and typesetting_list.name="{}" \n'.format(plan_name)
  156. sql = '''
  157. select foo.*,wechat_info.wechat_id_info from (select typesetting_list.user_id , typesetting_list.name,
  158. record_list.service_name ,record_list.wechat_name,
  159. typesetting_list.create_time,record_list.status,
  160. typesetting_list.typesetting
  161. from
  162. ad_plan_typesetting as typesetting_list
  163. left join
  164. action_record as record_list
  165. on object_name=name
  166. where typesetting_list.user_id ='{user_id}'
  167. and record_list.is_delete = 0
  168. and record_list.status in ('done','error')
  169. {other_info}
  170. ) as foo left join wechat_info
  171. on foo.service_name =wechat_info.service_name and foo.wechat_name = wechat_info.wechat_name ;
  172. '''.format(user_id=user_id, other_info=other_info)
  173. print(sql)
  174. cursor = sql_session.execute(sql)
  175. lines = cursor.fetchall()
  176. result = [line for line in lines]
  177. return result
  178. def get_undo_action(sql_session, user_id):
  179. # TODO:sql 里面添加doing,error状态的挑选
  180. sql = '''
  181. select action_type ,wechat_name ,service_name
  182. from action_record
  183. where user_id='{}' and status ='todo' ;
  184. '''.format(user_id)
  185. cursor = sql_session.execute(sql)
  186. lines = cursor.fetchall()
  187. result_list = [line for line in lines]
  188. return result_list
  189. def get_ad_task(sql_session, user_id):
  190. sql = '''
  191. select task_name,status,count(*),min(ar.create_time),apt.typesetting from action_record ar
  192. join ad_plan_typesetting apt on ar.object_name =apt.name
  193. where ar.user_id='{}'
  194. and action_type ='create_ad_plan'
  195. group by task_name ,status ;
  196. '''.format(user_id)
  197. cursor = sql_session.execute(sql)
  198. lines = cursor.fetchall()
  199. result_list = [line for line in lines]
  200. return result_list
  201. def get_ad_status(sql_session, user_id):
  202. sql = '''
  203. select action_type ,wechat_name ,service_name,max(update_time ),max(create_time),status from action_record
  204. where user_id='{}'
  205. group by action_type ,wechat_name ,service_name,status ;
  206. '''.format(user_id)
  207. cursor = sql_session.execute(sql)
  208. lines = cursor.fetchall()
  209. result = [line for line in lines]
  210. return result
  211. def get_action_status(sql_session, user_id):
  212. # TODO:sql 里面添加doing,error状态的挑选
  213. sql = '''
  214. select count(*)
  215. from action_record
  216. where user_id='{}' and status ='doing' ;
  217. '''.format(user_id)
  218. cursor = sql_session.execute(sql)
  219. lines = cursor.fetchall()
  220. result = lines[0][0]
  221. return result
  222. def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name,
  223. wechat_name,task_name):
  224. print('get in action record ', service_name, wechat_name, res)
  225. status = 'done' if res['sucess'] else 'error'
  226. print(action_type)
  227. update_res = action_table.update() \
  228. .where(action_table.c.service_name == service_name) \
  229. .where(action_table.c.wechat_name == wechat_name) \
  230. .where(action_table.c.user_id == user_id) \
  231. .where(action_table.c.object_name == object_name) \
  232. .where(action_table.c.action_type == action_type) \
  233. .where(action_table.c.task_name == task_name) \
  234. .where(action_table.c.status == 'todo') \
  235. .values({
  236. action_table.c.status: status,
  237. action_table.c.result: res['result_info']
  238. })
  239. # values 添加两个列
  240. sql_session.execute(update_res)
  241. sql_session.commit()
  242. def check_layout_alive(sql_session, service_name, wechat_name, layout_name):
  243. sql = '''
  244. select count(*) from action_record ar
  245. where service_name='{service_name}'
  246. and wechat_name='{wechat_name}'
  247. and action_type like '%layout_create%'
  248. and action_type like '%{layout_name}%'
  249. and status='done'
  250. '''.format(service_name=service_name, wechat_name=wechat_name, layout_name=layout_name)
  251. print(sql)
  252. cursor = sql_session.execute(sql)
  253. num = cursor.fetchall()[0][0]
  254. return num
  255. def check_plan_alive(sql_session, service_name, wechat_name, plan_name):
  256. sql = '''
  257. select count(*) from action_record ar
  258. where service_name='{service_name}'
  259. and wechat_name='{wechat_name}'
  260. and action_type like '%ad_plan_create%'
  261. and action_type like '%{plan_name}%'
  262. and status='done'
  263. '''.format(service_name=service_name, wechat_name=wechat_name, plan_name=plan_name)
  264. print('plan ', sql)
  265. cursor = sql_session.execute(sql)
  266. num = cursor.fetchall()[0][0]
  267. return num
  268. def get_wechat_info(sql_session, user_id):
  269. sql = '''
  270. select service_name ,wechat_name from wechat_info
  271. where user_id ='{}' ;
  272. '''.format(user_id)
  273. cursor = sql_session.execute(sql)
  274. lines = cursor.fetchall()
  275. result_list = [line for line in lines]
  276. return result_list
  277. def get_wechat_info_service_name(sql_session, user_id):
  278. sql = '''
  279. select distinct(service_name) from wechat_info
  280. where user_id ='{}' ;
  281. '''.format(user_id)
  282. cursor = sql_session.execute(sql)
  283. lines = cursor.fetchall()
  284. result_list = [line[0] for line in lines]
  285. return result_list
  286. def get_wechat_info_wechat_name(sql_session, user_id, service_name):
  287. sql = '''
  288. select service_name ,wechat_name from wechat_info
  289. where user_id ='{}' and service_name='{}';
  290. '''.format(user_id, service_name)
  291. cursor = sql_session.execute(sql)
  292. lines = cursor.fetchall()
  293. result_list = [line for line in lines]
  294. return result_list
  295. def get_wechat_cookies(sql_session, user_id):
  296. sql = '''
  297. select cookies from wechat_cookies where user_id='{}'
  298. '''.format(user_id)
  299. cursor = sql_session.execute(sql)
  300. lines = cursor.fetchall()
  301. print(type(lines), lines)
  302. if lines:
  303. return lines[0][0]