check_order_new.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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_time = order_item['order_time']
  125. dtime = datetime.datetime.strptime(order_time, "%Y-%m-%d %H:%M:%S")
  126. order_time_unix = int(time.mktime(dtime.timetuple()))
  127. order_id = order_item['order_id']
  128. if date_util.checkInterval(start_time, end_time, order_time_unix) == False:
  129. print('阅文账号【{key}】, 查询时间【{start_time} - {end_time}】,有不符合该时间范围的订单,订单Id【{order_id}】的时间为【{order_time}】'
  130. .format(key=email, start_time=date_util.getSecondsToDatetime(start_time),
  131. end_time=date_util.getSecondsToDatetime(end_time),order_id=order_id, order_time=order_time))
  132. continue
  133. order = {}
  134. order['date'] = ((order_time_unix + 8 * 3600) // 86400) * 86400 - 8 * 3600
  135. order['platform'] = '阅文'
  136. order['channel'] = order_item['app_name']
  137. order['from_novel'] = order_item['book_name']
  138. order['user_id'] = order_item['openid']
  139. order['stage'] = ''
  140. order['channel_id'] = 0
  141. order['order_time'] = order_time
  142. order['amount'] = order_item['amount']
  143. order['reg_time'] = order_item['reg_time']
  144. order['order_id'] = order_id
  145. order = sorted(order.items(), key=lambda item: item[0])
  146. order = dict(order)
  147. order = tuple(order.values())
  148. order_list = order_list + ((order),)
  149. # print('阅文账号【{key}】, 查询时间【{start_time} - {end_time}】,当前页【{page}】,本次查询订单数量【{total_count}】'
  150. # .format(key=email, start_time=date_util.getSecondsToDatetime(start_time),
  151. # end_time=date_util.getSecondsToDatetime(end_time),page=page, total_count=total_count))
  152. if int(page) >= math.ceil(total_count / int(page_count)):
  153. break
  154. page = page + 1
  155. start_time = start_time + 86400 #天数加1
  156. # sleep_seconds = random.randint(60, 70)
  157. # print('阅文获取订单数据线程休眠【{sleep_seconds}】秒,因为该接口有一分钟的限制'.format(sleep_seconds=sleep_seconds))
  158. # time.sleep(sleep_seconds)
  159. return order_list
  160. ##《2》掌读
  161. def get_zhangdu_order(st, et):
  162. start_exec_seconds = date_util.getCurrentSecondTime()
  163. total_order_list = ()
  164. account_list = platform_util.get_zhangdu_account_list()
  165. executor = ProcessPoolExecutor(max_workers=5)
  166. futures = []
  167. for account in account_list:
  168. future = executor.submit(get_zhangdu_order_task, st, et, account)
  169. futures.append(future)
  170. executor.shutdown(True)
  171. for future in futures:
  172. order_list = future.result()
  173. if len(order_list) > 0:
  174. total_order_list = order_list + total_order_list
  175. print('掌读订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  176. return total_order_list
  177. def get_zhangdu_order_task(st, et, account):
  178. order_list = ()
  179. url = 'https://api.zhangdu520.com/channel/getorder'
  180. uid = account[0]
  181. appsecert = account[1]
  182. channel = account[2]
  183. timestamp = int(time.time())
  184. sign = md5value(str(uid) + '&' + appsecert + '&' + str(timestamp))
  185. starttime = st
  186. timespace = 90 * 3600 * 24
  187. endtime = min(et, st + timespace)
  188. for x in range((et - st) // timespace + 1): # 分时段
  189. if x > 0:
  190. print('掌读跨天数查询:', x)
  191. params = {
  192. 'uid': uid,
  193. 'timestamp': timestamp,
  194. 'sign': sign,
  195. 'starttime': starttime,
  196. 'endtime': endtime
  197. }
  198. response_result_json = requests.get(url=url, params=params).json()
  199. pageCount = response_result_json['data']['pageCount']
  200. if pageCount == 0:
  201. continue
  202. for page in range(1, pageCount + 1): # 分页
  203. params = {
  204. 'uid': uid,
  205. 'timestamp': timestamp,
  206. 'sign': sign,
  207. 'starttime': starttime,
  208. 'endtime': endtime,
  209. 'page': page
  210. }
  211. list2 = requests.get(url=url, params=params).json()
  212. if 'data' in list2.keys():
  213. for b in list2['data']['list']:
  214. if b['status'] == '1':
  215. c = {}
  216. c['amount'] = b['amount']
  217. c['channel_id'] = uid
  218. c['order_id'] = str(b['orderno'])
  219. c['order_time'] = b['ctime']
  220. c['user_id'] = b['openid']
  221. c['platform'] = '掌读'
  222. c['channel'] = channel
  223. c['reg_time'] = b['regtime']
  224. c['from_novel'] = ''
  225. c['stage'] = ''
  226. c['date'] = ((int(b['ctime']) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  227. x = sorted(c.items(), key=lambda item: item[0])
  228. x = dict(x)
  229. x = tuple(x.values())
  230. order_list = order_list + ((x),)
  231. starttime = starttime + timespace
  232. endtime = min(et, starttime + timespace)
  233. return order_list
  234. ##《3》花生
  235. def get_huasheng_order(st, et):
  236. start_exec_seconds = date_util.getCurrentSecondTime()
  237. total_order_list = ()
  238. account_list = platform_util.get_huasheng_account_list()
  239. executor = ProcessPoolExecutor(max_workers=5)
  240. futures = []
  241. for account in account_list:
  242. url = 'https://vip.rlcps.cn/api/getMerchants'
  243. apiKey = str(account[0])
  244. apiSecurity = account[1]
  245. timestamp = str(int(time.time()))
  246. sign = md5value(apiKey + timestamp + apiSecurity).upper()
  247. params = {
  248. 'apiKey': apiKey,
  249. 'apiSecurity': apiSecurity,
  250. 'timestamp': timestamp,
  251. 'sign': sign
  252. }
  253. response_result_json = requests.post(url, params).json()
  254. if 'data' not in response_result_json.keys():
  255. # print('花生账号【{apiKey}】本次请求数据为空,响应报文【{result}】'.format(apiKey=apiKey, result=response_result_json))
  256. continue
  257. for merchant in response_result_json['data']:
  258. future = executor.submit(get_huasheng_order_task, st, et, account, merchant)
  259. futures.append(future)
  260. executor.shutdown(True)
  261. for future in futures:
  262. order_list = future.result()
  263. if len(order_list) > 0:
  264. total_order_list = order_list + total_order_list
  265. print('花生订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  266. return total_order_list
  267. def get_huasheng_order_task(st, et, account, merchant):
  268. order_list = ()
  269. apiKey = str(account[0])
  270. apiSecurity = account[1]
  271. stage = account[2]
  272. timestamp = str(int(time.time()))
  273. order_url = 'https://vip.rlcps.cn/api/orderList'
  274. merchant_id = merchant['merchant_id']
  275. merchant_name = merchant['merchant_name']
  276. start_time = st
  277. limit = 500
  278. for i in range((et - st) // 86400 + 1):
  279. page = 1
  280. while True:
  281. date = time.strftime("%Y-%m-%d", time.localtime(start_time))
  282. sign = md5value(apiKey + date + str(merchant_id) + timestamp + apiSecurity).upper()
  283. order_params = {
  284. 'apiKey': apiKey,
  285. 'apiSecurity': apiSecurity,
  286. 'timestamp': timestamp,
  287. 'date': date,
  288. 'merchant_id': merchant_id,
  289. 'sign': sign,
  290. 'page': page,
  291. 'limit': limit
  292. }
  293. response_result_json = requests.post(order_url, order_params).json()
  294. if 'data' not in response_result_json.keys() or len(response_result_json['data']) == 0:
  295. # print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】本次请求数据为空,响应报文【{result}】'
  296. # .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name,
  297. # result=response_result_json))
  298. break
  299. total_count = response_result_json['count']
  300. order_item_list = response_result_json['data']
  301. for order_item in order_item_list:
  302. if order_item['order_status'] == 1: # 1为已支付
  303. order = {}
  304. ##dtime = datetime.datetime.strptime(order_item['pay_at'],"%Y-%m-%d")
  305. ##order['date']= ((int(time.mktime(dtime.timetuple()))+8*3600)//86400)*86400-8*3600
  306. order['user_id'] = order_item['openid']
  307. order['order_id'] = order_item['trans_id']
  308. order['order_time'] = order_item['pay_at']
  309. order['reg_time'] = order_item['join_at']
  310. # TODO 花生的时间需要统一
  311. order['date'] = (start_time + 8 * 3600) // 86400 * 86400 - 8 * 3600
  312. order['channel'] = merchant_name
  313. order['channel_id'] = merchant_id
  314. order['platform'] = '花生'
  315. order['stage'] = stage
  316. order['from_novel'] = order_item['book_name']
  317. order['amount'] = order_item['amount']
  318. order = sorted(order.items(), key=lambda item: item[0])
  319. order = dict(order)
  320. order = tuple(order.values())
  321. order_list = order_list + ((order),)
  322. if int(page) >= math.ceil(total_count / int(limit)):
  323. break
  324. # print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  325. # .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name, page=page, total_count=total_count))
  326. page = page + 1
  327. start_time = start_time + 86400 # 天数加1
  328. return order_list
  329. ##《4》掌中云
  330. def get_zzy_order(st, et):
  331. start_exec_seconds = date_util.getCurrentSecondTime()
  332. total_order_list = ()
  333. account_list = al.zzy_account_list
  334. # account_list = platform_util.get_zhangzhongyun_account_list()
  335. # account_list = [['1108701f1d6','0f9c0f8429d1a16a8a78c2306e7a4db3','清勇7月']]
  336. # account_list = [['1109295d56c','9bb955186597882ac473e86ba4576158','趣程20期']]
  337. executor = ProcessPoolExecutor(max_workers=5)
  338. futures = []
  339. for account in account_list:
  340. url = 'https://openapi.818tu.com/partners/channel/channels/list?'
  341. key = account[0]
  342. secert = account[1]
  343. sign = md5value(secert + 'key=' + key)
  344. params = 'key=' + key + '&sign=' + sign
  345. response_result_json = requests.get(url + params).json() # 获取子渠道列表
  346. if 'data' not in response_result_json.keys():
  347. # print('掌中云账号【{key}】本次请求数据为空,响应报文【{result}】'.format(key=key, result=response_result_json))
  348. continue
  349. items = response_result_json['data']['items']
  350. for channel in items:
  351. # 获取channel_id 后逐个拉取历史orders
  352. future = executor.submit(get_zzy_order_task, st, et, account, channel)
  353. futures.append(future)
  354. executor.shutdown(True)
  355. for future in futures:
  356. order_list = future.result()
  357. if len(order_list) > 0:
  358. total_order_list = order_list + total_order_list
  359. print('掌中云订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  360. return total_order_list
  361. def get_zzy_order_task(st, et, account, channel):
  362. # 掌中云的时间格式比较特殊,转换下
  363. st = platform_util.get_zhangzhongyun_format_time(st)
  364. et = platform_util.get_zhangzhongyun_format_time(et)
  365. order_list = ()
  366. key = account[0]
  367. secert = account[1]
  368. stage = account[2]
  369. order_url = 'https://openapi.818tu.com/partners/channel/orders/list?'
  370. channel_id = channel['id']
  371. channel_name = channel['nickname']
  372. status = str(1)
  373. page = str(1)
  374. per_page = str(1000)
  375. get_time = st
  376. limit_time = et
  377. gte = parse.urlencode({'created_at[gte]': get_time}) # gte就是ge 大于等于开始时间
  378. lt = parse.urlencode({'created_at[lt]': limit_time}) # 小于 结束时间
  379. while True:
  380. sign = md5value(secert + 'channel_id=' + str(
  381. channel_id) + '&created_at[gte]=' + get_time + '&created_at[lt]=' + limit_time + '&key=' + key + '&page=' + str(
  382. page) + '&per_page=' + per_page + '&status=' + status)
  383. params = 'channel_id=' + str(channel_id) + '&' + gte + '&' + lt + '&page=' + str(
  384. page) + '&per_page=' + per_page + '&status=' + status + '&key=' + key + '&sign=' + sign
  385. response_result_json = requests.get(order_url + params).json()
  386. if 'data' not in response_result_json.keys():
  387. # print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】本次请求数据为空,响应报文【{result}】'
  388. # .format(key=key, channel_id=channel_id, channel_name=channel_name, result=response_result_json))
  389. break
  390. total_count = response_result_json['data']['count'] # 总数量
  391. order_item_list = response_result_json['data']['items'] # 订单列表
  392. for order_item in order_item_list:
  393. order = {}
  394. order['user_id'] = str(order_item['member']['openid'])
  395. order['channel'] = channel_name
  396. order['reg_time'] = order_item['member']['created_at']
  397. order['channel_id'] = channel_id
  398. order['amount'] = round(order_item['price'] / 100, 2)
  399. order['order_id'] = str(order_item['id'])
  400. order['order_time'] = order_item['created_at']
  401. order['platform'] = '掌中云'
  402. order['stage'] = stage
  403. dtime = datetime.datetime.strptime(order_item['created_at'][0:10], "%Y-%m-%d")
  404. order['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  405. if str(order_item['from_novel_id']) != 'None':
  406. order['from_novel'] = order_item['from_novel']['title']
  407. else:
  408. order['from_novel'] = 'None'
  409. x = sorted(order.items(), key=lambda item: item[0])
  410. x = dict(x)
  411. x = tuple(x.values())
  412. order_list = order_list + ((x),)
  413. if int(page) >= math.ceil(total_count / int(per_page)):
  414. break
  415. # print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  416. # .format(key=key, channel_id=channel_id, channel_name=channel_name, page=page, total_count=total_count))
  417. page = int(page) + 1
  418. return order_list
  419. ##《5》 悠书阁
  420. def get_ysg_order(st, et):
  421. start_exec_seconds = date_util.getCurrentSecondTime()
  422. total_order_list = ()
  423. account_list = platform_util.get_youshuge_account_list()
  424. executor = ProcessPoolExecutor(max_workers=5)
  425. futures = []
  426. for account in account_list:
  427. future = executor.submit(get_ysg_order_task, st, et, account)
  428. futures.append(future)
  429. executor.shutdown(True)
  430. for future in futures:
  431. order_list = future.result()
  432. if len(order_list) > 0:
  433. total_order_list = order_list + total_order_list
  434. print('悠书阁订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  435. return total_order_list
  436. def get_ysg_order_task(st, et, account):
  437. order_list = ()
  438. url = 'https://novel.youshuge.com/v2/open/orders'
  439. # 超过100条就需要分页,别问我为什么知道,看代码看出来的
  440. max_page_size = 100
  441. host_name = account[0]
  442. channel_id = int(account[1])
  443. secert_key = account[2]
  444. channel = account[3]
  445. stage = account[4]
  446. timestamp = int(time.time())
  447. start_date = time.strftime("%Y-%m-%d", time.localtime(st))
  448. end_date = time.strftime("%Y-%m-%d", time.localtime(et))
  449. page = 1
  450. str1 = 'channel_id=' + str(channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  451. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  452. sign = md5value(str1).upper()
  453. data = {
  454. 'sign': sign,
  455. 'host_name': host_name,
  456. 'time': timestamp,
  457. 'channel_id': channel_id,
  458. 'page': page,
  459. 'pay_status': 1,
  460. 'start_date': start_date,
  461. 'end_date': end_date
  462. }
  463. respone = requests.post(url, data)
  464. if respone.status_code == 400:
  465. print('respone', respone)
  466. result_json = respone.json()
  467. first_page_order = build_ysg_order_data(channel, channel_id, result_json, stage)
  468. order_list = order_list + first_page_order
  469. if len(first_page_order) == 0:
  470. return order_list
  471. total_count = result_json['data'][0]['count']
  472. if total_count > max_page_size:
  473. for i in range((total_count - 1) // max_page_size + 1):
  474. timestamp = int(time.time())
  475. str1 = 'channel_id=' + str(
  476. channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  477. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  478. sign = md5value(str1).upper()
  479. data2 = {
  480. 'sign': sign,
  481. 'host_name': host_name,
  482. 'time': timestamp,
  483. 'channel_id': channel_id,
  484. 'page': page,
  485. 'pay_status': 1,
  486. 'start_date': start_date,
  487. 'end_date': end_date
  488. }
  489. r2 = requests.post(url, data2).json()
  490. order_list = order_list + build_ysg_order_data(channel, channel_id, r2, stage)
  491. page = page + 1
  492. return order_list
  493. def build_ysg_order_data(channel, channel_id, result_json, stage):
  494. order_list = ()
  495. if 'data' in result_json.keys():
  496. data = result_json['data']
  497. if len(data) > 0:
  498. for x in data:
  499. y = {}
  500. dtime = datetime.datetime.strptime(x['create_time'][0:10], "%Y-%m-%d")
  501. y['date'] = ((int(
  502. time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  503. y['order_id'] = x['order_num']
  504. y['amount'] = round(int(x['price']) / 100, 2)
  505. y['order_time'] = x['create_time']
  506. y['channel'] = channel
  507. y['from_novel'] = x['book_name']
  508. y['stage'] = stage
  509. y['user_id'] = x['openid']
  510. y['channel_id'] = channel_id
  511. y['platform'] = '悠书阁'
  512. y['reg_time'] = x['reg_time']
  513. y = sorted(y.items(), key=lambda item: item[0])
  514. y = dict(y)
  515. y = tuple(y.values())
  516. order_list = order_list + ((y),)
  517. return order_list
  518. # 数据导入表采用replace替换主键orderid的方法
  519. def mysql_insert_order(data):
  520. if data is None or len(data) == 0:
  521. print('数据为空,不执行数据库操作!')
  522. else:
  523. 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);'
  524. connect = MySQLConnection()
  525. try:
  526. num = connect.batch(sql, data)
  527. # 提交
  528. connect.commit()
  529. print(num, '条订单数据入库成功')
  530. except Exception as e:
  531. traceback.print_exc()
  532. print('订单数据入库失败:', e)
  533. finally:
  534. connect.close()
  535. # 获取各平台的订单数量
  536. def mysql_select_platform_order_count(date):
  537. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order_zwg` WHERE date = %s GROUP BY platform'
  538. connect = MySQLConnection()
  539. platform_order_count = []
  540. try:
  541. platform_order_count = connect.query(sql, date)
  542. return platform_order_count
  543. except Exception as e:
  544. traceback.print_exc()
  545. print('各平台的订单数据查询失败:', e)
  546. finally:
  547. connect.close()
  548. return platform_order_count
  549. def start_all_job():
  550. start_exec_seconds = date_util.getCurrentSecondTime()
  551. st_unix = date_util.getYesterdayStartTime()
  552. et_unix = date_util.getTodayStartTime()
  553. # st_unix = 1601136000 # 2020/9/27 0:0:0
  554. # et_unix = 1601308800 # 2020/9/29 0:0:0
  555. # et_unix = st_unix + 10 # 2020/9/29 0:0:0
  556. print('查询开始时间:', st_unix, date_util.getSecondsToDatetime(st_unix))
  557. print('查询结束时间:', et_unix, date_util.getSecondsToDatetime(et_unix))
  558. order_list = get_yuewen_order(st_unix, et_unix)
  559. mysql_insert_order(order_list)
  560. order_id_list = []
  561. for order in order_list:
  562. if order[5] == '4200000691202009304870914729':
  563. print('存在', order[5])
  564. order_id_list.append(order[5])
  565. print(len(order_id_list))
  566. # b = set(order_id_list)
  567. # for each_b in b:
  568. # count = 0
  569. # for each_a in order_id_list:
  570. # if each_b == each_a:
  571. # count += 1
  572. # print(each_b, ": ", count)
  573. exit_flag = True
  574. if exit_flag:
  575. exit()
  576. platform_order_num_list = mysql_select_platform_order_count(date_util.getYesterdayStartTime())
  577. if len(platform_order_num_list) == 0:
  578. print('本地库中没有任何数据,现在全平台补全')
  579. mysql_insert_order(get_zzy_order(st_unix, et_unix))
  580. mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  581. mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  582. mysql_insert_order(get_ysg_order(st_unix, et_unix))
  583. mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  584. else:
  585. platform_list = ['阅文','悠书阁','掌读','掌中云','花生']
  586. for platform_order_num in platform_order_num_list:
  587. platform = str(platform_order_num['platform'])
  588. num = int(platform_order_num['num'])
  589. platform_list.remove(platform)
  590. if platform == '阅文':
  591. order_list = get_yuewen_order(st_unix, et_unix)
  592. if len(order_list) != num:
  593. print('阅文数据实际订单和已经入库数据差异:', len(order_list) - num)
  594. mysql_insert_order(order_list)
  595. elif platform == '悠书阁':
  596. order_list = get_ysg_order(st_unix, et_unix)
  597. if len(order_list) != num:
  598. print('悠书阁数据实际订单和已经入库数据差异:', len(order_list) - num)
  599. mysql_insert_order(order_list)
  600. elif platform == '掌读':
  601. order_list = get_zhangdu_order(st_unix, et_unix)
  602. if len(order_list) != num:
  603. print('掌读数据实际订单和已经入库数据差异:', len(order_list) - num)
  604. mysql_insert_order(order_list)
  605. elif platform == '掌中云':
  606. order_list = get_zzy_order(st_unix, et_unix)
  607. if len(order_list) != num:
  608. print('掌中云数据实际订单和已经入库数据差异:', len(order_list) - num)
  609. mysql_insert_order(order_list)
  610. elif platform == '花生':
  611. order_list = get_huasheng_order(st_unix, et_unix)
  612. if len(order_list) != num:
  613. print('花生数据实际订单和已经入库数据差异:', len(order_list) - num)
  614. mysql_insert_order(order_list)
  615. else:
  616. print('发现未知平台数据!', platform_order_num)
  617. for platform in platform_list:
  618. if platform == '阅文':
  619. print('阅文没有数据')
  620. mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  621. elif platform == '悠书阁':
  622. print('悠书阁没有数据')
  623. mysql_insert_order(get_ysg_order(st_unix, et_unix))
  624. elif platform == '掌读':
  625. print('掌读没有数据')
  626. mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  627. elif platform == '掌中云':
  628. print('掌中云没有数据')
  629. mysql_insert_order(get_zzy_order(st_unix, et_unix))
  630. elif platform == '花生':
  631. print('花生没有数据')
  632. mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  633. else:
  634. print('什么鬼平台:', platform)
  635. print('订单检查执行时间(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  636. if __name__ == '__main__':
  637. start_all_job()
  638. # scheduler = BlockingScheduler()
  639. # #每天凌晨3点到4点的30分钟都执行一次
  640. # scheduler.add_job(start_all_job, 'cron', hour='3-4', minute='35')
  641. # scheduler.start()