db_order_util.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. # 数据导入表采用replace替换主键orderid的方法
  25. def batch_save_order(data):
  26. if data is None or len(data) == 0:
  27. print('数据为空,不执行数据库操作!')
  28. else:
  29. sql = 'INSERT IGNORE 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);'
  30. connect = MySQLConnection()
  31. try:
  32. num = connect.batch(sql, data)
  33. # 提交
  34. connect.commit()
  35. print('订单数据最终入库【{num}】条'.format(num=num))
  36. except Exception as e:
  37. print('订单数据入库失败:', e)
  38. finally:
  39. connect.close()
  40. # 获取各平台的订单数量
  41. def get_platform_order_count(date):
  42. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order` WHERE date = %s GROUP BY platform'
  43. connect = MySQLConnection()
  44. platform_order_count = []
  45. try:
  46. platform_order_count = connect.query(sql, date)
  47. except Exception as e:
  48. print('各平台的订单数据查询失败:', e)
  49. finally:
  50. connect.close()
  51. return platform_order_count