balance_monitoring.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. # -*- encoding=utf8 -*-
  2. import re
  3. import time
  4. import json
  5. import logging
  6. import pandas as pd
  7. import pymysql
  8. from DBUtils.PooledDB import PooledDB
  9. #配置输出日志格式
  10. LOG_FORMAT = '%(asctime)s %(filename)s[line:%(lineno)d] %(name)s %(levelname)s %(pathname)s %(message)s'
  11. #配置输出时间格式
  12. DATE_FORMAT = '%Y-%m-%d %H-%M-%S %a'
  13. logging.basicConfig(level = logging.INFO,
  14. format = LOG_FORMAT,
  15. datefmt = DATE_FORMAT,
  16. filename = r"./balance_monitoring.log")
  17. comment_re = re.compile(
  18. '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
  19. re.DOTALL | re.MULTILINE
  20. )
  21. #数据库连接
  22. def conMysql(mysql_host,mysql_port,mysql_user,mysql_password,mysql_db):
  23. db = pymysql.connect(host=mysql_host,port=mysql_port,user=mysql_user, password=mysql_password,db=mysql_db,charset = 'utf8',
  24. autocommit = 1)
  25. print("数据库连接成功")
  26. return db
  27. #创建数据库连接池
  28. def createPool(db_config):
  29. spool = PooledDB(pymysql, 5, **db_config)
  30. return spool
  31. #查询余额
  32. def getData(sql,db):
  33. df = pd.read_sql(sql,con=db)
  34. db.close
  35. #print(df)
  36. return df
  37. #解析带注释的json
  38. def parse_json(filename):
  39. with open(filename,encoding='utf8') as f:
  40. content = ''.join(f.readlines())
  41. ## Looking for comments
  42. match = comment_re.search(content)
  43. while match:
  44. # single line comment
  45. content = content[:match.start()] + content[match.end():]
  46. match = comment_re.search(content)
  47. #print(content)
  48. # Return json file
  49. return json.loads(content)
  50. def saveData():
  51. data_json = parse_json(r'./config.json')
  52. #print(json_data)
  53. '''
  54. ys_df = chanel_df[(chanel_df['supplier_name'] == '亚杉') & (chanel_df['balance'] < 0)]
  55. mf_df = chanel_df[(chanel_df['supplier_name'] == '满帆起航') & (chanel_df['balance'] <= -45000)]
  56. zr_df = chanel_df[(chanel_df['supplier_name'] == '兆蓉') & (chanel_df['balance'] <= 0)]
  57. zrwt_df = chanel_df[(chanel_df['supplier_name'] == '兆蓉WT') & (chanel_df['balance'] <= 5000)]
  58. yxj_df = chanel_df[(chanel_df['supplier_name'] == '易迅捷') & (chanel_df['balance'] <= 10000)]
  59. fy_df = chanel_df[(chanel_df['supplier_name'] == '枫叶') & (chanel_df['balance'] <= 10000)]
  60. zx_df = chanel_df[(chanel_df['supplier_name'] == '智信') & (chanel_df['balance'] <= 2000)]
  61. sht_df = cus_df[(cus_df['customer_id'] == 54) & (cus_df['available_balance'] < 50000)] #十荟团
  62. ylb_df = cus_df[(cus_df['customer_id'] == 47) & (cus_df['available_balance'] < 40000)] #云喇叭
  63. fql_df = cus_df[(cus_df['customer_id'] == 66) & (cus_df['available_balance'] < 20000)] #分期乐
  64. '''
  65. #print(fql_df)
  66. timestamp = time.time()
  67. str_time = time.strftime('%H:%M',time.localtime(timestamp))
  68. str_time_int = int(str(str_time).split(':')[-1])
  69. print(str(str_time).split(':')[-1])
  70. ins_sql = """ INSERT INTO balance_monitoring VALUES(0,%s,%s,1,%s) """
  71. que_sql = " SELECT id FROM balance_monitoring WHERE msg LIKE '%{}%' AND group_name = '{}' "
  72. upd_sql = " UPDATE balance_monitoring SET msg = %s,status = 1,timestamp = %s WHERE id = %s"
  73. group_que_sql = " SELECT id FROM balance_monitoring WHERE group_name = '{}' "
  74. msg = str_time + ': {} 余额为 {},请及时充值'
  75. cus_msg = str_time + ': {} 可用额度为 {};额度不足,请及时处理'
  76. ord_msg = str_time + ': 超过1小时订单总数量为:{},手机号为:\n {}'
  77. sup_msg = str_time + ':超过30分钟无成功的通道:\n{}'
  78. ord_count_msg = ' {} 面额 {} :{} ; '
  79. #art_msg = str_time + ':自动补单2中充值订单 :{}'
  80. #print(ord_df)
  81. #print(sup_df['channel_id'])
  82. #mon_cursor = mon_db.cursor()
  83. try:
  84. #print(3333)
  85. mon_cursor = mon_db.cursor()
  86. #print(mon_cursor)
  87. for item in data_json:
  88. #print(item)
  89. channels = item['channels']
  90. customers = item['customers']
  91. #print(channels)
  92. #print(customes)
  93. for channel in channels:
  94. supplier_name = channel['supplier_name']
  95. balance = channel['balance']
  96. #print(supplier_name)
  97. #print(balance)
  98. ch_df = chanel_df[(chanel_df['supplier_name'] == supplier_name) & (chanel_df['balance'] < balance)]
  99. #print(ch_df)
  100. if ch_df.empty is False:
  101. sup_name = ch_df['supplier_name'].values[0]
  102. balance = ch_df['balance'].values[0]
  103. channel_msg = msg.format(sup_name,balance)
  104. mon_cursor.execute(que_sql.format(supplier_name,group_name1))
  105. channel_ids = mon_cursor.fetchall()
  106. #print('channel_id is {}'.format(channel_ids[0][0]))
  107. if channel_ids is not None:
  108. mon_cursor.execute(upd_sql,(channel_msg,int(timestamp),channel_ids[0][0]))
  109. else:
  110. mon_cursor.execute(ins_sql, (channel_msg,group_name1,int(timestamp)))
  111. #print(88888)
  112. for customer in customers:
  113. customer_id = customer['customer_id']
  114. available_balance = customer['available_balance']
  115. customer_df = cus_df[(cus_df['customer_id'] == customer_id) & (cus_df['available_balance'] < available_balance)]
  116. #if customer_df.empty is False:
  117. if((customer_df.empty is False) and (str_time_int % 30 == 0)):
  118. cus_name = customer_df['customer_name'].values[0]
  119. balance = customer_df['available_balance'].values[0]
  120. customer_msg = cus_msg.format(cus_name,balance)
  121. mon_cursor.execute(que_sql.format(customer_name,group_name3))
  122. customer_ids = mon_cursor.fetchall()
  123. #print('channel_id is {}'.format(channel_ids[0][0]))
  124. if customer_ids is not None:
  125. mon_cursor.execute(upd_sql,(customer_msg,int(timestamp),customer_ids[0][0]))
  126. else:
  127. mon_cursor.execute(ins_sql,(customer_msg,group_name3,int(timestamp)))
  128. #print(customer_msg)
  129. '''
  130. if(mf_df.empty is False):
  131. #print(444)
  132. sup_name = mf_df['supplier_name'].values[0]
  133. balance = mf_df['balance'].values[0]
  134. mf_msg = msg.format(sup_name,balance)
  135. mon_cursor.execute(ins_sql, (mf_msg,group_name1,int(timestamp)))
  136. #print(444)
  137. if(zr_df.empty is False):
  138. sup_name = zr_df['supplier_name'].values[0]
  139. balance = zr_df['balance'].values[0]
  140. zr_msg = msg.format(sup_name,balance)
  141. mon_cursor.execute(ins_sql, (zr_msg,group_name1,int(timestamp)))
  142. if(zrwt_df.empty is False):
  143. sup_name = zrwt_df['supplier_name'].values[0]
  144. balance = zrwt_df['balance'].values[0]
  145. zrwt_msg = msg.format(sup_name,balance)
  146. mon_cursor.execute(ins_sql, (zrwt_msg,group_name1,int(timestamp)))
  147. #print(sup_name)
  148. if(yxj_df.empty is False):
  149. sup_name = yxj_df['supplier_name'].values[0]
  150. balance = yxj_df['balance'].values[0]
  151. yxj_msg = msg.format(sup_name,balance)
  152. mon_cursor.execute(ins_sql, (yxj_msg,group_name1,int(timestamp)))
  153. #print(sup_name)
  154. if(fy_df.empty is False):
  155. sup_name = fy_df['supplier_name'].values[0]
  156. balance = fy_df['balance'].values[0]
  157. fy_msg = msg.format(sup_name,balance)
  158. mon_cursor.execute(ins_sql, (fy_msg,group_name1,int(timestamp)))
  159. #print(sup_name)
  160. if(zx_df.empty is False):
  161. sup_name = zx_df['supplier_name'].values[0]
  162. balance = zx_df['balance'].values[0]
  163. zx_msg = msg.format(sup_name,balance)
  164. mon_cursor.execute(ins_sql, (zx_msg,group_name1,int(timestamp)))
  165. #print(sup_name)
  166. if((sht_df.empty is False) and (str_time_int % 30 == 0)):
  167. cus_name = sht_df['customer_name'].values[0]
  168. balance = sht_df['available_balance'].values[0]
  169. sht_msg = cus_msg.format(cus_name,balance)
  170. mon_cursor.execute(ins_sql,(sht_msg,group_name3,int(timestamp)))
  171. #print(cus_name)
  172. if((ylb_df.empty is False) and (str_time_int % 30 == 0)):
  173. cus_name = ylb_df['customer_name'].values[0]
  174. balance = ylb_df['available_balance'].values[0]
  175. ylb_msg = cus_msg.format(cus_name,balance)
  176. mon_cursor.execute(ins_sql,(ylb_msg,group_name3,int(timestamp)))
  177. #print(222)
  178. if((fql_df.empty is False) and (str_time_int % 30 == 0)):
  179. cus_name = fql_df['customer_name'].values[0]
  180. balance = fql_df['available_balance'].values[0]
  181. fql_msg = cus_msg.format(cus_name,balance)
  182. mon_cursor.execute(ins_sql,(fql_msg,group_name3,int(timestamp)))
  183. #print('aaa')
  184. '''
  185. #print(str_time_int % 10)
  186. #超时订单
  187. if((ord_df.empty is False) and (str_time_int % 10 == 0)):
  188. #print(len(ord_df))
  189. ord_que_sql = group_que_sql.format(group_name4)
  190. total_num = ord_df.shape[0]
  191. phone_list = []
  192. if total_num >10:
  193. phone_list = ord_df['used_mobile'].head(10).tolist()
  194. else:
  195. phone_list = ord_df['used_mobile'].values.tolist()
  196. #print(total_num)
  197. #print(phone_list)
  198. ord_msg1 = ord_msg.format(total_num,phone_list)
  199. #print(ord_msg1)
  200. mon_cursor.execute(ord_que_sql)
  201. over_ids = mon_cursor.fetchall()
  202. if over_ids is not None:
  203. mon_cursor.execute(upd_sql,(ord_msg1,int(timestamp),over_ids[0][0]))
  204. else:
  205. mon_cursor.execute(ins_sql,(ord_msg1,group_name4,int(timestamp)))
  206. logging.info(ord_msg1)
  207. #sup_total_list = ['83','84','85','89','90','91','95','104','105','116','117','118','119','120','121','122','123']
  208. #补单中的订单:
  209. if(ord_count_df.empty is False) and (str_time_int % 10 == 0):
  210. #if(ord_count_df.empty is False) :
  211. ord_count_list = ord_count_df.values.tolist()
  212. #print(ord_count_list)
  213. temp_msg = ''
  214. total_count = 0
  215. for i in range(len(ord_count_list)):
  216. total_count += ord_count_list[i][2]
  217. if i == 0:
  218. temp_msg = str_time + ' 补单中的订单数量为:\n {} 面额 {} :{} ; '.format(ord_count_list[i][0],
  219. int(ord_count_list[i][1]),ord_count_list[i][2])
  220. else:
  221. temp_msg += ord_count_msg.format(ord_count_list[i][0],int(ord_count_list[i][1]),ord_count_list[i][2])
  222. temp_msg = temp_msg + '补单总数量为:【{}】。'.format(total_count)
  223. #print(temp_msg)
  224. mon_cursor.execute(group_que_sql.format(group_name5))
  225. recharge_ids = mon_cursor.fetchall()
  226. if recharge_ids is not None:
  227. mon_cursor.execute(upd_sql,(temp_msg,int(timestamp),recharge_ids[0][0]))
  228. else:
  229. mon_cursor.execute(ins_sql,(temp_msg1,group_name5,int(timestamp)))
  230. #mon_cursor.execute(ins_sql,(temp_msg,group_name5,int(timestamp)))
  231. #自动补单2中的订单
  232. if(art_df.empty is False):
  233. #print(art_df)
  234. art_count_list = art_df.values.tolist()
  235. #print(art_count_list)
  236. temp_msg = ''
  237. total_count = 0
  238. for i in range(len(art_count_list)):
  239. total_count += art_count_list[i][2]
  240. #print(total_count)
  241. if i == 0:
  242. temp_msg = str_time + ' 补单2中的订单数量为:\n {} 面额 {} :{} ; '.format(art_count_list[i][0],
  243. int(art_count_list[i][1]),art_count_list[i][2])
  244. #iprint(temp_msg)
  245. else:
  246. temp_msg += ord_count_msg.format(art_count_list[i][0],int(art_count_list[i][1]),art_count_list[i][2])
  247. temp_msg = temp_msg + '补单2总数量为:【{}】。'.format(total_count)
  248. #print(temp_msg)
  249. mon_cursor.execute(group_que_sql.format(group_name6))
  250. recharge_ids = mon_cursor.fetchall()
  251. if recharge_ids is not None:
  252. mon_cursor.execute(upd_sql,(temp_msg,int(timestamp),recharge_ids[0][0]))
  253. else:
  254. mon_cursor.execute(ins_sql,(temp_msg,group_name6,int(timestamp)))
  255. #mon_cursor.execute(ins_sql,(temp_msg,group_name6,int(timestamp)))
  256. sup_total_list = ['83','84','85','89','90','91','95','105','116','118','119','120','121','122',
  257. '123','125','126','127','142','143','144']
  258. sup_list = list(sup_df['channel_id'].values)
  259. #print(sup_list)
  260. dif_list = [i for i in sup_total_list if i not in sup_list]
  261. #print(dif_list)
  262. if (len(dif_list) > 0) and (str_time_int % 10 == 0):
  263. for i in range(len(dif_list)):
  264. if dif_list[i] == '83':
  265. dif_list[i] = '满帆移动WT'
  266. elif dif_list[i] == '84':
  267. dif_list[i] = '满帆联通WT'
  268. elif dif_list[i] == '85':
  269. dif_list[i] = '满帆电信WT'
  270. elif dif_list[i] == '89':
  271. dif_list[i] = '兆蓉移动'
  272. elif dif_list[i] == '90':
  273. dif_list[i] = '兆蓉联通'
  274. elif dif_list[i] == '91':
  275. dif_list[i] = '兆蓉电信'
  276. elif dif_list[i] == '95':
  277. dif_list[i] = '兆蓉移动WT'
  278. elif dif_list[i] == '142':
  279. dif_list[i] = '亚杉移动WT2'
  280. elif dif_list[i] == '105':
  281. dif_list[i] = '兆蓉电信WT'
  282. elif dif_list[i] == '116':
  283. dif_list[i] = '兆蓉联通WT'
  284. elif dif_list[i] == '144':
  285. dif_list[i] = '亚杉电信WT2'
  286. elif dif_list[i] == '118':
  287. dif_list[i] = '智信全国移动'
  288. elif dif_list[i] == '119':
  289. dif_list[i] = '智信全国联通'
  290. elif dif_list[i] == '120':
  291. dif_list[i] = '智信全国电信'
  292. elif dif_list[i] == '121':
  293. dif_list[i] = '易迅捷移动WT'
  294. elif dif_list[i] == '122':
  295. dif_list[i] = '易迅捷电信WT'
  296. elif dif_list[i] == '123':
  297. dif_list[i] = '易迅捷联通WT'
  298. elif dif_list[i] == '143':
  299. dif_list[i] = '亚杉联通WT2'
  300. elif dif_list[i] == '145':
  301. dif_list[i] = '幻星移动WT'
  302. elif dif_list[i] == '146':
  303. dif_list[i] = '幻星联通WT'
  304. elif dif_list[i] == '147':
  305. dif_list[i] = '幻星电信WT'
  306. elif dif_list[i] == '148':
  307. dif_list[i] = '立纵电信WT'
  308. elif dif_list[i] == '149':
  309. dif_list[i] = '立纵联通WT'
  310. elif dif_list[i] == '150':
  311. dif_list[i] = '立纵移动WT'
  312. elif dif_list[i] == '151':
  313. dif_list[i] = 'A11移动WT'
  314. elif dif_list[i] == '152':
  315. dif_list[i] = 'A11联通WT'
  316. elif dif_list[i] == '153':
  317. dif_list[i] = 'A11电信WT'
  318. elif dif_list[i] == '125':
  319. dif_list[i] = '枫叶移动WT'
  320. elif dif_list[i] == '126':
  321. dif_list[i] = '枫叶联通WT'
  322. elif dif_list[i] == '127':
  323. dif_list[i] = '枫叶电信WT'
  324. dif_list = sorted(dif_list)
  325. #print(dif_list)
  326. sup_msg1 = sup_msg.format(dif_list)
  327. mon_cursor.execute(group_que_sql.format(group_name2))
  328. recharge_ids = mon_cursor.fetchall()
  329. if recharge_ids is not None:
  330. mon_cursor.execute(upd_sql,(sup_msg1,int(timestamp),recharge_ids[0][0]))
  331. else:
  332. mon_cursor.execute(ins_sql,(sup_msg1,group_name2,int(timestamp)))
  333. #mon_cursor.execute(ins_sql,(sup_msg1,group_name2,int(timestamp)))
  334. logging.info(sup_msg1)
  335. #print(dif_list)
  336. except Exception as ex:
  337. #mon_db.rollback()
  338. print(ex)
  339. print('数据回滚')
  340. finally:
  341. mon_cursor.close
  342. mon_db.close
  343. if __name__ == '__main__':
  344. #数据库连接信息
  345. chanel_db_config = {
  346. 'host' : '47.95.217.180',
  347. 'port' : 3306,
  348. 'user' : 'root',
  349. 'password' : '93DkChZMgZRyCbWh',
  350. 'db' : 'fmp',
  351. 'charset' : 'utf8',
  352. 'autocommit' : 1
  353. }
  354. #监控消息数据库连接信息
  355. mon_db_config = {
  356. 'host' : '127.0.0.1',
  357. 'port' : 9001,
  358. 'user' : 'root',
  359. 'password' : 'nibuzhidaowozhidao',
  360. 'db' : 'monitoring',
  361. 'charset' : 'utf8',
  362. 'autocommit' : 1
  363. }
  364. #数据库连接
  365. #db = conMysql(mysql_host,mysql_port,mysql_user,mysql_password,mysql_db)
  366. chanel_db = createPool(chanel_db_config).connection()
  367. mon_db = createPool(mon_db_config).connection()
  368. #通道余额
  369. chanel_sql = '''SELECT supplier_name,balance FROM channel_supplier
  370. WHERE
  371. supplier_name like '%兆蓉%' OR supplier_name like '%满帆%' OR supplier_name like '%亚杉%'
  372. OR supplier_name like '%易迅捷%' OR supplier_name like '%枫叶%' OR supplier_name like '%智信%'
  373. '''
  374. #客户余额
  375. cus_sql = '''
  376. SELECT customer_id,customer_name,(balance + credit_amount - current_amount) 'available_balance'
  377. FROM customer_info
  378. WHERE customer_id IN(47,54)
  379. '''
  380. #超过1小时未处理订单
  381. ord_sql = ''' SELECT used_mobile,(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(apply_date)) 'used_time'
  382. FROM flow_order_info
  383. WHERE
  384. (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(apply_date)) >=3600 AND status NOT IN(4,6) AND
  385. channel_id NOT IN ( 101,102,103)
  386. ORDER BY used_time DESC;
  387. '''
  388. #半小时之内成功的通道
  389. sup_sql = '''
  390. SELECT
  391. channel_id
  392. FROM
  393. flow_order_info
  394. WHERE
  395. (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(check_time)) <=1800 AND
  396. status = 6 AND
  397. channel_id NOT IN (92,93,94,96,97,98,101,102,103,107,108,109,110,111,112,113,114,115)
  398. GROUP BY channel_id;
  399. '''
  400. #自动补单充值中的订单数量:
  401. ord_count_sql = """ SELECT
  402. substring_index(mobile_home,'-',-1) provider , flow_amount,count(*) count_num
  403. FROM
  404. flow_order_info
  405. WHERE
  406. status NOT IN(4,6) AND channel_id IN ( 96,97,98)
  407. GROUP BY provider,flow_amount ORDER BY provider,flow_amount"""
  408. #自动补单2充值中的订单
  409. art_sql = """ SELECT
  410. substring_index(mobile_home,'-',-1) provider , flow_amount,count(*) count_num
  411. FROM
  412. flow_order_info
  413. WHERE
  414. status NOT IN(4,6) AND channel_id IN ( 113,114,115)
  415. GROUP BY provider,flow_amount ORDER BY provider,flow_amount """
  416. chanel_df = getData(chanel_sql,chanel_db)
  417. cus_df = getData(cus_sql,chanel_db)
  418. ord_df = getData(ord_sql,chanel_db)
  419. sup_df = getData(sup_sql,chanel_db)
  420. ord_count_df = getData(ord_count_sql,chanel_db)
  421. art_df = getData(art_sql,chanel_db)
  422. chanel_db.close
  423. #群名:
  424. group_name1 = '通道余额监控群'
  425. group_name2 = '30分钟无成功通道监控群'
  426. group_name3 = '客户授信监控群'
  427. group_name4 = '超时订单监控群'
  428. group_name5 = '补单订单数量监控群'
  429. group_name6 = '自动补单2监控群'
  430. saveData()