count_data.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. # -*- coding: utf-8 -*-
  2. # Author: dukun
  3. import pymysql #Python3的mysql模块,Python2 是mysqldb
  4. import os
  5. import datetime #定时发送,以及日期
  6. import shutil #文件操作
  7. import smtplib #邮件模块
  8. from email.mime.text import MIMEText
  9. from email.mime.multipart import MIMEMultipart
  10. from email.header import Header
  11. import time
  12. import xlwt
  13. #连接数据库
  14. def conMysql(mysql_host,mysql_user,mysql_password,mysql_db):
  15. db = pymysql.connect(host=mysql_host,user=mysql_user, password=mysql_password,db=mysql_db,charset = 'utf8')
  16. print("数据库连接成功")
  17. return db
  18. #获取数据
  19. def getPassData(sql,db):
  20. #db = conMysql(mysql_host,mysql_user,mysql_password,mysql_db)
  21. cursor = db.cursor()
  22. try:
  23. # 执行SQL语句
  24. cursor.execute(sql)
  25. # 获取所有记录列表
  26. results = cursor.fetchall()
  27. #print(results)
  28. cursor.scroll(0,mode='absolute')
  29. #cursor.description获取表格的字段信息
  30. fields=cursor.description
  31. return results,fields
  32. except:
  33. print ("Error: unable to fetch data")
  34. return 0
  35. cursor.scroll(0,mode='absolute')
  36. #cursor.description获取表格的字段信息
  37. fields=cursor.description
  38. #print(fields)
  39. cursor.close()
  40. db.close()
  41. #入表格
  42. def writeToExcel(fLocate,file_sheet,style1,style2,style3):
  43. results,fields = getPassData(sql,db)#通道数据
  44. cus_results,cus_fields = getPassData(cus_sql,db) #客户数据
  45. ord_results,ord_fields = getPassData(ord_sql,db) #订单时间统计
  46. mob_results,mob_fields = getPassData(mob_sql,db) #满帆移动次数统计
  47. uni_results,uni_fields = getPassData(uni_sql,db) #满帆联通次数统计
  48. tel_results,tel_fields = getPassData(tel_sql,db) #满帆电信次数统计
  49. ys_results,ys_fields = getPassData(ys_sql,db) #亚杉移动次数统计
  50. zr_results,zr_fields = getPassData(zr_sql,db) #兆荣移动WT次数统计
  51. zr_uni_results,zr_uni_fields = getPassData(zr_uni_sql,db) #兆荣联通WT次数统计
  52. zr_tel_results,zr_tel_fields = getPassData(zr_tel_sql,db) #兆荣电信WT次数统计
  53. if(results != 0):
  54. if os.path.exists(fLocate):
  55. os.remove(fLocate) # 如果文件存在,则删除
  56. #将查询结果写入到excel
  57. workbook = xlwt.Workbook(encoding = 'utf-8')
  58. #通道数据表
  59. sheet = workbook.add_sheet(file_sheet,cell_overwrite_ok=True)
  60. #通道成功次数表
  61. sheet2 = workbook.add_sheet("通道成功次数统计",cell_overwrite_ok=True)
  62. #sheet1 列宽
  63. for i in range(0,100):
  64. if i == 1:
  65. sheet.col(i).width = 256 * 25
  66. sheet2.col(i).width = 256 * 25
  67. else:
  68. sheet.col(i).width = style1.width
  69. #sheet2 列宽
  70. for i in range(0,100):
  71. sheet2.col(i).width = 256 * 15
  72. #sheet.col(2).width = 256 * 100
  73. #将通道数据表头写入excel
  74. sheet.write(0,0,"序号",style1)
  75. sheet.write(0,1,"通道",style1)
  76. sheet.write(0,2,"面额",style1)
  77. sheet.write(0,3,"订单数",style1)
  78. sheet.write(0,4,"金额百分比",style1)
  79. #订单时间统计表头
  80. sheet2.write_merge(0, 0, 0, 6, 'First Merge',style1)
  81. sheet2.write(0,0,"订单时间百分比",style1)
  82. sheet2.write(1,0,"小于10分钟",style1)
  83. sheet2.write(1,1,"10-20分钟",style1)
  84. sheet2.write(1,2,"20-30分钟",style1)
  85. sheet2.write(1,3,"30-40分钟",style1)
  86. sheet2.write(1,4,"40-50分钟",style1)
  87. sheet2.write(1,5,"50-60分钟",style1)
  88. sheet2.write(1,6,"大于60分钟",style1)
  89. #通道成功次数统计
  90. sheet2.write_merge(5, 5, 0, 10, 'Second Merge',style1)
  91. sheet2.write(5,0,"通道成功次数统计",style1)
  92. sheet2.write(6,0,"通道",style1)
  93. sheet2.write(6,1,"1-5次",style1)
  94. sheet2.write(6,2,"6-10次",style1)
  95. sheet2.write(6,3,"11-15次",style1)
  96. sheet2.write(6,4,"16-20次",style1)
  97. sheet2.write(6,5,"21-30次",style1)
  98. sheet2.write(6,6,"31-40次",style1)
  99. sheet2.write(6,7,"41-50次",style1)
  100. sheet2.write(6,8,"51-60次",style1)
  101. sheet2.write(6,9,"大于60次",style1)
  102. sheet2.write(6,10,"总订单数",style1)
  103. sheet2.write(7,0,"满帆移动",style1)
  104. sheet2.write(8,0,"满帆联通",style1)
  105. sheet2.write(9,0,"满帆电信",style1)
  106. sheet2.write(10,0,"亚杉移动",style1)
  107. sheet2.write(11,0,"兆蓉移动WT",style1)
  108. sheet2.write(12,0,"兆蓉联通WT",style1)
  109. sheet2.write(13,0,"兆蓉电信WT",style1)
  110. sheet2.write(14,0,"总计",style1)
  111. sheet2.write(15,0,"百分比",style1)
  112. #订单时间统计写入excle
  113. ord_col = len(ord_fields)
  114. for j in range(ord_col):
  115. sheet2.write(2,j,ord_results[0][j],style1)
  116. #通道成功次数统计写入excle
  117. mob_col = len(mob_fields)
  118. for j in range(1,mob_col+1):
  119. sheet2.write(7,j,mob_results[0][j-1],style1)
  120. sheet2.write(8,j,uni_results[0][j-1],style1)
  121. sheet2.write(9,j,tel_results[0][j-1],style1)
  122. sheet2.write(10,j,ys_results[0][j-1],style1)
  123. sheet2.write(11,j,zr_results[0][j-1],style1)
  124. sheet2.write(12,j,zr_uni_results[0][j-1],style1)
  125. sheet2.write(13,j,zr_tel_results[0][j-1],style1)
  126. c_row = 14
  127. #通道成功次数总计栏
  128. sheet2.write(c_row,1,xlwt.Formula("SUM($B$8:$B${})".format(c_row)),style1)
  129. sheet2.write(c_row,2,xlwt.Formula("SUM($C$8:$C${})".format(c_row)),style1)
  130. sheet2.write(c_row,3,xlwt.Formula("SUM($D$8:$D${})".format(c_row)),style1)
  131. sheet2.write(c_row,4,xlwt.Formula("SUM($E$8:$E${})".format(c_row)),style1)
  132. sheet2.write(c_row,5,xlwt.Formula("SUM($F$8:$F${})".format(c_row)),style1)
  133. sheet2.write(c_row,6,xlwt.Formula("SUM($G$8:$G${})".format(c_row)),style1)
  134. sheet2.write(c_row,7,xlwt.Formula("SUM($H$8:$H${})".format(c_row)),style1)
  135. sheet2.write(c_row,8,xlwt.Formula("SUM($I$8:$I${})".format(c_row)),style1)
  136. sheet2.write(c_row,9,xlwt.Formula("SUM($J$8:$J${})".format(c_row)),style1)
  137. sheet2.write(c_row,10,xlwt.Formula("SUM($K$8:$K${})".format(c_row)),style1)
  138. #通道成功次数百分比
  139. sheet2.write(c_row+1,1,xlwt.Formula("$B${}/$K${}".format(c_row+1,c_row+1)),style2)
  140. sheet2.write(c_row+1,2,xlwt.Formula("$C${}/$K${}".format(c_row+1,c_row+1)),style2)
  141. sheet2.write(c_row+1,3,xlwt.Formula("$D${}/$K${}".format(c_row+1,c_row+1)),style2)
  142. sheet2.write(c_row+1,4,xlwt.Formula("$E${}/$K${}".format(c_row+1,c_row+1)),style2)
  143. sheet2.write(c_row+1,5,xlwt.Formula("$F${}/$K${}".format(c_row+1,c_row+1)),style2)
  144. sheet2.write(c_row+1,6,xlwt.Formula("$G${}/$K${}".format(c_row+1,c_row+1)),style2)
  145. sheet2.write(c_row+1,7,xlwt.Formula("$H${}/$K${}".format(c_row+1,c_row+1)),style2)
  146. sheet2.write(c_row+1,8,xlwt.Formula("$I${}/$K${}".format(c_row+1,c_row+1)),style2)
  147. sheet2.write(c_row+1,9,xlwt.Formula("$J${}/$K${}".format(c_row+1,c_row+1)),style2)
  148. #通道数据写入excle
  149. num = 1
  150. row = len(results)
  151. col = len(fields)
  152. for i in range(1,row+1):
  153. for j in range(col):
  154. if j == 0:
  155. sheet.write(i,j,num,style1)
  156. num += 1
  157. else:
  158. sheet.write(i,j,results[i-1][j],style1)
  159. #客户数据表头
  160. sheet.write(row+4,0,"序号",style1)
  161. sheet.write(row+4,1,"客户",style1)
  162. sheet.write(row+4,2,"面额",style1)
  163. sheet.write(row+4,3,"订单数",style1)
  164. sheet.write(row+4,4,"金额百分比",style1)
  165. #客户数据
  166. income_money = 0;
  167. num = 1
  168. cus_row = len(cus_results)
  169. cus_col = len(cus_fields)
  170. for i in range(row+5,row+5 + cus_row):
  171. for j in range(cus_col):
  172. if cus_results[i-row-5][0] >= 57:
  173. #cus_num += 1
  174. #print('cus_num:{}'.format(cus_num))
  175. continue
  176. else:
  177. if j == 0:
  178. sheet.write(i,j,num,style1)
  179. num += 1
  180. else:
  181. sheet.write(i,j,cus_results[i-row-5][j],style1)
  182. #总收入
  183. for i in range(1,cus_row):
  184. if cus_results[i-1][0] >= 57:
  185. continue
  186. else:
  187. if ('身边科技' in cus_results[i-1][1]):
  188. income_money = income_money + 0.978 * cus_results[i-1][2]
  189. #print('身边科技:%d'%income_money)
  190. elif ('十荟' in cus_results[i-1][1]):
  191. income_money = income_money + 0.985 * cus_results[i-1][2]
  192. #print('十荟:%d'%income_money)
  193. elif ('脉粒' in cus_results[i-1][1]):
  194. income_money = income_money + 0.984 * cus_results[i-1][2]
  195. #print('脉粒:%d'%income_money)
  196. elif ('小米' in cus_results[i-1][1]):
  197. income_money = income_money + 0.985 * cus_results[i-1][2]
  198. #print('小米:%d'%income_money)
  199. elif ('挖财' in cus_results[i-1][1]):
  200. income_money = income_money + 0.985 * cus_results[i-1][2]
  201. #print('挖财:%d'%income_money)
  202. elif ('H5' in cus_results[i-1][1]):
  203. income_money = income_money + 0.989 * cus_results[i-1][2]
  204. #print('H5:%d'%income_money)
  205. elif ('折800' in cus_results[i-1][1]):
  206. income_money = income_money + 0.985 * cus_results[i-1][2]
  207. #print('折800:%d'%income_money)
  208. #合计栏
  209. for i in range(0,5):
  210. #if cus_results[i-row-6][0] >= 57:
  211. # cus_num += 1
  212. if i == 0:
  213. sheet.write(row+1,i,row+1,style1)
  214. sheet.write(row+num+4,i,num,style1)
  215. elif i == 1:
  216. sheet.write(row+1,i,'合计',style1)
  217. sheet.write(row+num+4,i,'合计',style1)
  218. elif i == 2:
  219. sheet.write(row+1,i,xlwt.Formula("SUM($C$%d:$C$%d)"%(2,row+1)),style1)
  220. sheet.write(row+num+4,i,xlwt.Formula("SUM($C$%d:$C$%d)"%(row+6,row+num+4)),style1)
  221. elif i == 3:
  222. sheet.write(row+1,i,xlwt.Formula("SUM($D$%d:$D$%d)"%(2,row+1)),style1)
  223. sheet.write(row+num+4,i,xlwt.Formula("SUM($D$%d:$D$%d)"%(row+6,row+num+4)),style1)
  224. else:
  225. #sheet.write(row+1,i,' ')
  226. #sheet.write(row+1,i,' ')
  227. pass
  228. #print(row)
  229. #print(col)
  230. #通道金额百分比
  231. for i in range(1,row+1):
  232. j = i + 1
  233. sheet.write(i,col,xlwt.Formula("$C$%d/$C$%d"%(j,row+2)),style2)
  234. #客户金额百分比
  235. for i in range(row+5,row+num+4):
  236. j = i + 1
  237. sheet.write(i,col,xlwt.Formula("$C$%d/$C$%d"%(j,row+num+5)),style2)
  238. sheet.write(0,col+2,"网厅百分比",style2)
  239. sheet.write(0,col+3,"补单面额",style1)
  240. sheet.write(0,col+4,"均单值[995预估]",style3)
  241. sheet.write(0,col+5,"均单面值",style3)
  242. supOrder_money = 0 #补单面额
  243. cost_money = 0 #成本
  244. net_money = 0 #网厅金额
  245. sup = 0
  246. for i in range(1,row+1):
  247. #for j in rang(col+1):
  248. #print(results[i-1][1])
  249. if('补单' in results[i-1][1]):
  250. supOrder_money += results[i-1][2]
  251. #print(supOrder_money)
  252. cost_money += 1.0 * results[i-1][2]
  253. elif('人工' in results[i-1][1]):
  254. supOrder_money += results[i-1][2]
  255. cost_money += results[i-1][2]
  256. elif('亚杉' in results[i-1][1]):
  257. net_money += results[i-1][2]
  258. cost_money = cost_money + 0.979 * results[i-1][2]
  259. elif('满帆移动' in results[i-1][1]):
  260. net_money += results[i-1][2]
  261. cost_money = cost_money + 0.979 * results[i-1][2]
  262. #print(results[i-1][2])
  263. #print(cost_money)
  264. elif('满帆联通' in results[i-1][1]):
  265. net_money += results[i-1][2]
  266. cost_money = cost_money + 0.978 * results[i-1][2]
  267. #print(cost_money)
  268. elif('满帆电信' in results[i-1][1]):
  269. net_money += results[i-1][2]
  270. cost_money = cost_money + 0.974 * results[i-1][2]
  271. #print(cost_money)
  272. elif('兆蓉移动WT' == results[i-1][1]):
  273. net_money += results[i-1][2]
  274. cost_money = cost_money + 0.975 * results[i-1][2]
  275. #print(cost_money)
  276. elif('兆蓉联通WT' == results[i-1][1]):
  277. net_money += results[i-1][2]
  278. cost_money = cost_money + 0.972 * results[i-1][2]
  279. elif('兆蓉电信WT' == results[i-1][1]):
  280. net_money += results[i-1][2]
  281. cost_money = cost_money + 0.970 * results[i-1][2]
  282. elif('兆蓉移动' == results[i-1][1]):
  283. #print(results[i-1][2])
  284. cost_money = cost_money + 0.9972 * results[i-1][2]
  285. #print(cost_money)
  286. elif('兆蓉联通' == results[i-1][1]):
  287. cost_money = cost_money + 1.01 * results[i-1][2]
  288. #print(cost_money)
  289. elif('兆蓉电信' == results[i-1][1]):
  290. cost_money = cost_money + 1.0 * results[i-1][2]
  291. #print(cost_money)
  292. #print('=========================')
  293. sheet.write(1,col+2,xlwt.Formula("%d / SUM($C$%d:$C$%d) "%(net_money,2,row+1)),style2)
  294. sheet.write(1,col+3,supOrder_money,style1)
  295. sheet.write(1,col+4,xlwt.Formula("0.995 * SUM($C$%d:$C$%d)/SUM($D$%d:$D$%d) "%(2,row+1,2,row+1)),style3)
  296. sheet.write(1,col+5,xlwt.Formula("SUM($C$%d:$C$%d)/SUM($D$%d:$D$%d) "%(2,row+1,2,row+1)),style3)
  297. sheet.write(5,col+2,'总成本')
  298. sheet.write(6,col+2,'总收入')
  299. sheet.write(7,col+2,'总利润')
  300. sheet.write(8,col+2,'利润率')
  301. style4 = xlwt.XFStyle()#设置利润率小数点保留位数
  302. style4.num_format_str='0.000000000'
  303. sheet.write(5,col+3,cost_money)
  304. sheet.write(6,col+3,income_money)
  305. sheet.write(7,col+3,xlwt.Formula("H7 - H6"))
  306. sheet.write(8,col+3,xlwt.Formula("H8 / H7 "),style4)
  307. workbook.save(fLocate)
  308. #设置表格样式
  309. def setStyle():
  310. #初始化样式
  311. style1 = xlwt.XFStyle()
  312. style2 = xlwt.XFStyle()
  313. style3 = xlwt.XFStyle()
  314. font = xlwt.Font() # 为样式创建字体
  315. font.name = '宋体'
  316. # 设置字体颜色
  317. #font.colour_index = color
  318. # 字体大小
  319. font.height = 20 * 11
  320. #设置边框
  321. color = 1
  322. borders = xlwt.Borders()
  323. borders.left = color
  324. borders.left = xlwt.Borders.THIN
  325. borders.right = color
  326. borders.top = color
  327. borders.bottom = color
  328. #对齐方式
  329. al = xlwt.Alignment()
  330. al.horz = 0x02 # 设置水平居中
  331. al.vert = 0x01 # 设置垂直居中
  332. #al.wrap = 1 # 自动换行
  333. #列宽
  334. width = 256 * 18
  335. # 定义格式
  336. style1.borders = borders
  337. style1.alignment = al
  338. style1.font = font
  339. style1.width = width
  340. style2.borders = borders
  341. style2.alignment = al
  342. style2.font = font
  343. style2.width = width
  344. style2.num_format_str='0%'
  345. style3.borders = borders
  346. style3.alignment = al
  347. style3.font = font
  348. style3.width = width
  349. style3.num_format_str='0.00'
  350. return style1,style2,style3
  351. def sendMail(sender,receiver,mail_user,mail_pass,mail_host,subject,content,fLocate,file_name):
  352. try:
  353. #邮件头
  354. message = MIMEMultipart()
  355. message['From'] = "{}".format(sender)#发送
  356. message['To'] = ",".join(receiver)#收件
  357. message['Subject'] = Header(subject, 'utf-8')
  358. message.attach(MIMEText(content, 'plain', 'utf-8'))# 邮件正文
  359. # 构造附件
  360. att1 = MIMEText(open(fLocate,'rb').read(), 'plain', 'utf-8')
  361. #att1 = MIMEApplication(open(fLocate))
  362. att1["Content-Type"] = 'application/octet-stream'
  363. #att1["Content-Disposition"] = "attachment; filename=" + file_name
  364. att1.add_header('Content-Disposition', 'attachment', filename=file_name)
  365. message.attach(att1)
  366. #执行
  367. smtp = smtplib.SMTP()
  368. #smtp.connect(smtpserver) #连接服务器
  369. smtp = smtplib.SMTP_SSL(mail_host, 465)
  370. smtp.login(mail_user, mail_pass) #登录
  371. smtp.sendmail(sender, receiver, message.as_string()) #发送
  372. smtp.quit()
  373. print("SEND SUCCES")
  374. except:
  375. print("SEND FALSE")
  376. if __name__ == '__main__':
  377. #数据库连接信息
  378. mysql_host = '47.95.217.180'
  379. mysql_user = 'rtech'
  380. mysql_password = 'S5RE4dQ65MphYk7F'
  381. mysql_db = 'fmp'
  382. #数据库连接
  383. db = conMysql(mysql_host,mysql_user,mysql_password,mysql_db)
  384. #获取时间
  385. today = datetime.date.today()
  386. yesterday = today - datetime.timedelta(days=1)
  387. yesterdayStart = yesterday.strftime("%Y-%m-%d") + ' 00:00:00'
  388. yesterdayEnd = yesterday.strftime("%Y-%m-%d") + ' 23:59:59'
  389. #通道sql语句
  390. sql = """SELECT
  391. t1.channel_id, access_channel_info.channel_name,t1.flow_amount_sum,t1.count_num
  392. FROM
  393. (SELECT
  394. channel_id,SUM(flow_amount) flow_amount_sum,count(*) count_num
  395. FROM
  396. flow_order_info
  397. WHERE
  398. apply_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ ' AND
  399. status = 6 AND
  400. channel_id NOT IN ( 101,102,103,110,111,112 )
  401. GROUP BY channel_id) t1 LEFT JOIN access_channel_info
  402. ON t1.channel_id = access_channel_info.channel_seq_id; """
  403. #客户sql
  404. cus_sql = """SELECT
  405. t1.enterprise_id,customer_name,t1.flow_amount_sum,t1.count_num
  406. FROM
  407. (SELECT
  408. enterprise_id,SUM(flow_amount) flow_amount_sum,count(*) count_num
  409. FROM
  410. flow_order_info
  411. WHERE
  412. apply_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ ' AND
  413. status = 6
  414. GROUP BY enterprise_id)t1 LEFT JOIN customer_info
  415. ON
  416. t1.enterprise_id = customer_id; """
  417. #订单时间比例统计sql
  418. ord_sql = """ SELECT
  419. CONCAT(CAST(round((A/H)*100,2) AS CHAR),'%') as 'AA[<10min]',
  420. CONCAT(CAST(round((B/H)*100,2) AS CHAR),'%') as 'BB[10-20min]',
  421. CONCAT(CAST(round((C/H)*100,2) AS CHAR),'%') as 'CC[20-30min]',
  422. CONCAT(CAST(round((D/H)*100,2) AS CHAR),'%') as 'DD[30-40min]',
  423. CONCAT(CAST(round((E/H)*100,2) AS CHAR),'%') as 'EE[40-50min]',
  424. CONCAT(CAST(round((F/H)*100,2) AS CHAR),'%') as 'FF[50-60min]',
  425. CONCAT(CAST(round((G/H)*100,2) AS CHAR),'%') as 'GG[>60min]'
  426. FROM
  427. (SELECT
  428. SUM(CASE WHEN diff_time BETWEEN 0 AND 600 THEN 1 ELSE 0 END) AS A,
  429. SUM(CASE WHEN diff_time BETWEEN 601 AND 1200 THEN 1 ELSE 0 END) AS B,
  430. SUM(CASE WHEN diff_time BETWEEN 1201 AND 1800 THEN 1 ELSE 0 END) AS C,
  431. SUM(CASE WHEN diff_time BETWEEN 1801 AND 2400 THEN 1 ELSE 0 END) AS D,
  432. SUM(CASE WHEN diff_time BETWEEN 2401 AND 3000 THEN 1 ELSE 0 END) AS E,
  433. SUM(CASE WHEN diff_time BETWEEN 3001 AND 3600 THEN 1 ELSE 0 END) AS F,
  434. SUM(CASE WHEN diff_time > 3600 THEN 1 ELSE 0 END) AS G,
  435. COUNT(*) AS H
  436. FROM
  437. (SELECT
  438. (UNIX_TIMESTAMP(check_time) - UNIX_TIMESTAMP(apply_date)) AS diff_time
  439. FROM
  440. flow_order_info
  441. WHERE
  442. apply_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ ' AND
  443. status = 6 AND channel_id NOT IN ( 101,102,103,110,111,112 ) ) AS t1 ) AS t2 """
  444. #满帆移动通道成功次数统计
  445. mob_sql = """SELECT
  446. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  447. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  448. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  449. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  450. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  451. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  452. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  453. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  454. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  455. COUNT(*) AS 'H[总订单数]'
  456. FROM
  457. ( SELECT
  458. t1.order_id, t1.cnt
  459. FROM
  460. (SELECT
  461. order_id, COUNT(*) AS cnt
  462. FROM
  463. mobile_flow_dispatch_rec
  464. WHERE
  465. trans_id = 83 AND
  466. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  467. GROUP BY
  468. order_id
  469. ORDER BY
  470. cnt ASC) t1 LEFT JOIN flow_order_info
  471. ON
  472. t1.order_id = flow_order_info.order_id
  473. WHERE
  474. flow_order_info.channel_id = 83 AND
  475. flow_order_info.status = 6 )t2; """
  476. #满帆联通通道成功次数统计
  477. uni_sql = """SELECT
  478. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  479. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  480. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  481. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  482. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  483. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  484. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  485. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  486. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  487. COUNT(*) AS 'H[总订单数]'
  488. FROM
  489. ( SELECT
  490. t1.order_id, t1.cnt
  491. FROM
  492. (SELECT
  493. order_id, COUNT(*) AS cnt
  494. FROM
  495. mobile_flow_dispatch_rec
  496. WHERE
  497. trans_id = 84 AND
  498. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  499. GROUP BY
  500. order_id
  501. ORDER BY
  502. cnt ASC) t1 LEFT JOIN flow_order_info
  503. ON
  504. t1.order_id = flow_order_info.order_id
  505. WHERE
  506. flow_order_info.channel_id = 84 AND
  507. flow_order_info.status = 6 )t2; """
  508. #满帆电信通道成功次数统计
  509. tel_sql = """SELECT
  510. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  511. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  512. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  513. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  514. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  515. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  516. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  517. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  518. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  519. COUNT(*) AS 'H[总订单数]'
  520. FROM
  521. ( SELECT
  522. t1.order_id, t1.cnt
  523. FROM
  524. (SELECT
  525. order_id, COUNT(*) AS cnt
  526. FROM
  527. mobile_flow_dispatch_rec
  528. WHERE
  529. trans_id = 85 AND
  530. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  531. GROUP BY
  532. order_id
  533. ORDER BY
  534. cnt ASC) t1 LEFT JOIN flow_order_info
  535. ON
  536. t1.order_id = flow_order_info.order_id
  537. WHERE
  538. flow_order_info.channel_id = 85 AND
  539. flow_order_info.status = 6 )t2; """
  540. #亚杉移动通道成功次数统计
  541. ys_sql = """SELECT
  542. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  543. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  544. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  545. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  546. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  547. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  548. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  549. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  550. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  551. COUNT(*) AS 'H[总订单数]'
  552. FROM
  553. ( SELECT
  554. t1.order_id, t1.cnt
  555. FROM
  556. (SELECT
  557. order_id, COUNT(*) AS cnt
  558. FROM
  559. mobile_flow_dispatch_rec
  560. WHERE
  561. trans_id = 104 AND
  562. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  563. GROUP BY
  564. order_id
  565. ORDER BY
  566. cnt ASC) t1 LEFT JOIN flow_order_info
  567. ON
  568. t1.order_id = flow_order_info.order_id
  569. WHERE
  570. flow_order_info.channel_id = 104 AND
  571. flow_order_info.status = 6 )t2; """
  572. #兆蓉移动WT通道成功次数统计
  573. zr_sql = """SELECT
  574. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  575. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  576. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  577. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  578. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  579. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  580. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  581. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  582. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  583. COUNT(*) AS 'H[总订单数]'
  584. FROM
  585. ( SELECT
  586. t1.order_id, t1.cnt
  587. FROM
  588. (SELECT
  589. order_id, COUNT(*) AS cnt
  590. FROM
  591. mobile_flow_dispatch_rec
  592. WHERE
  593. trans_id = 95 AND
  594. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  595. GROUP BY
  596. order_id
  597. ORDER BY
  598. cnt ASC) t1 LEFT JOIN flow_order_info
  599. ON
  600. t1.order_id = flow_order_info.order_id
  601. WHERE
  602. flow_order_info.channel_id = 95 AND
  603. flow_order_info.status = 6 )t2; """
  604. #兆蓉联通WT通道成功次数统计
  605. zr_uni_sql = """SELECT
  606. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  607. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  608. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  609. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  610. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  611. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  612. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  613. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  614. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  615. COUNT(*) AS 'H[总订单数]'
  616. FROM
  617. ( SELECT
  618. t1.order_id, t1.cnt
  619. FROM
  620. (SELECT
  621. order_id, COUNT(*) AS cnt
  622. FROM
  623. mobile_flow_dispatch_rec
  624. WHERE
  625. trans_id = 116 AND
  626. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  627. GROUP BY
  628. order_id
  629. ORDER BY
  630. cnt ASC) t1 LEFT JOIN flow_order_info
  631. ON
  632. t1.order_id = flow_order_info.order_id
  633. WHERE
  634. flow_order_info.channel_id = 116 AND
  635. flow_order_info.status = 6 )t2; """
  636. #兆蓉电信WT通道成功次数统计
  637. zr_tel_sql = """SELECT
  638. SUM(CASE WHEN cnt BETWEEN 0 AND 5 THEN 1 ELSE 0 END) AS 'A[1-5]',
  639. SUM(CASE WHEN cnt BETWEEN 6 AND 10 THEN 1 ELSE 0 END) AS 'A[6-10]',
  640. SUM(CASE WHEN cnt BETWEEN 11 AND 15 THEN 1 ELSE 0 END) AS 'A[11-15]',
  641. SUM(CASE WHEN cnt BETWEEN 16 AND 20 THEN 1 ELSE 0 END) AS 'B[16-20]',
  642. SUM(CASE WHEN cnt BETWEEN 21 AND 30 THEN 1 ELSE 0 END) AS 'C[21-30]',
  643. SUM(CASE WHEN cnt BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS 'D[31-40]',
  644. SUM(CASE WHEN cnt BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS 'E[41-50]',
  645. SUM(CASE WHEN cnt BETWEEN 51 AND 60 THEN 1 ELSE 0 END) AS 'F[51-60]',
  646. SUM(CASE WHEN cnt > 61 THEN 1 ELSE 0 END) AS 'G[>60]',
  647. COUNT(*) AS 'H[总订单数]'
  648. FROM
  649. ( SELECT
  650. t1.order_id, t1.cnt
  651. FROM
  652. (SELECT
  653. order_id, COUNT(*) AS cnt
  654. FROM
  655. mobile_flow_dispatch_rec
  656. WHERE
  657. trans_id = 105 AND
  658. create_date BETWEEN ' """ + yesterdayStart + """ ' AND ' """ + yesterdayEnd + """ '
  659. GROUP BY
  660. order_id
  661. ORDER BY
  662. cnt ASC) t1 LEFT JOIN flow_order_info
  663. ON
  664. t1.order_id = flow_order_info.order_id
  665. WHERE
  666. flow_order_info.channel_id = 105 AND
  667. flow_order_info.status = 6 )t2; """
  668. #表格信息
  669. file_sheet = '通道总数'
  670. file_name = '通道数据' + str(yesterday) + '.xls'
  671. fLocate = r'/home/dukun/work/passData/' + file_name
  672. #邮件信息
  673. #sender = 'd2759360205@163.com' #发件人邮箱
  674. sender = 'youzixifeng@bluefire.top'
  675. #收件人邮箱,可以多个(列表形式)群发
  676. receiver = ['fengfei@bluefire.top','steven@bluefire.top','skyzyk@bluefire.top','miaomiao@bluefire.top']
  677. mail_user = 'youzixifeng@bluefire.top' #发件人姓名
  678. #mail_pass = 'YCHFLABGLFGSLSJZ' #smtp授权163
  679. mail_pass = 'Dukun864633403'
  680. mail_host = 'smtp.exmail.qq.com' #邮箱服务器
  681. subject = "通道数据统计" #邮件标题
  682. content = ' ' #邮件正文 #邮件正文
  683. #__file_____
  684. #file_subject='Gave', 'you', 'a', 'piece', 'of', 'shit.' #sheet标题
  685. style1,style2,style3 = setStyle()
  686. writeToExcel(fLocate,file_sheet,style1,style2,style3)
  687. sendMail(sender,receiver,mail_user,mail_pass,mail_host,subject,content,fLocate,file_name)
  688. print(fLocate)
  689. """
  690. while True:
  691. now = datetime.datetime.now()
  692. if now.hour == 8 and now.minute == 46:
  693. style = setStyle()
  694. writeToExcel(fLocate,file_sheet,style)
  695. sendMail(sender,receiver,mail_user,mail_pass,mail_host,subject,content,fLocate,file_name)
  696. #每隔60秒检测一次
  697. time.sleep(60)
  698. """