Exemplo n.º 1
0
def get_repeat_buying_member_count(webapp_id, low_date, high_date):
    """
	获取复购会员数

	检查给定date范围的订单,如果其用户在之前购买过
	"""
    #只统计来源为“本店”的购买订单
    orders = Order.by_webapp_id(webapp_id).filter(
        order_source=ORDER_SOURCE_OWN,
        created_at__range=(low_date, high_date),
        status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP,
                    ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED))
    repeat_buying_member_set = set()

    #为了优化查询效率,把所有订单按日期倒序都读出来,然后判断是否有复购订单
    all_orders = Order.by_webapp_id(webapp_id).filter(
        order_source=ORDER_SOURCE_OWN,
        #created_at__range=(low_date, high_date),
        status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP,
                    ORDER_STATUS_PAYED_SHIPED,
                    ORDER_STATUS_SUCCESSED)).order_by('-created_at')
    webapp_user_id2created_at = {}

    # 保留最早时间
    for order in all_orders:
        webapp_user_id2created_at[order.webapp_user_id] = (order.id,
                                                           order.created_at)

    for order in orders:
        if order.type == 'test':
            continue

        if not order.webapp_user_id in repeat_buying_member_set:
            if order.webapp_user_id != -1 and order.webapp_user_id != 0:
                # webapp_users = WebAppUser.objects.filter(id=order.webapp_user_id)
                # if webapp_users.count() > 0:
                # 	webapp_user = webapp_users[0]
                # member = WebAppUser.get_member_by_webapp_user_id(order.webapp_user_id)
                # if member and member.is_for_test is False:
                #判断在该笔订单前是否有有效订单
                # order_count = Order.objects.filter(
                # 				webapp_user_id=order.webapp_user_id,
                # 				webapp_id=webapp_id,
                # 				order_source=ORDER_SOURCE_OWN,
                # 				created_at__lt=order.created_at,
                # 				status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED)
                # 			).count()
                # if order_count > 0:
                # 	repeat_buying_member_set.add(order.webapp_user_id)

                (order_id, old_created_at
                 ) = webapp_user_id2created_at[order.webapp_user_id]

                if order_id != order.id and old_created_at < order.created_at:
                    repeat_buying_member_set.add(order.webapp_user_id)

    return len(repeat_buying_member_set)
Exemplo n.º 2
0
def get_bought_member_count(webapp_id, low_date, high_date):
    """
	统计下单会员数
	"""
    #这里不能像其他数据一样调用get_date2bought_member_count然后进行加和获取数据
    #要考虑会员滤重问题
    orders = Order.by_webapp_id(webapp_id).filter(
        order_source=ORDER_SOURCE_OWN,
        status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP,
                    ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED),
        created_at__range=(low_date, high_date))

    member_set = set()
    for order in orders:
        if order.type == 'test':
            continue
        if order.webapp_user_id != -1 and order.webapp_user_id != 0:
            # webapp_users = WebAppUser.objects.filter(id=order.webapp_user_id)
            # if webapp_users.count() > 0:
            # 	webapp_user = webapp_users[0]
            # member = WebAppUser.get_member_by_webapp_user_id(order.webapp_user_id)
            # if member and member.is_for_test is False:
            member_set.add(order.webapp_user_id)

    return len(member_set)
Exemplo n.º 3
0
def get_date2bought_member_count(webapp_id,
                                 low_date,
                                 high_date,
                                 date_formatter=None):
    """
	统计各个日期的下单会员数
	"""
    if not date_formatter:
        date_formatter = TYPE2FORMATTER['day']

    orders = Order.by_webapp_id(webapp_id).filter(
        order_source=ORDER_SOURCE_OWN,
        status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP,
                    ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED),
        created_at__range=(low_date, high_date))

    date2member_set = {}
    for order in orders:
        if order.type == 'test':
            continue
        if order.webapp_user_id != -1 and order.webapp_user_id != 0:
            # webapp_users = WebAppUser.objects.filter(id=order.webapp_user_id)
            # if webapp_users.count() > 0:
            # 	webapp_user = webapp_users[0]
            # 	member = WebAppUser.get_member_by_webapp_user_id(webapp_user.id)
            # 	if member and member.is_for_test is False:
            date = date_formatter(order.created_at)
            if not date2member_set.has_key(date):
                date2member_set[date] = set()
            date2member_set[date].add(order.webapp_user_id)

    date2bought_member_count = {}

    for date in date2member_set:
        date2bought_member_count[date] = len(date2member_set[date])

    return date2bought_member_count