def statFood(self, params): # Food统计 act = params['act'] date = params['date'] date_from = params['date_from'] date_to = params['date_to'] app.logger.info( "act:{0},from:{1},to:{2}".format( act, date_from, date_to)) stat_food_list = db.session.query( FoodSaleChangeLog.food_id, func.sum( FoodSaleChangeLog.quantity).label("total_count"), func.sum( FoodSaleChangeLog.price).label("total_pay_money")).filter( FoodSaleChangeLog.created_time >= date_from, FoodSaleChangeLog.created_time <= date_to).group_by( FoodSaleChangeLog.food_id).all() if not stat_food_list: app.logger.info("no data") return for item in stat_food_list: tmp_food_id = item[0] tmp_stat_food = StatDailyFood.query.filter_by( date=date, food_id=tmp_food_id).first() if tmp_stat_food: tmp_model_stat_food = tmp_stat_food else: tmp_model_stat_food = StatDailyFood() tmp_model_stat_food.date = date tmp_model_stat_food.food_id = tmp_food_id tmp_model_stat_food.created_time = get_current_time() tmp_model_stat_food.total_count = item[1] tmp_model_stat_food.total_pay_money = item[2] tmp_model_stat_food.updated_time = get_current_time() # 为了测试效果模拟数据 tmp_model_stat_food.total_count = random.randint(50, 100) tmp_model_stat_food.total_pay_money = random.randint(1000, 1010) db.session.add(tmp_model_stat_food) db.session.commit() return
def statFood(self, params): act = params['act'] date = params['date'] date_from = params['date_from'] date_to = params['date_to'] stat_food_list = db.session.query(FoodSaleChangeLog.food_id, func.sum(FoodSaleChangeLog.quantity).label('total_count'),\ func.sum(FoodSaleChangeLog.price).label("total_pay_money"))\ .filter(FoodSaleChangeLog.created_time >= date_from, FoodSaleChangeLog.created_time <= date_to)\ .group_by(FoodSaleChangeLog.food_id).all() if not stat_food_list: app.logger.info("no data") return for item in stat_food_list: tmp_food_id = item[0] tmp_stat_food = StatDailyFood.query.filter_by( date=date, food_id=tmp_food_id).first() if tmp_stat_food: tmp_model_stat_food = tmp_stat_food else: tmp_model_stat_food = StatDailyFood() tmp_model_stat_food.food_id = tmp_food_id tmp_model_stat_food.date = date tmp_model_stat_food.created_time = getCurrentDate() tmp_model_stat_food.total_count = item[1] tmp_model_stat_food.total_pay_money = item[2] tmp_model_stat_food.total_count = random.randint(50, 100) tmp_model_stat_food.total_pay_money = random.randint(1000, 1010) db.session.add(tmp_model_stat_food) db.session.commit() return True