check_order_new.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ = '每日凌晨空闲时检查本地数据库中的订单数据是否和平台昨天总订单一致'
  5. @Time : 2020/9/26 19:44
  6. @Author : Kenny-PC
  7. @Software: PyCharm
  8. # code is far away from bugs with the god animal protecting
  9. I love animals. They taste delicious.
  10. ┏┓ ┏┓
  11. ┏┛┻━━━┛┻┓
  12. ┃ ☃ ┃
  13. ┃ ┳┛ ┗┳ ┃
  14. ┃ ┻ ┃
  15. ┗━┓ ┏━┛
  16. ┃ ┗━━━┓
  17. ┃ 神兽保佑 ┣┓
  18. ┃ 永无BUG! ┏┛
  19. ┗┓┓┏━┳┓┏┛
  20. ┃┫┫ ┃┫┫
  21. ┗┻┛ ┗┻┛
  22. """
  23. import datetime
  24. import hashlib
  25. import math
  26. import random
  27. import time
  28. import traceback
  29. from concurrent.futures import ProcessPoolExecutor
  30. from urllib import parse
  31. import requests
  32. import account_list_zwg as al
  33. from MySQLConnection import MySQLConnection
  34. from util import date_util
  35. from util import platform_util
  36. def md5value(s):
  37. md5 = hashlib.md5()
  38. md5.update(s.encode("utf-8"))
  39. return md5.hexdigest()
  40. ##《1》阅文
  41. def get_yuewen_order(st, et):
  42. start_exec_seconds = date_util.getCurrentSecondTime()
  43. total_order_list = ()
  44. account_list = platform_util.get_yuewen_account_list()
  45. executor = ProcessPoolExecutor(max_workers=5)
  46. futures = []
  47. for account in account_list:
  48. future = executor.submit(get_yuewen_order_task, st, et, account)
  49. futures.append(future)
  50. executor.shutdown(True)
  51. for future in futures:
  52. order_list = future.result()
  53. if len(order_list) > 0:
  54. total_order_list = order_list + total_order_list
  55. print('阅文订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  56. return total_order_list
  57. def get_yuewen_order_task(st, et, account):
  58. order_list = ()
  59. email = account[0]
  60. appsecert = account[1]
  61. url = 'https://open.yuewen.com/cpapi/wxRecharge/querychargelog'
  62. version = 1
  63. order_status = 2 #已支付
  64. page_count = 100 #每页100条数据
  65. start_time = st
  66. for i in range((et - st) // 86400 + 1):
  67. page = 1
  68. last_min_id = ''
  69. last_max_id = ''
  70. total_count = ''
  71. last_page = ''
  72. while True:
  73. if start_time == et:
  74. break
  75. end_time = min(start_time + 86400, et)
  76. timestamp = int(time.time())
  77. params = {
  78. 'email': email,
  79. 'version': version,
  80. 'timestamp': timestamp,
  81. 'start_time': start_time,
  82. 'end_time': end_time,
  83. 'page': page,
  84. 'order_status': order_status
  85. }
  86. if page > 1:
  87. params['last_min_id'] = last_min_id
  88. params['last_max_id'] = last_max_id
  89. params['total_count'] = total_count
  90. params['last_page'] = last_page
  91. sorted_data = sorted(params.items())
  92. str_params = ''
  93. for k, v in sorted_data:
  94. str_params = str_params + str(k) + str(v)
  95. sign = md5value(appsecert + str_params).upper()
  96. #放入签名
  97. params['sign'] = sign
  98. response_result_json = requests.get(url=url, params=params).json()
  99. code = response_result_json['code']
  100. ## 此接口有调用频率限制,相同查询条件每分钟仅能请求一次
  101. if code != 0:
  102. print('阅文查询充值接口异常:', response_result_json, '参数', params)
  103. break
  104. # if code == 10408:
  105. # if fail_count > 0:
  106. # break
  107. #
  108. # sleep_seconds = random.randint(60, 70)
  109. # print('阅文获取订单数据线程休眠【{sleep_seconds}】秒,因为该接口有一分钟的限制'.format(sleep_seconds=sleep_seconds))
  110. # time.sleep(sleep_seconds)
  111. #
  112. # print('重试一次')
  113. # fail_count = fail_count + 1
  114. # get_yuewen_order_task(st, et, account, fail_count)
  115. response_data = response_result_json['data']
  116. total_count = response_data['total_count']
  117. if total_count == 0:
  118. continue
  119. last_min_id = response_data['min_id']
  120. last_max_id = response_data['max_id']
  121. last_page = response_data['page']
  122. order_item_list = response_data['list']
  123. for order_item in order_item_list:
  124. order = {}
  125. dtime = datetime.datetime.strptime(order_item['order_time'], "%Y-%m-%d %H:%M:%S")
  126. order['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  127. order['platform'] = '阅文'
  128. order['channel'] = order_item['app_name']
  129. order['from_novel'] = order_item['book_name']
  130. order['user_id'] = order_item['openid']
  131. order['stage'] = ''
  132. order['channel_id'] = 0
  133. order['order_time'] = order_item['order_time']
  134. order['amount'] = order_item['amount']
  135. order['reg_time'] = order_item['reg_time']
  136. order['order_id'] = order_item['order_id']
  137. order = sorted(order.items(), key=lambda item: item[0])
  138. order = dict(order)
  139. order = tuple(order.values())
  140. order_list = order_list + ((order),)
  141. # print('阅文账号【{key}】, 查询时间【{start_time} - {end_time}】,当前页【{page}】,本次查询订单数量【{total_count}】'
  142. # .format(key=email, start_time=date_util.getSecondsToDatetime(start_time),
  143. # end_time=date_util.getSecondsToDatetime(end_time),page=page, total_count=total_count))
  144. if int(page) >= math.ceil(total_count / int(page_count)):
  145. break
  146. page = page + 1
  147. start_time = start_time + 86400 #天数加1
  148. # sleep_seconds = random.randint(60, 70)
  149. # print('阅文获取订单数据线程休眠【{sleep_seconds}】秒,因为该接口有一分钟的限制'.format(sleep_seconds=sleep_seconds))
  150. # time.sleep(sleep_seconds)
  151. return order_list
  152. ##《2》掌读
  153. def get_zhangdu_order(st, et):
  154. start_exec_seconds = date_util.getCurrentSecondTime()
  155. total_order_list = ()
  156. account_list = platform_util.get_zhangdu_account_list()
  157. executor = ProcessPoolExecutor(max_workers=5)
  158. futures = []
  159. for account in account_list:
  160. future = executor.submit(get_zhangdu_order_task, st, et, account)
  161. futures.append(future)
  162. executor.shutdown(True)
  163. for future in futures:
  164. order_list = future.result()
  165. if len(order_list) > 0:
  166. total_order_list = order_list + total_order_list
  167. print('掌读订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  168. return total_order_list
  169. def get_zhangdu_order_task(st, et, account):
  170. order_list = ()
  171. url = 'https://api.zhangdu520.com/channel/getorder'
  172. uid = account[0]
  173. appsecert = account[1]
  174. channel = account[2]
  175. timestamp = int(time.time())
  176. sign = md5value(str(uid) + '&' + appsecert + '&' + str(timestamp))
  177. starttime = st
  178. timespace = 90 * 3600 * 24
  179. endtime = min(et, st + timespace)
  180. for x in range((et - st) // timespace + 1): # 分时段
  181. if x > 0:
  182. print('掌读跨天数查询:', x)
  183. params = {
  184. 'uid': uid,
  185. 'timestamp': timestamp,
  186. 'sign': sign,
  187. 'starttime': starttime,
  188. 'endtime': endtime
  189. }
  190. response_result_json = requests.get(url=url, params=params).json()
  191. pageCount = response_result_json['data']['pageCount']
  192. if pageCount == 0:
  193. continue
  194. for page in range(1, pageCount + 1): # 分页
  195. params = {
  196. 'uid': uid,
  197. 'timestamp': timestamp,
  198. 'sign': sign,
  199. 'starttime': starttime,
  200. 'endtime': endtime,
  201. 'page': page
  202. }
  203. list2 = requests.get(url=url, params=params).json()
  204. if 'data' in list2.keys():
  205. for b in list2['data']['list']:
  206. if b['status'] == '1':
  207. c = {}
  208. c['amount'] = b['amount']
  209. c['channel_id'] = uid
  210. c['order_id'] = str(b['orderno'])
  211. c['order_time'] = b['ctime']
  212. c['user_id'] = b['openid']
  213. c['platform'] = '掌读'
  214. c['channel'] = channel
  215. c['reg_time'] = b['regtime']
  216. c['from_novel'] = ''
  217. c['stage'] = ''
  218. c['date'] = ((int(b['ctime']) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  219. x = sorted(c.items(), key=lambda item: item[0])
  220. x = dict(x)
  221. x = tuple(x.values())
  222. order_list = order_list + ((x),)
  223. starttime = starttime + timespace
  224. endtime = min(et, starttime + timespace)
  225. return order_list
  226. ##《3》花生
  227. def get_huasheng_order(st, et):
  228. start_exec_seconds = date_util.getCurrentSecondTime()
  229. total_order_list = ()
  230. account_list = platform_util.get_huasheng_account_list()
  231. executor = ProcessPoolExecutor(max_workers=5)
  232. futures = []
  233. for account in account_list:
  234. url = 'https://vip.rlcps.cn/api/getMerchants'
  235. apiKey = str(account[0])
  236. apiSecurity = account[1]
  237. timestamp = str(int(time.time()))
  238. sign = md5value(apiKey + timestamp + apiSecurity).upper()
  239. params = {
  240. 'apiKey': apiKey,
  241. 'apiSecurity': apiSecurity,
  242. 'timestamp': timestamp,
  243. 'sign': sign
  244. }
  245. response_result_json = requests.post(url, params).json()
  246. if 'data' not in response_result_json.keys():
  247. # print('花生账号【{apiKey}】本次请求数据为空,响应报文【{result}】'.format(apiKey=apiKey, result=response_result_json))
  248. continue
  249. for merchant in response_result_json['data']:
  250. future = executor.submit(get_huasheng_order_task, st, et, account, merchant)
  251. futures.append(future)
  252. executor.shutdown(True)
  253. for future in futures:
  254. order_list = future.result()
  255. if len(order_list) > 0:
  256. total_order_list = order_list + total_order_list
  257. print('花生订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  258. return total_order_list
  259. def get_huasheng_order_task(st, et, account, merchant):
  260. order_list = ()
  261. apiKey = str(account[0])
  262. apiSecurity = account[1]
  263. stage = account[2]
  264. timestamp = str(int(time.time()))
  265. order_url = 'https://vip.rlcps.cn/api/orderList'
  266. merchant_id = merchant['merchant_id']
  267. merchant_name = merchant['merchant_name']
  268. start_time = st
  269. limit = 500
  270. for i in range((et - st) // 86400 + 1):
  271. page = 1
  272. while True:
  273. date = time.strftime("%Y-%m-%d", time.localtime(start_time))
  274. sign = md5value(apiKey + date + str(merchant_id) + timestamp + apiSecurity).upper()
  275. order_params = {
  276. 'apiKey': apiKey,
  277. 'apiSecurity': apiSecurity,
  278. 'timestamp': timestamp,
  279. 'date': date,
  280. 'merchant_id': merchant_id,
  281. 'sign': sign,
  282. 'page': page,
  283. 'limit': limit
  284. }
  285. response_result_json = requests.post(order_url, order_params).json()
  286. if 'data' not in response_result_json.keys() or len(response_result_json['data']) == 0:
  287. # print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】本次请求数据为空,响应报文【{result}】'
  288. # .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name,
  289. # result=response_result_json))
  290. break
  291. total_count = response_result_json['count']
  292. order_item_list = response_result_json['data']
  293. for order_item in order_item_list:
  294. if order_item['order_status'] == 1: # 1为已支付
  295. order = {}
  296. ##dtime = datetime.datetime.strptime(order_item['pay_at'],"%Y-%m-%d")
  297. ##order['date']= ((int(time.mktime(dtime.timetuple()))+8*3600)//86400)*86400-8*3600
  298. order['user_id'] = order_item['openid']
  299. order['order_id'] = order_item['trans_id']
  300. order['order_time'] = order_item['pay_at']
  301. order['reg_time'] = order_item['join_at']
  302. # TODO 花生的时间需要统一
  303. order['date'] = (start_time + 8 * 3600) // 86400 * 86400 - 8 * 3600
  304. order['channel'] = merchant_name
  305. order['channel_id'] = merchant_id
  306. order['platform'] = '花生'
  307. order['stage'] = stage
  308. order['from_novel'] = order_item['book_name']
  309. order['amount'] = order_item['amount']
  310. order = sorted(order.items(), key=lambda item: item[0])
  311. order = dict(order)
  312. order = tuple(order.values())
  313. order_list = order_list + ((order),)
  314. if int(page) >= math.ceil(total_count / int(limit)):
  315. break
  316. # print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  317. # .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name, page=page, total_count=total_count))
  318. page = page + 1
  319. start_time = start_time + 86400 # 天数加1
  320. return order_list
  321. ##《4》掌中云
  322. def get_zzy_order(st, et):
  323. start_exec_seconds = date_util.getCurrentSecondTime()
  324. total_order_list = ()
  325. account_list = al.zzy_account_list
  326. # account_list = platform_util.get_zhangzhongyun_account_list()
  327. # account_list = [['1108701f1d6','0f9c0f8429d1a16a8a78c2306e7a4db3','清勇7月']]
  328. # account_list = [['1109295d56c','9bb955186597882ac473e86ba4576158','趣程20期']]
  329. executor = ProcessPoolExecutor(max_workers=5)
  330. futures = []
  331. for account in account_list:
  332. url = 'https://openapi.818tu.com/partners/channel/channels/list?'
  333. key = account[0]
  334. secert = account[1]
  335. sign = md5value(secert + 'key=' + key)
  336. params = 'key=' + key + '&sign=' + sign
  337. response_result_json = requests.get(url + params).json() # 获取子渠道列表
  338. if 'data' not in response_result_json.keys():
  339. # print('掌中云账号【{key}】本次请求数据为空,响应报文【{result}】'.format(key=key, result=response_result_json))
  340. continue
  341. items = response_result_json['data']['items']
  342. for channel in items:
  343. # 获取channel_id 后逐个拉取历史orders
  344. future = executor.submit(get_zzy_order_task, st, et, account, channel)
  345. futures.append(future)
  346. executor.shutdown(True)
  347. for future in futures:
  348. order_list = future.result()
  349. if len(order_list) > 0:
  350. total_order_list = order_list + total_order_list
  351. print('掌中云订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  352. return total_order_list
  353. def get_zzy_order_task(st, et, account, channel):
  354. # 掌中云的时间格式比较特殊,转换下
  355. st = platform_util.get_zhangzhongyun_format_time(st)
  356. et = platform_util.get_zhangzhongyun_format_time(et)
  357. order_list = ()
  358. key = account[0]
  359. secert = account[1]
  360. stage = account[2]
  361. order_url = 'https://openapi.818tu.com/partners/channel/orders/list?'
  362. channel_id = channel['id']
  363. channel_name = channel['nickname']
  364. status = str(1)
  365. page = str(1)
  366. per_page = str(1000)
  367. get_time = st
  368. limit_time = et
  369. gte = parse.urlencode({'created_at[gte]': get_time}) # gte就是ge 大于等于开始时间
  370. lt = parse.urlencode({'created_at[lt]': limit_time}) # 小于 结束时间
  371. while True:
  372. sign = md5value(secert + 'channel_id=' + str(
  373. channel_id) + '&created_at[gte]=' + get_time + '&created_at[lt]=' + limit_time + '&key=' + key + '&page=' + str(
  374. page) + '&per_page=' + per_page + '&status=' + status)
  375. params = 'channel_id=' + str(channel_id) + '&' + gte + '&' + lt + '&page=' + str(
  376. page) + '&per_page=' + per_page + '&status=' + status + '&key=' + key + '&sign=' + sign
  377. response_result_json = requests.get(order_url + params).json()
  378. if 'data' not in response_result_json.keys():
  379. # print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】本次请求数据为空,响应报文【{result}】'
  380. # .format(key=key, channel_id=channel_id, channel_name=channel_name, result=response_result_json))
  381. break
  382. total_count = response_result_json['data']['count'] # 总数量
  383. order_item_list = response_result_json['data']['items'] # 订单列表
  384. for order_item in order_item_list:
  385. order = {}
  386. order['user_id'] = str(order_item['member']['openid'])
  387. order['channel'] = channel_name
  388. order['reg_time'] = order_item['member']['created_at']
  389. order['channel_id'] = channel_id
  390. order['amount'] = round(order_item['price'] / 100, 2)
  391. order['order_id'] = str(order_item['id'])
  392. order['order_time'] = order_item['created_at']
  393. order['platform'] = '掌中云'
  394. order['stage'] = stage
  395. dtime = datetime.datetime.strptime(order_item['created_at'][0:10], "%Y-%m-%d")
  396. order['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  397. if str(order_item['from_novel_id']) != 'None':
  398. order['from_novel'] = order_item['from_novel']['title']
  399. else:
  400. order['from_novel'] = 'None'
  401. x = sorted(order.items(), key=lambda item: item[0])
  402. x = dict(x)
  403. x = tuple(x.values())
  404. order_list = order_list + ((x),)
  405. if int(page) >= math.ceil(total_count / int(per_page)):
  406. break
  407. # print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  408. # .format(key=key, channel_id=channel_id, channel_name=channel_name, page=page, total_count=total_count))
  409. page = int(page) + 1
  410. return order_list
  411. ##《5》 悠书阁
  412. def get_ysg_order(st, et):
  413. start_exec_seconds = date_util.getCurrentSecondTime()
  414. total_order_list = ()
  415. account_list = platform_util.get_youshuge_account_list()
  416. executor = ProcessPoolExecutor(max_workers=5)
  417. futures = []
  418. for account in account_list:
  419. future = executor.submit(get_ysg_order_task, st, et, account)
  420. futures.append(future)
  421. executor.shutdown(True)
  422. for future in futures:
  423. order_list = future.result()
  424. if len(order_list) > 0:
  425. total_order_list = order_list + total_order_list
  426. print('悠书阁订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  427. return total_order_list
  428. def get_ysg_order_task(st, et, account):
  429. order_list = ()
  430. url = 'https://novel.youshuge.com/v2/open/orders'
  431. # 超过100条就需要分页,别问我为什么知道,看代码看出来的
  432. max_page_size = 100
  433. host_name = account[0]
  434. channel_id = int(account[1])
  435. secert_key = account[2]
  436. channel = account[3]
  437. stage = account[4]
  438. timestamp = int(time.time())
  439. start_date = time.strftime("%Y-%m-%d", time.localtime(st))
  440. end_date = time.strftime("%Y-%m-%d", time.localtime(et))
  441. page = 1
  442. str1 = 'channel_id=' + str(channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  443. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  444. sign = md5value(str1).upper()
  445. data = {
  446. 'sign': sign,
  447. 'host_name': host_name,
  448. 'time': timestamp,
  449. 'channel_id': channel_id,
  450. 'page': page,
  451. 'pay_status': 1,
  452. 'start_date': start_date,
  453. 'end_date': end_date
  454. }
  455. respone = requests.post(url, data)
  456. if respone.status_code == 400:
  457. print('respone', respone)
  458. result_json = respone.json()
  459. first_page_order = build_ysg_order_data(channel, channel_id, result_json, stage)
  460. order_list = order_list + first_page_order
  461. if len(first_page_order) == 0:
  462. return order_list
  463. total_count = result_json['data'][0]['count']
  464. if total_count > max_page_size:
  465. for i in range((total_count - 1) // max_page_size + 1):
  466. timestamp = int(time.time())
  467. str1 = 'channel_id=' + str(
  468. channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  469. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  470. sign = md5value(str1).upper()
  471. data2 = {
  472. 'sign': sign,
  473. 'host_name': host_name,
  474. 'time': timestamp,
  475. 'channel_id': channel_id,
  476. 'page': page,
  477. 'pay_status': 1,
  478. 'start_date': start_date,
  479. 'end_date': end_date
  480. }
  481. r2 = requests.post(url, data2).json()
  482. order_list = order_list + build_ysg_order_data(channel, channel_id, r2, stage)
  483. page = page + 1
  484. return order_list
  485. def build_ysg_order_data(channel, channel_id, result_json, stage):
  486. order_list = ()
  487. if 'data' in result_json.keys():
  488. data = result_json['data']
  489. if len(data) > 0:
  490. for x in data:
  491. y = {}
  492. dtime = datetime.datetime.strptime(x['create_time'][0:10], "%Y-%m-%d")
  493. y['date'] = ((int(
  494. time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  495. y['order_id'] = x['order_num']
  496. y['amount'] = round(int(x['price']) / 100, 2)
  497. y['order_time'] = x['create_time']
  498. y['channel'] = channel
  499. y['from_novel'] = x['book_name']
  500. y['stage'] = stage
  501. y['user_id'] = x['openid']
  502. y['channel_id'] = channel_id
  503. y['platform'] = '悠书阁'
  504. y['reg_time'] = x['reg_time']
  505. y = sorted(y.items(), key=lambda item: item[0])
  506. y = dict(y)
  507. y = tuple(y.values())
  508. order_list = order_list + ((y),)
  509. return order_list
  510. # 数据导入表采用replace替换主键orderid的方法
  511. def mysql_insert_order(data):
  512. if data is None or len(data) == 0:
  513. print('数据为空,不执行数据库操作!')
  514. else:
  515. sql = 'replace into quchen_text.`order_zwg` (amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  516. connect = MySQLConnection()
  517. try:
  518. num = connect.batch(sql, data)
  519. # 提交
  520. connect.commit()
  521. print(num, '条订单数据入库成功')
  522. except Exception as e:
  523. traceback.print_exc()
  524. print('订单数据入库失败:', e)
  525. finally:
  526. connect.close()
  527. # 获取各平台的订单数量
  528. def mysql_select_platform_order_count(date):
  529. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order_zwg` WHERE date = %s GROUP BY platform'
  530. connect = MySQLConnection()
  531. platform_order_count = []
  532. try:
  533. platform_order_count = connect.query(sql, date)
  534. return platform_order_count
  535. except Exception as e:
  536. traceback.print_exc()
  537. print('各平台的订单数据查询失败:', e)
  538. finally:
  539. connect.close()
  540. return platform_order_count
  541. def start_all_job():
  542. start_exec_seconds = date_util.getCurrentSecondTime()
  543. st_unix = date_util.getYesterdayStartTime()
  544. et_unix = date_util.getTodayStartTime()
  545. # st_unix = 1601136000 # 2020/9/27 0:0:0
  546. # et_unix = 1601308800 # 2020/9/29 0:0:0
  547. # et_unix = st_unix + 10 # 2020/9/29 0:0:0
  548. print('查询开始时间:', st_unix, date_util.getSecondsToDatetime(st_unix))
  549. print('查询结束时间:', et_unix, date_util.getSecondsToDatetime(et_unix))
  550. # order_list = get_yuewen_order(st_unix, et_unix)
  551. # mysql_insert_order(order_list)
  552. platform_order_num_list = mysql_select_platform_order_count(date_util.getYesterdayStartTime())
  553. if len(platform_order_num_list) == 0:
  554. print('本地库中没有任何数据,现在全平台补全')
  555. mysql_insert_order(get_zzy_order(st_unix, et_unix))
  556. mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  557. mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  558. mysql_insert_order(get_ysg_order(st_unix, et_unix))
  559. mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  560. else:
  561. platform_list = ['阅文','悠书阁','掌读','掌中云','花生']
  562. for platform_order_num in platform_order_num_list:
  563. platform = str(platform_order_num['platform'])
  564. num = int(platform_order_num['num'])
  565. platform_list.remove(platform)
  566. if platform == '阅文':
  567. order_list = get_yuewen_order(st_unix, et_unix)
  568. if len(order_list) != num:
  569. print('阅文数据实际订单和已经入库数据差异:', len(order_list) - num)
  570. mysql_insert_order(order_list)
  571. elif platform == '悠书阁':
  572. order_list = get_ysg_order(st_unix, et_unix)
  573. if len(order_list) != num:
  574. print('悠书阁数据实际订单和已经入库数据差异:', len(order_list) - num)
  575. mysql_insert_order(order_list)
  576. elif platform == '掌读':
  577. order_list = get_zhangdu_order(st_unix, et_unix)
  578. if len(order_list) != num:
  579. print('掌读数据实际订单和已经入库数据差异:', len(order_list) - num)
  580. mysql_insert_order(order_list)
  581. elif platform == '掌中云':
  582. order_list = get_zzy_order(st_unix, et_unix)
  583. if len(order_list) != num:
  584. print('掌中云数据实际订单和已经入库数据差异:', len(order_list) - num)
  585. mysql_insert_order(order_list)
  586. elif platform == '花生':
  587. order_list = get_huasheng_order(st_unix, et_unix)
  588. if len(order_list) != num:
  589. print('花生数据实际订单和已经入库数据差异:', len(order_list) - num)
  590. mysql_insert_order(order_list)
  591. else:
  592. print('发现未知平台数据!', platform_order_num)
  593. for platform in platform_list:
  594. if platform == '阅文':
  595. print('阅文没有数据')
  596. mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  597. elif platform == '悠书阁':
  598. print('悠书阁没有数据')
  599. mysql_insert_order(get_ysg_order(st_unix, et_unix))
  600. elif platform == '掌读':
  601. print('掌读没有数据')
  602. mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  603. elif platform == '掌中云':
  604. print('掌中云没有数据')
  605. mysql_insert_order(get_zzy_order(st_unix, et_unix))
  606. elif platform == '花生':
  607. print('花生没有数据')
  608. mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  609. else:
  610. print('什么鬼平台:', platform)
  611. print('订单检查执行时间(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  612. if __name__ == '__main__':
  613. start_all_job()
  614. # scheduler = BlockingScheduler()
  615. # #每天凌晨3点到4点的30分钟都执行一次
  616. # scheduler.add_job(start_all_job, 'cron', hour='3-4', minute='35')
  617. # scheduler.start()