def get_transaction_orders(webapp_id, low_date, high_date): orders = belong_to(webapp_id) transaction_orders = orders.filter( Q(created_at__range=(low_date, high_date)), Q(status__in=(ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED))) return transaction_orders
def get_order_count(webapp_id, low_date, high_date): """ 获取下单单量 """ orders = belong_to(webapp_id) order_count = orders.filter( Q(status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED)), Q(created_at__range=(low_date, high_date))).count() return order_count
def get_buyer_count(webapp_id, low_date, high_date): """ 获取购买总人数,包括会员、已取消关注的会员和非会员 """ orders = belong_to(webapp_id) #使用授权机制后,webapp_user_id对应唯一一个member_id了,不会再有多个webapp_user_idduiying buyer_count = orders.filter( Q(status__in=(ORDER_STATUS_PAYED_SUCCESSED, ORDER_STATUS_PAYED_NOT_SHIP, ORDER_STATUS_PAYED_SHIPED, ORDER_STATUS_SUCCESSED)), Q(created_at__range=( low_date, high_date))).values('webapp_user_id').distinct().count() return buyer_count
def get_deal_product_count(webapp_id, low_date, high_date): """ 获取总成交件数 """ orders = belong_to(webapp_id).filter( 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)) order_ids = [order.id for order in orders] products = OrderHasProduct.objects.filter(order_id__in=order_ids) deal_product_count = 0 for product in products: deal_product_count += product.number return deal_product_count
def get_top10_product(webapp_id, low_date, high_date): """ 获取下单单量排行前10的商品 """ orders = belong_to(webapp_id).filter( 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)) order_ids = [order.id for order in orders] products = OrderHasProduct.objects.filter(order_id__in=order_ids) product_id2num = {} for product in products: if not product_id2num.has_key(product.product_id): product_id2num[product.product_id] = 0 product_id2num[product.product_id] += 1 #按下单单量倒序 sorted_product_id2num = sorted(product_id2num.items(), key=lambda d: d[1], reverse=True) top10_product_list = [] i = 1 for item in sorted_product_id2num: product_id = item[0] product_name = '' try: product = Product.objects.get(id=product_id) product_name = product.name except: pass num = item[1] top10_product_list.append({ 'rank': i, 'product_id': product_id, 'product_name': product_name, 'num': num }) i += 1 if i > 10: break return top10_product_list
def check_weizoom_card(name, password, webapp_user=None, member=None, owner_id=None, webapp_id=None): msg = None weizoom_card = {} weizoom_card = WeizoomCard.objects.filter(weizoom_card_id=name, password=password) if len(weizoom_card) == 1: weizoom_card = weizoom_card[0] weizoom_card_rule = WeizoomCardRule.objects.get( id=weizoom_card.weizoom_card_rule_id) rule_id = weizoom_card.weizoom_card_rule.id # print rule_id, owner_id today = datetime.today() if weizoom_card.is_expired: msg = u'微众卡已过期' elif weizoom_card.expired_time < today: weizoom_card.is_expired = True weizoom_card.save() msg = u'微众卡已过期' elif weizoom_card.status == WEIZOOM_CARD_STATUS_INACTIVE: msg = u'微众卡未激活' elif owner_id and weizoom_card_rule.card_attr: #专属卡 #是否为新会员专属卡 #多专属商家id shop_limit_list = str(weizoom_card_rule.shop_limit_list).split(',') #多黑名单商家id shop_black_list = str(weizoom_card_rule.shop_black_list).split(',') # userprofiles = UserProfile.objects.filter(user_id__in=belong_to_owner_ids) # store_name_list = [] # for profile in userprofiles: # if profile.store_name: # store_name_list.append(profile.store_name) ##多商家店铺名称 # store_name_str = ','.join(store_name_list) if weizoom_card_rule.is_new_member_special: if member and member.is_subscribed: orders = belong_to(webapp_id) orders = orders.filter( webapp_id=webapp_id, webapp_user_id=webapp_user.id).exclude( status=ORDER_STATUS_CANCEL) has_order = orders.count() > 0 #判断是否首次下单 if has_order: order_ids = [order.order_id for order in orders] #不是首次下单,判断该卡是否用过 has_use_card = WeizoomCardHasOrder.objects.filter( card_id=weizoom_card.id, order_id__in=order_ids).count() > 0 if not has_use_card: msg = u'该卡为新会员专属卡' if str(owner_id) in shop_limit_list: if str(owner_id) in shop_black_list: msg = u'该卡不能在此商家使用' else: msg = u'该专属卡不能在此商家使用' else: if str(owner_id) in shop_black_list: msg = u'该卡不能在此商家使用' else: msg = u'该卡为新会员专属卡' else: if str(owner_id) in shop_limit_list: if str(owner_id) in shop_black_list: msg = u'该卡不能在此商家使用' else: msg = u'该专属卡不能在此商家使用' elif owner_id and not weizoom_card_rule.card_attr: #不是专属卡,但有黑名单 shop_black_list = str(weizoom_card_rule.shop_black_list).split(',') if str(owner_id) in shop_black_list: msg = u'该卡不能在此商家使用' elif owner_id and rule_id in [23, 36] and owner_id != 157: WeizoomCardRule.objects.get(id=rule_id) if '吉祥大药房' in weizoom_card.weizoom_card_rule.name: msg = u'抱歉,该卡仅可在吉祥大药房微站使用!' elif owner_id and rule_id in [ 99, ] and owner_id != 474: WeizoomCardRule.objects.get(id=rule_id) if '爱伲' in weizoom_card.weizoom_card_rule.name: msg = u'抱歉,该卡仅可在爱伲咖啡微站使用!' # else: # WeizoomCardUsedAuthKey.objects.get_or_create(weizoom_card_id=weizoom_card.id, auth_key=request.COOKIES[core_setting.WEIZOOM_CARD_AUTH_KEY]) else: msg = u'卡号或密码错误' return msg, weizoom_card
def api_get(request): """ 获取订单概况数据 """ # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( #created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) order_num = qualified_orders.count() # 【成交金额】=∑订单.实付金额 paid_amount = 0.0 # 【成交商品】=∑订单.商品件数 product_num = 0 # 【优惠抵扣】=∑订单.积分抵扣金额 + +∑订单.优惠券抵扣金额 discount_amount = 0.0 for order in qualified_orders: # debug # order_id_str += order.order_id + "\n" if order.origin_order_id > 0: #商户从微众自营商城同步的子订单需要计算采购价 tmp_paid_amount = order.total_purchase_price else: tmp_paid_amount = order.final_price + order.weizoom_card_money paid_amount += tmp_paid_amount for r in order.orderhasproduct_set.all(): product_num += r.number discount_amount += float(order.integral_money) + float( order.coupon_money) #postage_amount += float(order.postage) item = { # 【成交订单】=∑订单.个数 'order_num': order_num, # 【成交金额】=∑订单.实付金额 'paid_amount': paid_amount, # 【成交商品】=∑订单.商品件数 'product_num': product_num, # 【优惠抵扣】=∑订单.积分抵扣金额 + +∑订单.优惠券抵扣金额 'discount_amount': discount_amount } if start_time and end_time: response = create_response(200) response.data = {'items': item, 'sortAttr': ''} return response.get_response() else: response = create_response(500) response.errMsg = u'未指定查询时间段' return response.get_response()
def api_get(request): # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( # created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) # 优惠抵扣统计 discount_stats = { # 订单总量:=∑订单.个数 'discount_order_num': 0, # 微众卡支付:=∑订单.个数 'wezoom_num': 0, # 微众卡支付金额:=∑订单.微众卡支付金额 'wezoom_amount': 0.0, # 积分抵扣:=∑订单.个数 'integral_num': 0, # 积分抵扣金额:=∑订单.积分抵扣金额 'integral_amount': 0.0, # 优惠券:=∑订单.个数 'coupon_num': 0, # 优惠券金额:=∑订单.优惠券金额 'coupon_amount': 0.0, # 微众卡+积分:=∑订单.个数 'wezoom_integral_num': 0, # (微众卡+积分)金额:=∑订单.(微众卡+积分)金额 'wezoom_integral_amount': 0.0, # 微众卡+优惠券:=∑订单.个数 'wezoom_coupon_num': 0, # (微众卡+优惠券)金额:=∑订单.(微众卡+优惠券)金额 'wezoom_coupon_amount': 0.0, # 积分+优惠券:=∑订单.个数 # 'integral_coupon_num': 0, # (积分+优惠券)金额:=∑订单.(积分+优惠券)金额 # 'integral_coupon_amount': 0.0, # 微众卡+积分+优惠券:=∑订单.个数 #'wezoom_integral_coupon_num': 0, # (微众卡+积分+优惠券)金额:=∑订单.(微众卡+积分+优惠券)金额 #'wezoom_integral_coupon_amount': 0.0 } for order in qualified_orders: _do_discount_stats(discount_stats, order) discount_stats['discount_order_num'] = discount_stats[ 'wezoom_num'] + discount_stats['coupon_num'] + discount_stats[ 'integral_num'] + discount_stats[ 'wezoom_coupon_num'] + discount_stats[ 'wezoom_integral_num'] response = create_response(200) response.data = {'discount_stats': discount_stats, 'sortAttr': ''} return response.get_response()
def api_get(request): # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( # created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) # 【优惠抵扣】=∑订单.积分抵扣金额 + +∑订单.优惠券抵扣金额 discount_amount = 0.0 # 【总运费】=∑订单.运费金额 postage_amount = 0.0 # 【在线付款订单】=∑订单.个数 online_order_num = 0 # 【在线付款订单金额】=∑订单.实付金额 online_paid_amount = 0.0 # 【货到付款订单】=∑订单.个数 cod_order_num = 0 # 【货到付款订单金额】=∑订单.实付金额 cod_amount = 0.0 # 支付宝支付金额:=∑订单.支付宝支付金额 alipay_amount = 0.0 # 微信支付金额:=∑订单.微信支付金额 weixinpay_amount = 0.0 # 微众卡支付金额:=∑订单.微众卡支付金额 wezoom_card_amount = 0.0 # 复购订单数 repeated_num = 0 bestpay_amount = 0.0 #翼支付金额 kangou_amount = 0.0 #看购支付金额 for order in qualified_orders: # debug # order_id_str += order.order_id + "\n" if order.origin_order_id > 0: #商户从微众自营商城同步的子订单需要计算采购价 tmp_paid_amount = order.total_purchase_price else: tmp_paid_amount = order.final_price + order.weizoom_card_money # paid_amount += tmp_paid_amount # for r in order.orderhasproduct_set.all(): # product_num += r.number discount_amount += float(order.integral_money) + float( order.coupon_money) postage_amount += float(order.postage) if order.pay_interface_type == models.PAY_INTERFACE_COD: cod_order_num += 1 cod_amount += float(order.final_price) else: # 在线付款需求修改时去掉 # online_order_num += 1 # online_paid_amount += tmp_paid_amount if order.pay_interface_type == models.PAY_INTERFACE_ALIPAY: alipay_amount += float(order.final_price) elif order.pay_interface_type == models.PAY_INTERFACE_BEST_PAY: bestpay_amount += float(order.final_price) elif order.pay_interface_type == models.PAY_INTERFACE_KANGOU: kangou_amount += float(order.final_price) elif order.pay_interface_type == models.PAY_INTERFACE_WEIXIN_PAY: if order.origin_order_id > 0: #判断同步订单 weixinpay_amount += float(order.total_purchase_price) else: weixinpay_amount += float(order.final_price) wezoom_card_amount += float(order.weizoom_card_money) #在线付款需求修改时添加 #online_order_num = order_num - cod_order_num #online_paid_amount = paid_amount - cod_amount # 系统封装的饼图方法,由于数据次序不统一,故舍弃使用 # return create_pie_chart_response('', # { # # 'discount_amount': discount_amount, # # 'postage_amount': postage_amount, # # 'online_order_num': online_order_num, # # 'online_paid_amount': online_paid_amount, # # 'cod_order_num': cod_order_num, # '支付宝': round(alipay_amount,2), # '微信支付': round(weixinpay_amount,2), # '货到付款': round(cod_amount,2), # '微众卡支付': round(wezoom_card_amount,2), # '翼支付': round(bestpay_amount,2), # '看购支付': round(kangou_amount,2) # } # ) response = create_response(200) response.data = { "alipay_amount": round(alipay_amount, 2), "weixinpay_amount": round(weixinpay_amount, 2), "cod_amount": round(cod_amount, 2), "wezoom_card_amount": round(wezoom_card_amount, 2), "bestpay_amount": round(bestpay_amount, 2), "kangou_amount": round(kangou_amount, 2) } return response.get_response()
def api_get(request): # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( # created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) status_qualified_orders = total_orders.filter(status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) pre_status_qualified_orders = status_qualified_orders.filter( created_at__lt=start_time) past_status_qualified_orders = status_qualified_orders.filter( created_at__lt=end_time) #买家来源 buyer_source_stats = { # “直接关注购买”:=∑订单.个数 'sub_source_num': 0, # “推广扫码购买”:=∑订单.个数 'qrcode_source_num': 0, # “分享链接购买”:=∑订单.个数 'url_source_num': 0, # “其他”:=∑订单.个数 'other_source_num': 0 } webapp_user_ids = set( [order.webapp_user_id for order in past_status_qualified_orders]) webappuser2member = Member.members_from_webapp_user_ids( webapp_user_ids) for order in qualified_orders: tmp_member = webappuser2member.get(order.webapp_user_id, None) if tmp_member: if tmp_member.source == SOURCE_SELF_SUB: buyer_source_stats['sub_source_num'] += 1 elif tmp_member.source == SOURCE_MEMBER_QRCODE: buyer_source_stats['qrcode_source_num'] += 1 elif tmp_member.source == SOURCE_BY_URL: buyer_source_stats['url_source_num'] += 1 else: buyer_source_stats['other_source_num'] += 1 else: buyer_source_stats['other_source_num'] += 1 # 系统封装的饼图方法,由于数据次序不统一,故舍弃使用 # return create_pie_chart_response('', # { # "直接关注购买":buyer_source_stats['sub_source_num'], # "推广扫码购买":buyer_source_stats['qrcode_source_num'], # "分享链接购买":buyer_source_stats['url_source_num'], # "其他":buyer_source_stats['other_source_num'] # } # ) response = create_response(200) response.data = { "sub_source_num": buyer_source_stats['sub_source_num'], "qrcode_source_num": buyer_source_stats['qrcode_source_num'], "url_source_num": buyer_source_stats['url_source_num'], "other_source_num": buyer_source_stats['other_source_num'] } return response.get_response()
def api_get(request): # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date # 用于检查复购订单的辅助List wuid_dict = {'pos': 0} webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( # created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) status_qualified_orders = total_orders.filter(status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) pre_status_qualified_orders = status_qualified_orders.filter( created_at__lt=start_time) past_status_qualified_orders = status_qualified_orders.filter( created_at__lt=end_time) # 订单总量 order_num = qualified_orders.count() # 重复购买 repeated_num = 0 webapp_user_ids = set( [order.webapp_user_id for order in past_status_qualified_orders]) webappuser2member = Member.members_from_webapp_user_ids( webapp_user_ids) for order in qualified_orders: tmp_member_two = webappuser2member.get(order.webapp_user_id, None) # t0 = time.clock() # t1 = time.time() # print ">>>>>" repeated_num += _get_repeated_num_increment( order.webapp_user_id, wuid_dict, tmp_member_two, webappuser2member, pre_status_qualified_orders) # __report_performance(t0, t1, "repeat counter") # 初次购买 first_buy = order_num - repeated_num # 系统封装的饼图方法,由于数据次序不统一,故舍弃使用 # return create_pie_chart_response('', # { # "重复购买":repeated_num, # "初次购买":first_buy # } # ) response = create_response(200) response.data = {"first_buy": first_buy, "repeated_num": repeated_num} return response.get_response()
def api_get(request): # 时间区间 low_date, high_date, date_range = stats_util.get_date_range(request) start_time = low_date end_time = high_date webapp_id = request.user_profile.webapp_id total_orders = belong_to(webapp_id) qualified_orders = total_orders.prefetch_related( 'orderhasproduct_set').filter( #created_at__gte=start_time, created_at__lt=end_time, created_at__range=(start_time, end_time), status__in=[ models.ORDER_STATUS_PAYED_NOT_SHIP, models.ORDER_STATUS_PAYED_SHIPED, models.ORDER_STATUS_SUCCESSED ]) orders = qualified_orders # 待发货:已付款,未发货 order_count = orders.count() if order_count > 0: data = [(order.id, order.status) for order in orders] df = pd.DataFrame(data, columns=['id', 'status']) counts = df['status'].value_counts() # 按status的值统计频度 #not_shipped_num = orders.filter(status=models.ORDER_STATUS_PAYED_NOT_SHIP).count() not_shipped_num = int( counts.get(models.ORDER_STATUS_PAYED_NOT_SHIP, 0)) # 已发货:已付款,已发货 #shipped_num = orders.filter(status=models.ORDER_STATUS_PAYED_SHIPED).count() shipped_num = int(counts.get(models.ORDER_STATUS_PAYED_SHIPED, 0)) # 已完成:自下单10日后自动置为已完成状态 #succeeded_num = orders.filter(status=models.ORDER_STATUS_SUCCESSED).count() succeeded_num = int(counts.get(models.ORDER_STATUS_SUCCESSED, 0)) else: not_shipped_num = 0 # 已发货:已付款,已发货 shipped_num = 0 # 已完成:自下单10日后自动置为已完成状态 succeeded_num = 0 # 系统封装的饼图方法,由于数据次序不统一,故舍弃使用 # return create_pie_chart_response('', # { # "待发货":not_shipped_num, # "已发货":shipped_num, # "已完成":succeeded_num # } # ) response = create_response(200) response.data = { "not_shipped_num": not_shipped_num, "shipped_num": shipped_num, "succeeded_num": succeeded_num } return response.get_response()
def inner_func(): count = belong_to(webapp_id).filter(status=ORDER_STATUS_PAYED_NOT_SHIP).count() return { 'keys': [key], 'value': count }