balance_monitoring.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. msg = str_time + ': {} 余额为 {},请及时充值'
  72. cus_msg = str_time + ': {} 可用额度为 {};额度不足,请及时处理'
  73. ord_msg = str_time + ': 超过1小时订单总数量为:{},手机号为:\n {}'
  74. sup_msg = str_time + ':超过30分钟无成功的通道:\n{}'
  75. ord_count_msg = ' {} 面额 {} :{} ; '
  76. art_msg = str_time + ':自动补单2中充值订单 :{}'
  77. #print(ord_df)
  78. #print(sup_df['channel_id'])
  79. #mon_cursor = mon_db.cursor()
  80. try:
  81. #print(3333)
  82. mon_cursor = mon_db.cursor()
  83. #print(mon_cursor)
  84. for item in data_json:
  85. #print(item)
  86. channels = item['channels']
  87. customers = item['customers']
  88. #print(channels)
  89. #print(customes)
  90. for channel in channels:
  91. supplier_name = channel['supplier_name']
  92. balance = channel['balance']
  93. #print(supplier_name)
  94. #print(balance)
  95. ch_df = chanel_df[(chanel_df['supplier_name'] == supplier_name) & (chanel_df['balance'] < balance)]
  96. #print(ch_df)
  97. if ch_df.empty is False:
  98. sup_name = ch_df['supplier_name'].values[0]
  99. balance = ch_df['balance'].values[0]
  100. channel_msg = msg.format(sup_name,balance)
  101. mon_cursor.execute(ins_sql, (channel_msg,group_name1,int(timestamp)))
  102. #print(88888)
  103. for customer in customers:
  104. customer_id = customer['customer_id']
  105. available_balance = customer['available_balance']
  106. customer_df = cus_df[(cus_df['customer_id'] == 54) & (cus_df['available_balance'] < 50000)]
  107. #if customer_df.empty is True:
  108. if((customer_df.empty is False) and (str_time_int % 30 == 0)):
  109. cus_name = customer_df['customer_name'].values[0]
  110. balance = customer_df['available_balance'].values[0]
  111. customer_msg = cus_msg.format(cus_name,balance)
  112. mon_cursor.execute(ins_sql,(customer_msg,group_name3,int(timestamp)))
  113. #print(customer_msg)
  114. '''
  115. if(mf_df.empty is False):
  116. #print(444)
  117. sup_name = mf_df['supplier_name'].values[0]
  118. balance = mf_df['balance'].values[0]
  119. mf_msg = msg.format(sup_name,balance)
  120. mon_cursor.execute(ins_sql, (mf_msg,group_name1,int(timestamp)))
  121. #print(444)
  122. if(zr_df.empty is False):
  123. sup_name = zr_df['supplier_name'].values[0]
  124. balance = zr_df['balance'].values[0]
  125. zr_msg = msg.format(sup_name,balance)
  126. mon_cursor.execute(ins_sql, (zr_msg,group_name1,int(timestamp)))
  127. if(zrwt_df.empty is False):
  128. sup_name = zrwt_df['supplier_name'].values[0]
  129. balance = zrwt_df['balance'].values[0]
  130. zrwt_msg = msg.format(sup_name,balance)
  131. mon_cursor.execute(ins_sql, (zrwt_msg,group_name1,int(timestamp)))
  132. #print(sup_name)
  133. if(yxj_df.empty is False):
  134. sup_name = yxj_df['supplier_name'].values[0]
  135. balance = yxj_df['balance'].values[0]
  136. yxj_msg = msg.format(sup_name,balance)
  137. mon_cursor.execute(ins_sql, (yxj_msg,group_name1,int(timestamp)))
  138. #print(sup_name)
  139. if(fy_df.empty is False):
  140. sup_name = fy_df['supplier_name'].values[0]
  141. balance = fy_df['balance'].values[0]
  142. fy_msg = msg.format(sup_name,balance)
  143. mon_cursor.execute(ins_sql, (fy_msg,group_name1,int(timestamp)))
  144. #print(sup_name)
  145. if(zx_df.empty is False):
  146. sup_name = zx_df['supplier_name'].values[0]
  147. balance = zx_df['balance'].values[0]
  148. zx_msg = msg.format(sup_name,balance)
  149. mon_cursor.execute(ins_sql, (zx_msg,group_name1,int(timestamp)))
  150. #print(sup_name)
  151. if((sht_df.empty is False) and (str_time_int % 30 == 0)):
  152. cus_name = sht_df['customer_name'].values[0]
  153. balance = sht_df['available_balance'].values[0]
  154. sht_msg = cus_msg.format(cus_name,balance)
  155. mon_cursor.execute(ins_sql,(sht_msg,group_name3,int(timestamp)))
  156. #print(cus_name)
  157. if((ylb_df.empty is False) and (str_time_int % 30 == 0)):
  158. cus_name = ylb_df['customer_name'].values[0]
  159. balance = ylb_df['available_balance'].values[0]
  160. ylb_msg = cus_msg.format(cus_name,balance)
  161. mon_cursor.execute(ins_sql,(ylb_msg,group_name3,int(timestamp)))
  162. #print(222)
  163. if((fql_df.empty is False) and (str_time_int % 30 == 0)):
  164. cus_name = fql_df['customer_name'].values[0]
  165. balance = fql_df['available_balance'].values[0]
  166. fql_msg = cus_msg.format(cus_name,balance)
  167. mon_cursor.execute(ins_sql,(fql_msg,group_name3,int(timestamp)))
  168. #print('aaa')
  169. '''
  170. print(str_time_int % 10)
  171. if((ord_df.empty is False) and (str_time_int % 10 == 0)):
  172. #print(len(ord_df))
  173. total_num = ord_df.shape[0]
  174. phone_list = []
  175. if total_num >10:
  176. phone_list = ord_df['used_mobile'].head(10).tolist()
  177. else:
  178. phone_list = ord_df['used_mobile'].values.tolist()
  179. #print(total_num)
  180. #print(phone_list)
  181. ord_msg1 = ord_msg.format(total_num,phone_list)
  182. #print(ord_msg1)
  183. mon_cursor.execute(ins_sql,(ord_msg1,group_name4,int(timestamp)))
  184. logging.info(ord_msg1)
  185. #sup_total_list = ['83','84','85','89','90','91','95','104','105','116','117','118','119','120','121','122','123']
  186. #补单中的订单:
  187. if(ord_count_df.empty is False) and (str_time_int % 10 == 0):
  188. #if(ord_count_df.empty is False) :
  189. ord_count_list = ord_count_df.values.tolist()
  190. #print(ord_count_list)
  191. temp_msg = ''
  192. total_count = 0
  193. for i in range(len(ord_count_list)):
  194. total_count += ord_count_list[i][2]
  195. if i == 0:
  196. temp_msg = str_time + ' 补单中的订单数量为:\n {} 面额 {} :{} ; '.format(ord_count_list[i][0],
  197. int(ord_count_list[i][1]),ord_count_list[i][2])
  198. else:
  199. temp_msg += ord_count_msg.format(ord_count_list[i][0],int(ord_count_list[i][1]),ord_count_list[i][2])
  200. temp_msg = temp_msg + '补单总数量为:【{}】。'.format(total_count)
  201. #print(temp_msg)
  202. mon_cursor.execute(ins_sql,(temp_msg,group_name5,int(timestamp)))
  203. #自动补单2中的订单
  204. if(art_df.empty is False):
  205. #print(art_df)
  206. art_df['flow_amount'] = art_df['flow_amount'].apply(int)
  207. art_msg1 = art_msg.format(art_df.values.tolist())
  208. mon_cursor.execute(ins_sql,(art_msg1,group_name6,int(timestamp)))
  209. sup_total_list = ['83','84','85','89','90','91','95','104','105','116','117','118','119','120','121','122',
  210. '123','124','125','126','127']
  211. sup_list = list(sup_df['channel_id'].values)
  212. #print(sup_list)
  213. dif_list = [i for i in sup_total_list if i not in sup_list]
  214. #print(dif_list)
  215. if (len(dif_list) > 0) and (str_time_int % 10 == 0):
  216. for i in range(len(dif_list)):
  217. if dif_list[i] == '83':
  218. dif_list[i] = '满帆移动网厅'
  219. elif dif_list[i] == '84':
  220. dif_list[i] = '满帆联通网厅'
  221. elif dif_list[i] == '85':
  222. dif_list[i] = '满帆电信网厅'
  223. elif dif_list[i] == '89':
  224. dif_list[i] = '兆蓉移动'
  225. elif dif_list[i] == '90':
  226. dif_list[i] = '兆蓉联通'
  227. elif dif_list[i] == '91':
  228. dif_list[i] = '兆蓉电信'
  229. elif dif_list[i] == '95':
  230. dif_list[i] = '兆蓉移动网厅'
  231. elif dif_list[i] == '104':
  232. dif_list[i] = '亚杉移动网厅'
  233. elif dif_list[i] == '105':
  234. dif_list[i] = '兆蓉电信网厅'
  235. elif dif_list[i] == '116':
  236. dif_list[i] = '兆蓉联通网厅'
  237. elif dif_list[i] == '117':
  238. dif_list[i] = '亚杉电信网厅'
  239. elif dif_list[i] == '118':
  240. dif_list[i] = '智信全国移动'
  241. elif dif_list[i] == '119':
  242. dif_list[i] = '智信全国联通'
  243. elif dif_list[i] == '120':
  244. dif_list[i] = '智信全国电信'
  245. elif dif_list[i] == '121':
  246. dif_list[i] = '易迅捷移动网厅'
  247. elif dif_list[i] == '122':
  248. dif_list[i] = '易迅捷电信网厅'
  249. elif dif_list[i] == '123':
  250. dif_list[i] = '易迅捷联通网厅'
  251. elif dif_list[i] == '124':
  252. dif_list[i] = '亚杉联通网厅'
  253. elif dif_list[i] == '125':
  254. dif_list[i] = '枫叶移动网厅'
  255. elif dif_list[i] == '126':
  256. dif_list[i] = '枫叶联通网厅'
  257. elif dif_list[i] == '127':
  258. dif_list[i] = '枫叶电信网厅'
  259. dif_list = sorted(dif_list)
  260. #print(dif_list)
  261. sup_msg1 = sup_msg.format(dif_list)
  262. mon_cursor.execute(ins_sql,(sup_msg1,group_name2,int(timestamp)))
  263. logging.info(sup_msg1)
  264. #print(dif_list)
  265. except:
  266. #mon_db.rollback()
  267. print('数据回滚')
  268. finally:
  269. mon_cursor.close
  270. mon_db.close
  271. if __name__ == '__main__':
  272. #数据库连接信息
  273. chanel_db_config = {
  274. 'host' : '47.95.217.180',
  275. 'port' : 3306,
  276. 'user' : 'root',
  277. 'password' : '93DkChZMgZRyCbWh',
  278. 'db' : 'fmp',
  279. 'charset' : 'utf8',
  280. 'autocommit' : 1
  281. }
  282. #监控消息数据库连接信息
  283. mon_db_config = {
  284. 'host' : '127.0.0.1',
  285. 'port' : 9001,
  286. 'user' : 'root',
  287. 'password' : 'nibuzhidaowozhidao',
  288. 'db' : 'monitoring',
  289. 'charset' : 'utf8',
  290. 'autocommit' : 1
  291. }
  292. #数据库连接
  293. #db = conMysql(mysql_host,mysql_port,mysql_user,mysql_password,mysql_db)
  294. chanel_db = createPool(chanel_db_config).connection()
  295. mon_db = createPool(mon_db_config).connection()
  296. #通道余额
  297. chanel_sql = '''SELECT supplier_name,balance FROM channel_supplier
  298. WHERE
  299. supplier_name like '%兆蓉%' OR supplier_name like '%满帆%' OR supplier_name like '%亚杉%'
  300. OR supplier_name like '%易迅捷%' OR supplier_name like '%枫叶%' OR supplier_name like '%智信%'
  301. '''
  302. #客户余额
  303. cus_sql = '''
  304. SELECT customer_id,customer_name,(balance + credit_amount - current_amount) 'available_balance'
  305. FROM customer_info
  306. WHERE customer_id IN(47,54)
  307. '''
  308. #超过1小时未处理订单
  309. ord_sql = ''' SELECT used_mobile,(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(apply_date)) 'used_time'
  310. FROM flow_order_info
  311. WHERE
  312. (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(apply_date)) >=3600 AND status NOT IN(4,6) AND
  313. channel_id NOT IN ( 101,102,103)
  314. ORDER BY used_time DESC;
  315. '''
  316. #半小时之内成功的通道
  317. sup_sql = '''
  318. SELECT
  319. channel_id
  320. FROM
  321. flow_order_info
  322. WHERE
  323. (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(check_time)) <=1800 AND
  324. status = 6 AND
  325. channel_id NOT IN (92,93,94,96,97,98,101,102,103,107,108,109,110,111,112,113,114,115)
  326. GROUP BY channel_id;
  327. '''
  328. #自动补单充值中的订单数量:
  329. ord_count_sql = """ SELECT
  330. substring_index(mobile_home,'-',-1) provider , flow_amount,count(*) count_num
  331. FROM
  332. flow_order_info
  333. WHERE
  334. status NOT IN(4,6) AND channel_id IN ( 96,97,98)
  335. GROUP BY provider,flow_amount ORDER BY provider,flow_amount"""
  336. #自动补单2充值中的订单
  337. art_sql = """ SELECT
  338. used_mobile , flow_amount
  339. FROM
  340. flow_order_info
  341. WHERE
  342. status NOT IN(4,6) AND channel_id IN ( 113,114,115)
  343. ORDER BY flow_amount """
  344. chanel_df = getData(chanel_sql,chanel_db)
  345. cus_df = getData(cus_sql,chanel_db)
  346. ord_df = getData(ord_sql,chanel_db)
  347. sup_df = getData(sup_sql,chanel_db)
  348. ord_count_df = getData(ord_count_sql,chanel_db)
  349. art_df = getData(art_sql,chanel_db)
  350. chanel_db.close
  351. #群名:
  352. group_name1 = '通道余额监控群'
  353. group_name2 = '30分钟无成功通道监控群'
  354. group_name3 = '客户授信监控群'
  355. group_name4 = '超时订单监控群'
  356. group_name5 = '补单订单数量监控群'
  357. group_name6 = '自动补单2监控群'
  358. saveData()