当前位置:   article > 正文

python Flask与微信小程序 统计管理

python Flask与微信小程序 统计管理

common/models/stat/StatDailyMember.py

  1. DROP TABLE IF EXISTS `stat_daily_member`;
  2. CREATE TABLE `stat_daily_member` (
  3. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  4. `date` date NOT NULL COMMENT '日期',
  5. `member_id` int(11) NOT NULL DEFAULT '0' COMMENT '会员id',
  6. `total_shared_count` int(11) NOT NULL DEFAULT '0' COMMENT '当日分享总次数',
  7. `total_pay_money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '当日付款总金额',
  8. `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新时间',
  9. `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
  10. PRIMARY KEY (`id`),
  11. KEY `idx_date_member_id` (`date`,`member_id`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会员日统计';
flask-sqlacodegen 'mysql://root:root@127.0.0.1/food_db' --tables stat_daily_member --outfile "common/models/stat/StatDailyMember.py"  --flask

common/models/stat/StatDailySite.py

  1. DROP TABLE IF EXISTS `stat_daily_site`;
  2. CREATE TABLE `stat_daily_site` (
  3. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  4. `date` date NOT NULL COMMENT '日期',
  5. `total_pay_money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '当日应收总金额',
  6. `total_member_count` int(11) NOT NULL COMMENT '会员总数',
  7. `total_new_member_count` int(11) NOT NULL COMMENT '当日新增会员数',
  8. `total_order_count` int(11) NOT NULL COMMENT '当日订单数',
  9. `total_shared_count` int(11) NOT NULL,
  10. `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新时间',
  11. `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
  12. PRIMARY KEY (`id`),
  13. KEY `idx_date` (`date`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='全站日统计';
flask-sqlacodegen 'mysql://root:root@127.0.0.1/food_db' --tables stat_daily_site --outfile "common/models/stat/StatDailySite.py"  --flask

common/models/stat/StatDailyFood.py

  1. DROP TABLE IF EXISTS `stat_daily_food`;
  2. CREATE TABLE `stat_daily_food` (
  3. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  4. `date` date NOT NULL,
  5. `food_id` int(11) NOT NULL DEFAULT '0' COMMENT '菜品id',
  6. `total_count` int(11) NOT NULL DEFAULT '0' COMMENT '售卖总数量',
  7. `total_pay_money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总售卖金额',
  8. `updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新时间',
  9. `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
  10. PRIMARY KEY (`id`),
  11. KEY `date_food_id` (`date`,`food_id`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='书籍售卖日统计';
flask-sqlacodegen 'mysql://root:root@127.0.0.1/food_db' --tables stat_daily_food --outfile "common/models/stat/StatDailyFood.py"  --flask

jobs/tasks/stat/daily.py

  1. # -*- coding: utf-8 -*-
  2. from application import app,db
  3. from common.libs.Helper import getFormatDate,getCurrentDate
  4. from common.models.member.Member import Member
  5. from common.models.pay.PayOrder import PayOrder
  6. from common.models.stat.StatDailyFood import StatDailyFood
  7. from common.models.stat.StatDailySite import StatDailySite
  8. from common.models.stat.StatDailyMember import StatDailyMember
  9. from common.models.food.WxShareHistory import WxShareHistory
  10. from common.models.food.FoodSaleChangeLog import FoodSaleChangeLog
  11. from sqlalchemy import func
  12. import random
  13. '''
  14. python manager.py runjob -m stat/daily -a member|food|site -p 2018-07-01
  15. '''
  16. class JobTask():
  17. def __init__(self):
  18. pass
  19. def run(self, params):
  20. act = params['act'] if 'act' in params else ''
  21. date = params['param'][0] if params['param'] and len(params['param']) else getFormatDate(format="%Y-%m-%d")
  22. if not act:
  23. return
  24. date_from = date + " 00:00:00"
  25. date_to = date + " 23:59:59"
  26. func_params = {
  27. 'act': act,
  28. 'date':date,
  29. 'date_from':date_from,
  30. 'date_to':date_to
  31. }
  32. if act == "member":
  33. self.statMember( func_params )
  34. elif act == "food":
  35. self.statFood( func_params )
  36. elif act == "site":
  37. self.statSite( func_params)
  38. elif act == "test":
  39. self.test()
  40. app.logger.info("it's over~~")
  41. return
  42. '''
  43. 会员统计
  44. '''
  45. def statMember(self,params):
  46. act = params['act']
  47. date = params['date']
  48. date_from = params['date_from']
  49. date_to = params['date_to']
  50. app.logger.info( "act:{0},from:{1},to:{2}".format( act,date_from,date_to ) )
  51. member_list = Member.query.all()
  52. if not member_list:
  53. app.logger.info( "no member list" )
  54. return
  55. for member_info in member_list:
  56. tmp_stat_member = StatDailyMember.query.filter_by( date = date,member_id = member_info.id ).first()
  57. if tmp_stat_member:
  58. tmp_model_stat_member = tmp_stat_member
  59. else:
  60. tmp_model_stat_member = StatDailyMember()
  61. tmp_model_stat_member.date = date
  62. tmp_model_stat_member.member_id = member_info.id
  63. tmp_model_stat_member.created_time = getCurrentDate()
  64. tmp_stat_pay = db.session.query( func.sum(PayOrder.total_price).label("total_pay_money")) \
  65. .filter( PayOrder.member_id == member_info.id ,PayOrder.status == 1 )\
  66. .filter( PayOrder.created_time >= date_from,PayOrder.created_time <= date_to ).first()
  67. tmp_stat_share_count = WxShareHistory.query.filter( PayOrder.member_id == member_info.id )\
  68. .filter( PayOrder.created_time >= date_from,PayOrder.created_time <= date_to ).count()
  69. tmp_model_stat_member.total_shared_count = tmp_stat_share_count
  70. tmp_model_stat_member.total_pay_money = tmp_stat_pay[ 0 ] if tmp_stat_pay[ 0 ] else 0.00
  71. '''
  72. 为了测试效果模拟数据
  73. '''
  74. tmp_model_stat_member.total_shared_count = random.randint(50,100)
  75. tmp_model_stat_member.total_pay_money = random.randint(1000,1010)
  76. tmp_model_stat_member.updated_time = getCurrentDate()
  77. db.session.add( tmp_model_stat_member )
  78. db.session.commit()
  79. return
  80. '''
  81. Food统计
  82. '''
  83. def statFood(self,params):
  84. act = params['act']
  85. date = params['date']
  86. date_from = params['date_from']
  87. date_to = params['date_to']
  88. app.logger.info( "act:{0},from:{1},to:{2}".format( act,date_from,date_to ) )
  89. stat_food_list = db.session.query(FoodSaleChangeLog.food_id, func.sum(FoodSaleChangeLog.quantity).label("total_count"),
  90. func.sum(FoodSaleChangeLog.price).label("total_pay_money")) \
  91. .filter(FoodSaleChangeLog.created_time >= date_from, FoodSaleChangeLog.created_time <= date_to)\
  92. .group_by( FoodSaleChangeLog.food_id ).all()
  93. if not stat_food_list:
  94. app.logger.info("no data")
  95. return
  96. for item in stat_food_list:
  97. tmp_food_id = item[ 0 ]
  98. tmp_stat_food = StatDailyFood.query.filter_by(date=date, food_id = tmp_food_id ).first()
  99. if tmp_stat_food:
  100. tmp_model_stat_food = tmp_stat_food
  101. else:
  102. tmp_model_stat_food = StatDailyFood()
  103. tmp_model_stat_food.date = date
  104. tmp_model_stat_food.food_id = tmp_food_id
  105. tmp_model_stat_food.created_time = getCurrentDate()
  106. tmp_model_stat_food.total_count = item[1]
  107. tmp_model_stat_food.total_pay_money = item[2]
  108. tmp_model_stat_food.updated_time = getCurrentDate()
  109. '''
  110. 为了测试效果模拟数据
  111. '''
  112. tmp_model_stat_food.total_count = random.randint(50, 100)
  113. tmp_model_stat_food.total_pay_money = random.randint(1000, 1010)
  114. db.session.add( tmp_model_stat_food )
  115. db.session.commit()
  116. return
  117. '''
  118. site统计
  119. '''
  120. def statSite(self,params):
  121. act = params['act']
  122. date = params['date']
  123. date_from = params['date_from']
  124. date_to = params['date_to']
  125. app.logger.info( "act:{0},from:{1},to:{2}".format( act,date_from,date_to ) )
  126. stat_pay = db.session.query(func.sum(PayOrder.total_price).label("total_pay_money")) \
  127. .filter(PayOrder.status == 1) \
  128. .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to).first()
  129. stat_member_count = Member.query.count()
  130. stat_new_member_count = Member.query.filter(Member.created_time >= date_from,
  131. Member.created_time <= date_to).count()
  132. stat_order_count = PayOrder.query.filter_by( status = 1 )\
  133. .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to)\
  134. .count()
  135. stat_share_count = WxShareHistory.query.filter(WxShareHistory.created_time >= date_from
  136. , WxShareHistory.created_time <= date_to).count()
  137. tmp_stat_site = StatDailySite.query.filter_by(date=date).first()
  138. if tmp_stat_site:
  139. tmp_model_stat_site = tmp_stat_site
  140. else:
  141. tmp_model_stat_site = StatDailySite()
  142. tmp_model_stat_site.date = date
  143. tmp_model_stat_site.created_time = getCurrentDate()
  144. tmp_model_stat_site.total_pay_money = stat_pay[ 0 ] if stat_pay[ 0 ] else 0.00
  145. tmp_model_stat_site.total_new_member_count = stat_new_member_count
  146. tmp_model_stat_site.total_member_count = stat_member_count
  147. tmp_model_stat_site.total_order_count = stat_order_count
  148. tmp_model_stat_site.total_shared_count = stat_share_count
  149. tmp_model_stat_site.updated_time = getCurrentDate()
  150. '''
  151. 为了测试效果模拟数据
  152. '''
  153. tmp_model_stat_site.total_pay_money = random.randint(1000, 1010)
  154. tmp_model_stat_site.total_new_member_count = random.randint(50, 100)
  155. tmp_model_stat_site.total_member_count += tmp_model_stat_site.total_new_member_count
  156. tmp_model_stat_site.total_order_count = random.randint(900, 1000)
  157. tmp_model_stat_site.total_shared_count = random.randint(1000, 2000)
  158. db.session.add(tmp_model_stat_site)
  159. db.session.commit()
  160. def test(self):
  161. import datetime
  162. from common.libs.Helper import getFormatDate
  163. now = datetime.datetime.now()
  164. for i in reversed( range( 1,30 ) ):
  165. date_before = now + datetime.timedelta( days = -i )
  166. date = getFormatDate( date = date_before,format = "%Y-%m-%d" )
  167. tmp_params = {
  168. 'act': 'test',
  169. 'date': date,
  170. 'date_from': date + " 00:00:00",
  171. 'date_to': date + " 23:59:59"
  172. }
  173. self.testFood( date )
  174. self.statFood( tmp_params )
  175. self.statMember( tmp_params )
  176. self.statSite( tmp_params )
  177. def testFood(self,date):
  178. from common.models.food.Food import Food
  179. list = Food.query.all()
  180. if list:
  181. for item in list:
  182. model = FoodSaleChangeLog()
  183. model.food_id = item.id
  184. model.quantity = random.randint( 1,10 )
  185. model.price = model.quantity * item.price
  186. model.member_id = 1
  187. model.created_time = date + " " + getFormatDate( format = "%H:%M:%S")
  188. db.session.add( model )
  189. db.session.commit()

statMember(self, params)

这段代码是一个函数statMember的实现,它接受一个参数params,其中包含了actdatedate_fromdate_to等信息。函数的主要功能是统计会员信息。

首先,函数会根据传入的参数打印出actdate_fromdate_to的值。然后,它会查询数据库中的所有会员信息,并进行遍历。

在遍历过程中,函数会根据日期和会员ID查询对应的每日会员统计信息StatDailyMember。如果找到了对应的记录,则将其赋值给tmp_model_stat_member;否则,创建一个新的StatDailyMember对象,并设置其日期、会员ID和创建时间

接下来,函数会使用数据库查询语句计算该会员的总支付金额,并将结果保存在total_pay_money字段中。

最后,函数会返回统计结果。

statSite(self,params)

这段代码是一个函数statSite的定义,它接受一个参数params。函数内部首先从params中获取actdatedate_fromdate_to的值,并将它们打印出来。

接下来,函数使用SQLAlchemy进行数据库查询,统计了以下几个指标:

  1. stat_pay:计算了满足条件的支付订单的总金额。
  2. stat_member_count:统计了会员的总数量。
  3. stat_new_member_count:统计了在指定日期范围内新注册的会员数量。
  4. stat_order_count:统计了在指定日期范围内已支付的订单数量。
  5. stat_share_count:统计了在指定日期范围内的分享历史数量。

请注意,这段代码中使用了一些数据库模型(如PayOrderMemberWxShareHistory),以及一些SQLAlchemy的查询方法(如filter()count()first())来实现数据统计功能。

.count()

count()是一个用于统计字符串、列表、元组等可迭代对象中某个元素出现的次数的方法。它可以用于字符串、列表、元组等数据类型。

在字符串中,.count()方法可以统计指定字符或子字符串在字符串中出现的次数。例如:

python

  1. string = "Hello, World!"
  2. count = string.count("o")
  3. print(count) # 输出结果为2

在列表和元组中,.count()方法可以统计指定元素在列表或元组中出现的次数。例如:

python

  1. my_list = [1, 2, 3, 4, 2, 2]
  2. count = my_list.count(2)
  3. print(count) # 输出结果为3

.count()方法返回的是指定元素在可迭代对象中出现的次数。

test(self)

(imooc) [root@localhost order]# python manager.py runjob -m stat/daily -a test

加载30天的数据到数据库

这段代码是一个名为test的函数,它包含了一些操作。让我逐步解释一下:

  1. 首先,代码导入了datetime模块和getFormatDate函数。
  2. 然后,创建了一个当前时间的变量now,使用datetime.datetime.now()获取当前时间。
  3. 接下来,使用一个反向的循环,从1到29,创建一个变量date_before,表示当前时间减去i天的日期。
  4. 使用getFormatDate函数将date_before格式化为"%Y-%m-%d"的日期格式,并赋值给变量date
  5. 创建一个临时参数字典tmp_params,包含了一些键值对。
  6. 调用了self.testFood(date)函数,传入了date作为参数。
  7. 调用了self.statFood(tmp_params)函数,传入了tmp_params作为参数。
  8. 调用了self.statMember(tmp_params)函数,传入了tmp_params作为参数。
  9. 调用了self.statSite(tmp_params)函数,传入了tmp_params作为参数。

这段代码的作用是进行一系列的测试、统计操作,根据不同的日期进行相应的处理。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/122138
推荐阅读
相关标签
  

闽ICP备14008679号