db_order_util.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ = '订单工具类,用于查询,修改订单等'
  5. @Time : 2020/9/30 12:38
  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. from util.MySQLConnection import MySQLConnection
  24. import pymysql
  25. # 数据导入表采用replace替换主键orderid的方法
  26. def batch_save_order(data):
  27. if data is None or len(data) == 0:
  28. print('数据为空,不执行数据库操作!')
  29. else:
  30. sql = 'replace INTO quchen_text.`order` (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);'
  31. db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com', 'superc', 'Qc_123456', 'quchen_text')
  32. cur = db.cursor()
  33. try:
  34. num= cur.executemany(sql,data)
  35. # 提交
  36. db.commit()
  37. print('订单数据最终入库【{num}】条'.format(num=num))
  38. except Exception as e:
  39. print('订单数据入库失败:', e)
  40. finally:
  41. cur.close()
  42. # 获取各平台的订单数量
  43. def get_platform_order_count(date):
  44. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order` WHERE date = %s GROUP BY platform'
  45. connect = MySQLConnection()
  46. platform_order_count = []
  47. try:
  48. platform_order_count = connect.query(sql, date)
  49. except Exception as e:
  50. print('各平台的订单数据查询失败:', e)
  51. finally:
  52. connect.close()
  53. return platform_order_count
  54. def get_account_info_list(platform):
  55. sql=f"select text from order_account_text where platform='{platform}'"
  56. db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com', 'superc', 'Qc_123456', 'quchen_text')
  57. cur=db.cursor()
  58. try:
  59. cur.execute(sql)
  60. platform= cur.fetchall()
  61. except Exception as e:
  62. print(e)
  63. li=[]
  64. for i in platform:
  65. li.append(i[0].split(","))
  66. return li
  67. if __name__ == '__main__':
  68. get_account_info_list("掌中云")