sql_tools.py 15 KB

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