Example #1
0
    def statFood(self, params):
        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 date')
            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,3000)


            tmp_model_stat_food.updated_time = getCurrentDate()
            db.session.add(tmp_model_stat_food)
            db.session.commit()

        return True
Example #2
0
    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