sql_tools.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. scan_action=wechat_cookies_info['scan_action']
  16. )
  17. return insert_layout
  18. def save_human_info(human_info, table_human):
  19. insert_human = table_human.insert()
  20. insert_human = insert_human.values \
  21. (service_name=human_info['service_name'],
  22. wechat_name=human_info['wechat_name'],
  23. human_info=human_info['human_info']
  24. )
  25. return insert_human
  26. def save_layout_typesetting_info(layout_typesetting_info, table_layout_typesetting):
  27. insert_layout_typesetting = table_layout_typesetting.insert()
  28. insert_layout_typesetting = insert_layout_typesetting.values \
  29. (typesetting=layout_typesetting_info['typesetting'],
  30. user_id=layout_typesetting_info['user_id'],
  31. name=layout_typesetting_info['name']
  32. )
  33. return insert_layout_typesetting
  34. def save_ad_plan_typesetting_info(ad_plan_typesetting_info, table_ad_plan_typesetting):
  35. # TODO:正常action_record 返回plan_name layout_name有问题
  36. insert_ad_plan_typesetting = table_ad_plan_typesetting.insert()
  37. insert_ad_plan_typesetting = insert_ad_plan_typesetting.values \
  38. (typesetting=ad_plan_typesetting_info['typesetting'],
  39. user_id=ad_plan_typesetting_info['user_id'],
  40. name=ad_plan_typesetting_info['name']
  41. )
  42. return insert_ad_plan_typesetting
  43. def save_wechat_info(wechat_info, table_wechat):
  44. insert_wechat = table_wechat.insert()
  45. insert_wechat = insert_wechat.values \
  46. (service_name=wechat_info['service_name'],
  47. wechat_name=wechat_info['wechat_name'],
  48. appid=wechat_info['appid'],
  49. wxname=wechat_info['wxname'],
  50. user_id=wechat_info['user_id']
  51. )
  52. return insert_wechat
  53. def save_action_record(action_record_info, table_action_record):
  54. # TODO:service_name ,wechat_name,action_type 设置全局唯一
  55. insert_action_record = table_action_record.insert()
  56. insert_action_record = insert_action_record.values \
  57. (service_name=action_record_info['service_name'],
  58. wechat_name=action_record_info['wechat_name'],
  59. user_id=action_record_info['user_id'],
  60. object_name=action_record_info['object_name'],
  61. action_type=action_record_info['action_type'],
  62. status=action_record_info['status'],
  63. task_name=action_record_info['task_name'],
  64. result=action_record_info['result'] if 'result' in action_record_info.keys() else None
  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_scan_action_status(user_id, sql_session):
  84. # 扫码成功 done 扫码失败 error(doing ,超过一分钟) 扫码中 doing
  85. sql = '''select timestampdiff(minute ,update_time ,current_timestamp ) as diff_time,
  86. scan_action from wechat_cookies wc
  87. where user_id ='{}'
  88. '''.format(user_id)
  89. cursor = sql_session.execute(sql)
  90. lines = cursor.fetchall()
  91. if len(lines):
  92. diff_time, scan_action = lines[0]
  93. if scan_action == 'done':
  94. return 'done'
  95. elif scan_action == 'doing':
  96. if diff_time > 1:
  97. return 'error'
  98. else:
  99. return 'doing'
  100. return 'error'
  101. def get_layout_typesetting_rough(sql_session, user_id, typesetting_name):
  102. sql = '''
  103. select typesetting,name,create_time,update_time from layout_typesetting lt
  104. where user_id ='{}' and is_delete=0 and name like '%{}%';
  105. '''.format(user_id, typesetting_name)
  106. cursor = sql_session.execute(sql)
  107. lines = cursor.fetchall()
  108. result_list = [line for line in lines]
  109. return result_list
  110. def get_layout_typesetting(sql_session, user_id, typesetting_name):
  111. sql = '''
  112. select typesetting from layout_typesetting lt
  113. where user_id ='{}' and is_delete=0 and name='{}' ;
  114. '''.format(user_id, typesetting_name)
  115. cursor = sql_session.execute(sql)
  116. lines = cursor.fetchall()
  117. if lines:
  118. result_list = lines[0][0]
  119. return result_list
  120. def delete_layout_typesetting_vir(sql_session, user_id, typesetting_name):
  121. sql = '''
  122. update layout_typesetting lt
  123. set is_delete=1
  124. where user_id ='{}' and name = '{}';
  125. '''.format(user_id, typesetting_name)
  126. sql_session.execute(sql)
  127. sql_session.commit()
  128. def get_plan_typesetting_rough(sql_session, user_id, typesetting_name):
  129. sql = '''
  130. select typesetting,name,create_time,update_time from ad_plan_typesetting lt
  131. where user_id ='{}' and name like '%{}%';
  132. '''.format(user_id, typesetting_name)
  133. cursor = sql_session.execute(sql)
  134. lines = cursor.fetchall()
  135. result_list = [line for line in lines]
  136. return result_list
  137. def get_ad_plan_typesetting(sql_session, user_id, typesetting_name):
  138. sql = '''
  139. select typesetting from ad_plan_typesetting lt
  140. where user_id ='{}' and name='{}' ;
  141. '''.format(user_id, typesetting_name)
  142. cursor = sql_session.execute(sql)
  143. lines = cursor.fetchall()
  144. if lines:
  145. result_list = lines[0][0]
  146. return result_list
  147. def delete_ad_plan_typesetting_vir(sql_session, user_id, typesetting_name, wechat_name, service_name):
  148. sql = '''
  149. update action_record lt
  150. set is_delete=1
  151. where action_type='create_ad_plan' and
  152. user_id ='{user_id}' and object_name = '{name}' and
  153. wechat_name='{wechat_name}' and service_name='{service_name}';
  154. '''.format(user_id=user_id, name=typesetting_name,
  155. service_name=service_name, wechat_name=wechat_name)
  156. sql_session.execute(sql)
  157. sql_session.commit()
  158. def get_plan_record(sql_session, user_id, service_name, wechat_name,
  159. status, plan_name):
  160. other_info = ''
  161. if service_name:
  162. other_info = other_info + '\n and service_name="{}" \n'.format(service_name)
  163. if wechat_name:
  164. other_info = other_info + '\n and wechat_name="{}" \n'.format(wechat_name)
  165. if status:
  166. other_info = other_info + '\n and status="{}" \n'.format(status)
  167. if plan_name:
  168. other_info = other_info + '\n and typesetting_list.name="{}" \n'.format(plan_name)
  169. sql = '''
  170. select foo.*,concat(wechat_info.appid,'\n',wechat_info.wxname ) as wechat_id_info
  171. from (select typesetting_list.user_id , typesetting_list.name,
  172. record_list.service_name ,record_list.wechat_name,
  173. typesetting_list.create_time,record_list.status,
  174. typesetting_list.typesetting
  175. from
  176. ad_plan_typesetting as typesetting_list
  177. left join
  178. action_record as record_list
  179. on object_name=name
  180. where typesetting_list.user_id ='{user_id}'
  181. and record_list.is_delete = 0
  182. and record_list.status in ('done','error')
  183. {other_info}
  184. ) as foo left join wechat_info
  185. on foo.service_name =wechat_info.service_name and foo.wechat_name = wechat_info.wechat_name ;
  186. '''.format(user_id=user_id, other_info=other_info)
  187. cursor = sql_session.execute(sql)
  188. lines = cursor.fetchall()
  189. result = [line for line in lines]
  190. return result
  191. def get_undo_action(sql_session, user_id):
  192. # TODO:sql 里面添加doing,error状态的挑选
  193. sql = '''
  194. select action_type ,wechat_name ,service_name
  195. from action_record
  196. where user_id='{}' and status ='todo' ;
  197. '''.format(user_id)
  198. cursor = sql_session.execute(sql)
  199. lines = cursor.fetchall()
  200. result_list = [line for line in lines]
  201. return result_list
  202. def get_task_in_hand_num(user_id, sql_session):
  203. sql = '''
  204. select count(*) from
  205. action_record ar
  206. where timestampdiff(minute ,update_time ,current_timestamp )<60
  207. and status ='todo' and user_id='{}'
  208. '''.format(user_id)
  209. cursor = sql_session.execute(sql)
  210. lines = cursor.fetchall()
  211. result = lines[0][0]
  212. return result
  213. def get_task_in_hand_limit_one(user_id, sql_session):
  214. sql = '''
  215. select foo.typesetting,foo.wechat_name ,foo.service_name ,foo.action_type ,foo2.task_name from
  216. (select ad.typesetting,ar.wechat_name,ar.action_type ,ar.service_name ,
  217. ar.task_name from action_record ar join ad_plan_typesetting ad
  218. on ar.object_name = ad.name
  219. where action_type in ('create_ad_plan' ,'refresh_wechat_info')
  220. and status ='todo'
  221. and timestampdiff(minute ,ad.update_time ,current_timestamp )<60
  222. and ad.user_id ='{}') as foo
  223. join
  224. (select task_name from action_record ar
  225. where status ='todo' and action_type in ('create_ad_plan' ,'refresh_wechat_info')
  226. order by create_time
  227. limit 1) as foo2 on foo.task_name=foo2.task_name
  228. '''.format(user_id)
  229. cursor = sql_session.execute(sql)
  230. lines = cursor.fetchall()
  231. return lines
  232. def update_task_status_error(sql_session, user_id, task_name):
  233. sql = '''
  234. update action_record
  235. set status ='error'
  236. where status ='todo' and user_id ='{}' and task_name ='{}'
  237. '''.format(user_id, task_name)
  238. sql_session.execute(sql)
  239. sql_session.commit()
  240. def update_user_scan_action(user_id, sql_session):
  241. sql = '''update wechat_cookies
  242. set scan_action ='doing',update_time=current_timestamp
  243. where user_id ='{}' '''.format(user_id)
  244. sql_session.execute(sql)
  245. sql_session.commit()
  246. def get_wechat_id_from_cookies(user_id, sql_session):
  247. sql = '''
  248. select wechat_id from wechat_cookies wc
  249. where user_id ='{}'
  250. '''.format(user_id)
  251. cursor = sql_session.execute(sql)
  252. lines = cursor.fetchall()
  253. wechat_id = None
  254. if lines:
  255. wechat_id = lines[0][0]
  256. return wechat_id
  257. def get_ad_task(sql_session, user_id):
  258. sql = '''
  259. select task_name,status,count(*),min(ar.create_time),apt.typesetting from action_record ar
  260. join ad_plan_typesetting apt on ar.object_name =apt.name
  261. where ar.user_id='{}'
  262. and action_type ='create_ad_plan'
  263. group by task_name ,status ;
  264. '''.format(user_id)
  265. cursor = sql_session.execute(sql)
  266. lines = cursor.fetchall()
  267. result_list = [line for line in lines]
  268. return result_list
  269. def get_ad_status(sql_session, user_id):
  270. sql = '''
  271. select action_type ,wechat_name ,service_name,max(update_time ),max(create_time),status from action_record
  272. where user_id='{}'
  273. group by action_type ,wechat_name ,service_name,status ;
  274. '''.format(user_id)
  275. cursor = sql_session.execute(sql)
  276. lines = cursor.fetchall()
  277. result = [line for line in lines]
  278. return result
  279. def get_action_status(sql_session, user_id):
  280. # TODO:sql 里面添加doing,error状态的挑选
  281. sql = '''
  282. select count(*)
  283. from action_record
  284. where user_id='{}' and status ='doing' ;
  285. '''.format(user_id)
  286. cursor = sql_session.execute(sql)
  287. lines = cursor.fetchall()
  288. result = lines[0][0]
  289. return result
  290. def action_record(res, sql_session, action_type, user_id, object_name, action_table, service_name,
  291. wechat_name, task_name):
  292. status = 'done' if res['sucess'] else 'error'
  293. update_res = action_table.update() \
  294. .where(action_table.c.service_name == service_name) \
  295. .where(action_table.c.wechat_name == wechat_name) \
  296. .where(action_table.c.user_id == user_id) \
  297. .where(action_table.c.object_name == object_name) \
  298. .where(action_table.c.action_type == action_type) \
  299. .where(action_table.c.task_name == task_name) \
  300. .where(action_table.c.status == 'todo') \
  301. .values({
  302. action_table.c.status: status,
  303. action_table.c.result: res['result_info']
  304. })
  305. # values 添加两个列
  306. sql_session.execute(update_res)
  307. sql_session.commit()
  308. def check_layout_alive(sql_session, service_name, wechat_name, layout_name):
  309. sql = '''
  310. select count(*) from action_record ar
  311. where service_name='{service_name}'
  312. and wechat_name='{wechat_name}'
  313. and action_type like '%layout_create%'
  314. and action_type like '%{layout_name}%'
  315. and status='done'
  316. '''.format(service_name=service_name, wechat_name=wechat_name, layout_name=layout_name)
  317. cursor = sql_session.execute(sql)
  318. num = cursor.fetchall()[0][0]
  319. return num
  320. def check_plan_alive(sql_session, service_name, wechat_name, plan_name):
  321. sql = '''
  322. select count(*) from action_record ar
  323. where service_name='{service_name}'
  324. and wechat_name='{wechat_name}'
  325. and action_type like '%ad_plan_create%'
  326. and action_type like '%{plan_name}%'
  327. and status='done'
  328. '''.format(service_name=service_name, wechat_name=wechat_name, plan_name=plan_name)
  329. cursor = sql_session.execute(sql)
  330. num = cursor.fetchall()[0][0]
  331. return num
  332. def get_wechat_info(sql_session, user_id):
  333. sql = '''
  334. select service_name ,wechat_name from wechat_info
  335. where user_id ='{}' ;
  336. '''.format(user_id)
  337. cursor = sql_session.execute(sql)
  338. lines = cursor.fetchall()
  339. result_list = [line for line in lines]
  340. return result_list
  341. def get_wechat_info_service_name(sql_session, user_id):
  342. sql = '''
  343. select distinct(service_name) from wechat_info
  344. where user_id ='{}' ;
  345. '''.format(user_id)
  346. cursor = sql_session.execute(sql)
  347. lines = cursor.fetchall()
  348. result_list = [line[0] for line in lines]
  349. return result_list
  350. def get_wechat_info_wechat_name(sql_session, user_id, service_name):
  351. sql = '''
  352. select service_name ,wechat_name from wechat_info
  353. where user_id ='{}' and service_name='{}';
  354. '''.format(user_id, service_name)
  355. cursor = sql_session.execute(sql)
  356. lines = cursor.fetchall()
  357. result_list = [line for line in lines]
  358. return result_list
  359. def get_wechat_cookies(sql_session, user_id):
  360. sql = '''
  361. select cookies from wechat_cookies where user_id='{}'
  362. '''.format(user_id)
  363. cursor = sql_session.execute(sql)
  364. lines = cursor.fetchall()
  365. if lines:
  366. return lines[0][0]