db_order_util.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. connect = MySQLConnection()
  32. try:
  33. num = connect.batch(sql, data)
  34. # 提交
  35. connect.commit()
  36. print('订单数据最终入库【{num}】条'.format(num=num))
  37. except Exception as e:
  38. print('订单数据入库失败:', e)
  39. finally:
  40. connect.close()
  41. # 获取各平台的订单数量
  42. def get_platform_order_count(date):
  43. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order` WHERE date = %s GROUP BY platform'
  44. connect = MySQLConnection()
  45. platform_order_count = []
  46. try:
  47. platform_order_count = connect.query(sql, date)
  48. except Exception as e:
  49. print('各平台的订单数据查询失败:', e)
  50. finally:
  51. connect.close()
  52. return platform_order_count
  53. def get_account_info_list(platform):
  54. sql=f"select text from order_account_text where platform='{platform}'"
  55. db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com', 'superc', 'Cc719199895', 'quchen_text')
  56. cur=db.cursor()
  57. try:
  58. cur.execute(sql)
  59. platform= cur.fetchall()
  60. except Exception as e:
  61. print(e)
  62. li=[]
  63. for i in platform:
  64. li.append(i[0].split(","))
  65. return li
  66. if __name__ == '__main__':
  67. get_account_info_list("掌中云")