def update_models_use_webapp_user(current_webapp_user, expired_webapp_user): if current_webapp_user is None and expired_webapp_user is None: return None from mall.models import Order if current_webapp_user.webapp_id == expired_webapp_user.webapp_id: Order.by_webapp_user_id(expired_webapp_user.id).filter( webapp_id=current_webapp_user.webapp_id).update( webapp_user_id=current_webapp_user.id)
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)
def order_list(request, cur_page=1, template="admin/mall/order/list.tpl"): count = Order.get_count() if count != 0: page = int(cur_page) order_list = Order.get_list(page, config.default_page_size) p = Paginator(page, count, page_size=config.default_page_size) if recharge_list: return render_response(template, request=request, order_list=order_list, p=p) return render_response(template, request=None, recharge_list=None, p=None)
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)
def get_purchased_fans(follow_members): count = 0 for follow_member in follow_members: user_orders = Order.get_orders_from_webapp_user_ids( follow_member.get_webapp_user_ids) if user_orders and user_orders.filter(status=5).count() > 0: count += 1 return count
def __get_member_orders(member): if member is None: return None webapp_user = WebAppUser.from_member(member) if webapp_user is None: notify_message = u"获取会员对应webappuser失败,member id:{}".format(member.id) watchdog_error(notify_message) return None return Order.by_webapp_user_id(webapp_user.id).order_by("-created_at")
def _is_buyed(member): webapp_user = WebAppUser.from_member(member) #这种方式无法正确的判断是否购买过, 如果数据被删除则积分计算会出现问题 by bert # if webapp_user: # return webapp_user.has_purchased # else: # return False if webapp_user: return True if Order.by_webapp_user_id(webapp_user.id).filter( status__gte=2).count() > 0 else False else: return False
def api_get(request): webapp_id = request.user_profile.webapp_id member_id = request.GET.get('id', None) cur_page = int(request.GET.get('page', '1')) count = int(request.GET.get('count_per_page', COUNT_PER_PAGE)) orders = [] if member_id: member = Member.objects.get(id=member_id, webapp_id=webapp_id) webapp_user_ids = member.get_webapp_user_ids orders = Order.by_webapp_user_id(webapp_user_ids).order_by( "-created_at", "-id") pay_money = 0 orders_valid = orders.filter(status=ORDER_STATUS_SUCCESSED) for order in orders_valid: order_final_price = order.final_price + order.weizoom_card_money pay_money += order_final_price total_count = orders.count() pageinfo, orders = paginator.paginate(orders, cur_page, count) items = [] for order in orders: items.append({ "id": order.id, "order_id": order.order_id, "final_price": float('%.2f' % (order.final_price + order.weizoom_card_money)), "created_at": datetime.strftime(order.created_at, '%Y-%m-%d %H:%M:%S'), "order_status": order.status, }) response = create_response(200) response.data = { 'items': items, 'pageinfo': paginator.to_dict(pageinfo), 'pay_money': '%.2f' % pay_money, } return response.get_response()
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
def _is_buyed(member): return True if Order.by_webapp_user_id(member.get_webapp_user_ids).filter( status__gte=2).count() > 0 else False
def get_member_follow_relations(request): member_id = request.GET.get('member_id', 0) only_fans = request.GET.get('only_fans', 'false') data_value = request.GET.get('data_value', None) sort_attr = request.GET.get('sort_attr', '-id') if only_fans == 'true': only_fans = '1' else: only_fans = '0' if data_value: if data_value == 'shared': follow_members = MemberFollowRelation.get_follow_members_for_shred_url(member_id) elif data_value == 'qrcode': follow_members= MemberFollowRelation.get_follow_members_for(member_id, '1', True) else: follow_members = [] else: follow_members = MemberFollowRelation.get_follow_members_for(member_id, only_fans) #增加计算follow_members的人数、下单人数、成交金额 population = len(follow_members) population_order = 0 for follow_member in follow_members: user_orders = Order.get_orders_from_webapp_user_ids(follow_member.get_webapp_user_ids) if user_orders: population_order += 1 #成交金额 amount = 0 for follow_member in follow_members: amount += follow_member.pay_money #增加计算follow_members的人数、下单人数、成交金额 #进行排序 follow_members = follow_members.order_by(sort_attr) if data_value: filter_date_follow_members = follow_members else: filter_date_follow_members = [] #进行分页 count_per_page = int(request.GET.get('count_per_page', 8)) cur_page = int(request.GET.get('page', '1')) pageinfo, follow_members = paginator.paginate(follow_members, cur_page, count_per_page, query_string=request.META['QUERY_STRING']) return_follow_members_json_array = [] if data_value: follow_members = filter_date_follow_members for follow_member in follow_members: return_follow_members_json_array.append(__build_follow_member_basic_json(follow_member, member_id)) response = create_response(200) response.data = { 'items': return_follow_members_json_array, 'pageinfo': paginator.to_dict(pageinfo), 'only_fans':only_fans, 'sortAttr': request.GET.get('sort_attr', '-created_at'), 'population': population, 'population_order': population_order, 'amount': '%.2f' % amount } return response.get_response()
def get_datas(request): owner_id = request.user_profile.user_id member_name = request.GET.get('member_name', '') grade_id = request.GET.get('grade_id', '') coupon_status = request.GET.get('coupon_status', '') red_envelope_rule_id = request.GET.get('id',0) receive_method = request.GET.get('receive_method','') #引入 introduced_by = request.GET.get('introduced_by',0) relation_id = request.GET.get('relation_id',0) rule_id = request.GET.get('rule_id',0) #导出 is_export = request.GET.get('is_export',0) selected_ids = request.GET.get('selected_ids',0) #排序 sort_attr = request.GET.get('sort_attr', '-created_at') _update_member_bring_new_member_count(red_envelope_rule_id) if is_export: if selected_ids: selected_ids = selected_ids.split(",") relations = promotion_models.RedEnvelopeParticipences.objects.filter( owner_id = owner_id, red_envelope_rule_id=red_envelope_rule_id, red_envelope_relation_id__in=selected_ids, introduced_by=0 ) else: relations = promotion_models.RedEnvelopeParticipences.objects.filter( owner_id = owner_id, red_envelope_rule_id=red_envelope_rule_id, introduced_by=0 ) else: if introduced_by: relations = promotion_models.RedEnvelopeParticipences.objects.filter( owner_id = owner_id, red_envelope_rule_id=rule_id, red_envelope_relation_id=relation_id, introduced_by=introduced_by ) else: relations = promotion_models.RedEnvelopeParticipences.objects.filter( owner_id = owner_id, red_envelope_rule_id=red_envelope_rule_id, introduced_by=0 ) all_member_ids = [relation.member_id for relation in relations] all_members = member_models.Member.objects.filter(id__in=all_member_ids) #筛选会员 if member_name: hexstr = byte_to_hex(member_name) members = all_members.filter(username_hexstr__contains=hexstr) all_members = members elif grade_id: members = all_members.filter(grade_id=grade_id) else: members = all_members member_ids = [] member_id2member = {} for member in members: member_ids.append(member.id) member_id2member[member.id] = member #优惠券查找 relations = relations.filter(member_id__in=member_ids) if coupon_status: final_relations_ids = [] for relation in relations: if str(coupon_status) == '1': if relation.coupon.status == int(coupon_status): final_relations_ids.append(relation.id) else: if relation.coupon.status !=1: final_relations_ids.append(relation.id) relations = relations.filter(id__in=final_relations_ids) #处理排序,需要放在分页之前 relations = relations.order_by(sort_attr) if not is_export: #进行分页 count_per_page = int(request.GET.get('count_per_page', COUNT_PER_PAGE)) cur_page = int(request.GET.get('page', '1')) pageinfo, relations = paginator.paginate(relations, cur_page, count_per_page, query_string=request.META['QUERY_STRING']) # 获取被使用的优惠券对应订单信息 coupon_ids = [c.coupon_id for c in relations if c.coupon.status == COUPON_STATUS_USED] orders = Order.get_orders_by_coupon_ids(coupon_ids) if orders: coupon_id2order_id = dict([(o.coupon_id, o.id) for o in orders]) else: coupon_id2order_id = {} member_ids = [] for sub_relation in relations: member_ids.append(sub_relation.member_id) #获取关注关系l member_follow_relation = MemberFollowRelation.objects.filter(member_id__in=member_ids) #只保存新会员的最后一条记录l member_id2follower_member_id ={} for member in member_follow_relation: member_id2follower_member_id[member.member_id] = member.follower_member_id members = Member.objects.filter(id__in=member_ids) member_id2member = dict([(m.id, m) for m in members]) grade_ids = [m.grade_id for m in members] grades = MemberGrade.objects.filter(id__in=grade_ids) grade_id2grade = dict([(g.id, g.name) for g in grades]) items = [] for relation in relations: cur_member = member_id2member[relation.member_id] cur_grade_id = cur_member.grade_id if receive_method == 'True': if cur_member.is_subscribed and cur_grade_id: grade = grade_id2grade[cur_grade_id] else: grade = u"会员" else: if cur_member.is_subscribed: if relation.is_new: if member_id2follower_member_id[relation.member_id] == relation.introduced_by: grade = u"新会员" else: grade = u"非会员" else: grade = grade_id2grade[cur_grade_id] else: grade = u"非会员" try: name = cur_member.username.decode('utf8') except: name = cur_member.username_hexstr items.append({ 'id': relation.red_envelope_relation_id, 'member_id': relation.member_id, 'username': cur_member.username_for_title, 'name': name, 'username_truncated': cur_member.username_truncated, 'participant_icon': relation.member.user_icon, 'introduce_received_number_count': relation.introduce_received_number, 'introduce_new_member_count': relation.introduce_new_member, 'introduce_used_number_count': relation.introduce_used_number, 'introduce_sales_number': '%.2f' % relation.introduce_sales_number, 'created_at': relation.created_at.strftime("%Y-%m-%d"), 'coupon_status': relation.coupon.status, 'coupon_status_name': COUPONSTATUS[relation.coupon.status]['name'], 'order_id': coupon_id2order_id[relation.coupon_id] if coupon_id2order_id.has_key(relation.coupon_id) else '', 'grade': grade, 'receive_method': receive_method }) if is_export: return items else: return pageinfo, items
def get_channel_qrcode_stats(setting_id): """ 渠道扫码的分析结果 """ relations = ChannelQrcodeHasMember.objects.filter( channel_qrcode=setting_id, is_new=True) member_num = relations.count() member_with_order_num = 0 order_num = 0 order_amount = 0.0 repurchase_member_num = 0 repurchase_order_num = 0 repurchase_order_amount = 0.0 for relation in relations: orders = Order.by_webapp_user_id( relation.member.get_webapp_user_ids).filter( status__in=QUALIFIED_ORDER_STATUS).order_by('created_at') order_count = orders.count() if order_count > 0: total_paid_amount = 0.0 first_paid_amount = None for order in orders: current_paid_amount = float(order.final_price) + float( order.weizoom_card_money) total_paid_amount += current_paid_amount if first_paid_amount == None: first_paid_amount = current_paid_amount order_amount += total_paid_amount member_with_order_num += 1 order_num += order_count if order_count >= 2: repurchase_member_num += 1 # 复购订单统计不计第一张订单 repurchase_order_num += order_count - 1 repurchase_order_amount += total_paid_amount - first_paid_amount if member_num > 0: transform_ratio = "%.2f%%" % float( float(member_with_order_num) / float(member_num) * 100) else: transform_ratio = '0.00%' # 被推荐用户数 stats = [{ "name": "被推荐用户数", "value": member_num }, { "name": "被推荐用户下单人数", "value": member_with_order_num }, { "name": "被推荐用户下单单数", "value": order_num }, { "name": "被推荐用户下单金额", "value": '%.2f' % order_amount }, { "name": "推荐扫码下单转换率", "value": transform_ratio }, { "name": "复购用户数", "value": repurchase_member_num }, { "name": "复购订单数", "value": repurchase_order_num }, { "name": "复购总金额", "value": '%.2f' % repurchase_order_amount }] return stats
def auto_update_grade(webapp_user_id=None, member=None, delete=False, **kwargs): """ :param webapp_user_id: :param member: :param delete: :param kwargs: :return:是否改变了等级 """ is_change = False if webapp_user_id: member = WebAppUser.get_member_by_webapp_user_id(webapp_user_id) if not isinstance(member, Member): return if not member: return False if not member.grade.is_auto_upgrade and not delete: return is_change webapp_id = member.webapp_id webapp_owner_id = webapp_models.WebApp.objects.get( appid=webapp_id).owner_id webapp_user_ids = member.get_webapp_user_ids # 获取会员数据 paid_orders = Order.by_webapp_user_id(webapp_user_ids).filter( status=mall_models.ORDER_STATUS_SUCCESSED, origin_order_id__lte=0) pay_times = paid_orders.count() bound = member.experience pay_money = 0 for order in paid_orders: pay_money += order.get_final_price( webapp_id) + order.weizoom_card_money if delete: grades_list = MemberGrade.objects.filter( webapp_id=webapp_id, is_auto_upgrade=True).exclude(id=member.grade_id).order_by('-id') else: grades_list = MemberGrade.objects.filter( webapp_id=webapp_id, is_auto_upgrade=True, id__gt=member.grade_id).order_by('-id') from cache.webapp_owner_cache import get_webapp_owner_info # 此处import写在文件头会报错 is_all_conditions = get_webapp_owner_info( webapp_owner_id).integral_strategy_settings.is_all_conditions # 计算条件 if is_all_conditions: for grade in grades_list: # if pay_money >= grade.pay_money and pay_times >= grade.pay_times and bound >= grade.upgrade_lower_bound: if pay_money >= grade.pay_money and pay_times >= grade.pay_times: is_change = True new_grade = grade break else: for grade in grades_list: # if pay_money >= grade.pay_money or pay_times >= grade.pay_times or bound >= grade.upgrade_lower_bound: if pay_money >= grade.pay_money or pay_times >= grade.pay_times: is_change = True new_grade = grade break if is_change: Member.objects.filter(id=member.id).update(grade=new_grade) return is_change
def get_member_orders(member): if member is None: return None webapp_user_ids = member.get_webapp_user_ids return Order.by_webapp_user_id(webapp_user_ids).order_by("-created_at")
def api_get(request): low_date, high_date, date_range = stats_util.get_date_range(request) try: webapp_id = request.user_profile.webapp_id date_list = [ date.strftime("%Y-%m-%d") for date in dateutil.get_date_range_list(low_date, high_date) ] date2count = dict() date2price = dict() # 11.20从查询mall_purchase_daily_statistics变更为直接统计订单表,解决mall_purchase_daily_statistics遗漏统计订单与统计时间不一样导致的统计结果不同的问题。 orders = Order.objects.belong_to(webapp_id).filter( created_at__range=(low_date, (high_date + timedelta(days=1)))) statuses = set([ ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED ]) orders = [ order for order in orders if (order.type != 'test') and (order.status in statuses) ] for order in orders: # date = dateutil.normalize_date(order.created_at) date = order.created_at.strftime("%Y-%m-%d") if order.webapp_id != webapp_id: order_price = Order.get_order_has_price_number( order) + order.postage else: order_price = order.final_price + order.weizoom_card_money if date in date2count: old_count = date2count[date] date2count[date] = old_count + 1 else: date2count[date] = 1 if date in date2price: old_price = date2price[date] date2price[date] = old_price + order_price else: date2price[date] = order_price price_trend_values = [] #当最后一天是今天时,折线图中不显示最后一天的数据 duhao 2015-08-12 #当起止日期都是今天时,数据正常显示 today = dateutil.get_today() if len(date_list) > 1 and date_list[-1] == today: del date_list[-1] for date in date_list: price_trend_values.append("%.2f" % (date2price.get(date, 0.0))) return create_line_chart_response('', '', date_list, [{ "name": "销售额", "values": price_trend_values }]) except: if settings.DEBUG: raise else: response = create_response(500) response.innerErrMsg = unicode_full_stack() return response.get_response()