sql_tools.py 14 KB

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