def search_categories(self, filters, target_page): """ 搜索商品分组 """ pageinfo = None result_categories = None if '__f-product_name-contain' in filters: product_filters = FilterParser.get().extract_by_keys( filters, {'__f-product_name-contain': '__f-name-contain'}) max_page = PageInfo.get_max_page() products, _ = self.corp.product_pool.get_products( max_page, filters=product_filters) product_ids = [product.id for product in products] category_ids = [ relation.category_id for relation in mall_models.CategoryHasProduct.select(). dj_where(product_id__in=product_ids) ] result_categories = mall_models.ProductCategory.select().dj_where( id__in=category_ids, owner_id=self.corp.id) pageinfo, result_categories = paginator.paginate( result_categories, target_page.cur_page, target_page.count_per_page) if '__f-name-contain' in filters: filter_parse_result = FilterParser.get().parse_key( filters, '__f-name-contain') params = { "owner_id": self.corp.id, } params.update(filter_parse_result) categories = mall_models.ProductCategory.select().dj_where( **params) if result_categories: #搜索场景:同时搜索“分组名”和“分组商品名”,基本不常用 #这里分页的逻辑非常复杂,所以,这种情况暂不支持分页 category_id_set = [category.id for category in categories] result_categories = [ category for category in result_categories if category.id in category_id_set ] else: pageinfo, result_categories = paginator.paginate( categories, target_page.cur_page, target_page.count_per_page) return [ Category.from_model({'db_model': category}) for category in result_categories ], pageinfo
def get_promotions(self, page_info, fill_options=None, options=None, filters=None): type2fiters = self.__split_filters(filters=filters) order_options = self.__get_promotion_order_options(options=options) promotion_filters = type2fiters['promotion'] promotions = promotion_models.Promotion.select().dj_where( **promotion_filters) # product_filters = type2fiters['product'] # if product_filters: # products = self.corp.product_pool.search_products_by_filters(**product_filters) # product_ids = [product.id for product in products] # relations = promotion_models.ProductHasPromotion.select().dj_where(product_id__in=product_ids) # promotions = promotions.dj_where(id__in=[re.promotion_id for re in relations]) premium_sale_filters = type2fiters['premium_sale'] if premium_sale_filters: premium_sales = promotion_models.PremiumSale.select().dj_where( **premium_sale_filters) promotions = promotions.dj_where(detail_id__in=[ premium_sale.id for premium_sale in premium_sales ]) flash_sale_filters = type2fiters['flash_sale'] if flash_sale_filters: pass product_filters = type2fiters['product'] if product_filters: # 根据promotion查处所有这些促销的商品 promotion_ids = [promotion.id for promotion in promotions] relations = promotion_models.ProductHasPromotion.select().dj_where( promotion_id__in=promotion_ids) product_filters['id__in'] = [ relation.product_id for relation in relations ] # 根据商品id和其他商品条件搜索出商品 products = self.corp.product_pool.search_products_by_filters( **product_filters) product_ids = [product.id for product in products] # 根据正确的商品搜索出促销 relations = promotion_models.ProductHasPromotion.select().dj_where( product_id__in=product_ids) promotions = promotions.dj_where( id__in=[re.promotion_id for re in relations]) if order_options: promotions = promotions.order_by(*order_options) pageinfo, promotions = paginator.paginate(promotions, page_info.cur_page, page_info.count_per_page) promotions = [Promotion(promotion) for promotion in promotions] fill_promotion_detail_service = FillPromotionDetailService(self.corp) fill_promotion_detail_service.fill_detail(promotions, self.corp, fill_options) return promotions, pageinfo
def get_delivery_items(self, filters, page_info, fill_options=None): db_models = self.__search(filters) pageinfo, db_models = paginator.paginate(db_models, page_info.cur_page, page_info.count_per_page) delivery_items = DeliveryItem.from_models({ "models": db_models, 'fill_options': fill_options, 'corp': self.corp }) return pageinfo, delivery_items
def get_all_categories(self, target_page): categories = mall_models.ProductCategory.select().dj_where( owner_id=self.corp.id) pageinfo, categories = paginator.paginate(categories, target_page.cur_page, target_page.count_per_page) return [ Category.from_model({'db_model': category}) for category in categories ], pageinfo
def get_materials(self, params, target_page): db_models = self.__get_material_models(params) pageinfo, db_models = paginator.paginate(db_models, target_page.cur_page, target_page.count_per_page) materials_models = [] for model in db_models: materials_models.append(Material(model)) return pageinfo, materials_models
def get_product_categories(self, product_id, target_page=None): """ 商品所在分组 """ relations = mall_models.CategoryHasProduct.select(mall_models.CategoryHasProduct, mall_models.ProductCategory)\ .join(mall_models.ProductCategory).dj_where(product_id=product_id) # paginator.paginate(relations, target_page.cur_page, target_page.count_per_page) categories = [] for relation in relations: categories.append(Category(relation.category)) page_info, categories = paginator.paginate(categories, target_page.cur_page, target_page.count_per_page) return categories, page_info
def get_orders(self, filters, target_page, fill_options): # db_models = mall_models.Order.select().dj_where(webapp_id=webapp_id) db_models = self.__search(filters) pageinfo, db_models = paginator.paginate(db_models, target_page.cur_page, target_page.count_per_page) self.context['db_models'] = db_models orders = Order.from_models({ "db_models": db_models, 'fill_options': fill_options, 'corp': self.corp }) return pageinfo, orders
def get_products(self, target_page): """ 获得category中的商品集合 """ corp_id = int(CorporationFactory.get().id) #由于历史数据库设计不合理,导致sql语句太复杂,这里使用raw sql进行查询 sql = """ SELECT c.id as id, c.category_id as category_id, c.product_id as product_id, p.status as status, c.display_index as display_index FROM mall_category_has_product as c INNER JOIN product_pool as p WHERE c.category_id = %d AND p.woid = %d AND c.product_id = p.product_id AND p.status in (0, 1) ORDER BY status desc, display_index, id desc LIMIT %d OFFSET %d """ % (self.category.id, corp_id, target_page.count_per_page, (target_page.cur_page - 1) * target_page.count_per_page) db = eaglet_db.db cursor = db.execute_sql(sql, ()) relation_ids = [] product2index = {} for index, row in enumerate(cursor.fetchall()): relation_id = row[0] product_id = row[2] relation_ids.append(relation_id) product2index[product_id] = index product_relations = mall_models.CategoryHasProduct.select().dj_where( id__in=relation_ids) category_products = self.__get_category_products_for_category_product_relations( product_relations) objects = FakeObjects() objects.item_count = mall_models.CategoryHasProduct.select().dj_where( category_id=self.category.id).count() pageinfo, _ = paginator.paginate(objects, target_page.cur_page, target_page.count_per_page) category_products.sort( lambda x, y: cmp(product2index[x.id], product2index[y.id])) return category_products, pageinfo
def get_on_shelf_products_for_category(self, category_id, corp_id, fill_options=None, target_page=None): """ 获取分组下的上架商品集合 """ product_relations = mall_models.CategoryHasProduct.select().dj_where(category_id=category_id)\ .order_by(mall_models.CategoryHasProduct.display_index, mall_models.CategoryHasProduct.created_at.desc()) product_ids = [relation.product_id for relation in product_relations] on_shelf_products = mall_models.ProductPool.select().dj_where( product_id__in=product_ids, status=mall_models.PP_STATUS_ON, woid=corp_id) on_shelf_product_ids = [] if not fill_options: products = [] for pool in on_shelf_products: on_shelf_product_ids.append(pool.product_id) product = mall_models.Product() product.id = pool.product_id products.append(Product(product)) products = sorted(products, key=lambda k: product_ids.index(k.id)) page_info, products = paginator.paginate( products, target_page.cur_page, target_page.count_per_page) return products, page_info else: options = {'order_options': ['-status', '-id']} filters = {'__f-id-in': on_shelf_product_ids} products, pageinfo = CorporationFactory().get( ).product_pool.get_products( target_page, fill_options, filters=filters, options=options, ) return products, pageinfo
def filter_corps(self, args=None, page_info=None): """ 筛选条件:is_weizoom_corp、status """ args = args if args else {} type = args.get('type') status = args.get('status') db_models = account_model.UserProfile.select() if not type == None: db_models = db_models.dj_where(webapp_type=int(type)) if status and not int(status) == -1: db_models = db_models.dj_where(status=int(status)) corps = [] for model in db_models: corp = Corporation(model.user_id) corps.append(corp) if page_info: page_info, corps = paginator.paginate(corps, page_info.cur_page, page_info.count_per_page) return page_info, corps
def get(args): """ @param page 可选参数,1 我发表的,2 我支持的,不带此参数则显示全部数据 @return want_to_buy_list """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] is_binded = webapp_user.is_binded member_id = webapp_user.member.id if not is_binded: #如果没绑定手机则直接返回 return {'is_binded': False} member_card = MemberCard.from_member_id({"member_id": member_id}) is_vip = True if member_card else False if not is_vip: #如果不是会员则直接返回 return {'is_binded': is_binded, 'is_vip': is_vip} #是否点击过广告蒙版 ad_clicked = AdClicked.from_member_id({ "member_id": member_id, "type": WANT_TO_BUY_PAGE_TYPE }) is_ad_clicked = True if ad_clicked else False page = int(args.get('page', '0')) items = [] if page == MY_WANT_TO_BUY_PAGE_PARAM: items = WantToBuy.get_my_list({ 'webapp_owner': webapp_owner, 'member_id': member_id }) elif page == MY_SUPPORT_PAGE_PARAM: items = WantToBuy.get_my_support_list({ 'webapp_owner': webapp_owner, 'member_id': member_id }) else: items = WantToBuy.get_all_list({'webapp_owner': webapp_owner}) #分页 count_per_page = int(args.get('count_per_page', DEFAULT_COUNT_PER_PAGE)) cur_page = int(args['cur_page']) pageinfo, items = paginator.paginate(items, cur_page, count_per_page) _items = [] for item in items: _item = item.to_dict() _item['is_can_support'] = False #是否显示“支持一下”按钮 if is_binded and is_vip and not item.has_supported( member_id) and item.member_id != member_id: _item['is_can_support'] = True _item['is_show_view_progress'] = False #是否显示“查看采购进度”按钮 if is_binded and is_vip and item.is_success: _item['is_show_view_progress'] = True _items.append(_item) return { 'is_binded': True, 'is_vip': is_vip, 'is_ad_clicked': is_ad_clicked, 'items': _items, 'page_info': pageinfo.to_dict() }
def get(args): """ 获取商品列表 新版本 @param category_id 商品分类ID @return { 'categories': simple_products.categories, 'products': simple_products.products, 'category': category_dict} """ category_id = args['category_id'] webapp_owner = args['webapp_owner'] webapp_user = args['webapp_user'] cur_page = args.get('cur_page', 1) webapp_type = webapp_owner.user_profile.webapp_type product_name = args.get('product_name', None) if webapp_type == 1: # if False: # 自营平台 simple_products = NewSimpleProducts.get({ "webapp_owner": webapp_owner, "category_id": category_id, "cur_page": cur_page }) products = simple_products.products page_info = simple_products.page_info.to_dict() if products is None: return { 'categories': simple_products.categories, 'products': [], 'category': {}, 'mall_config': webapp_owner.mall_config, "is_cache_pending": True, 'page_info': page_info } if product_name: # 商品搜索 searcher = NewProductSearch.get({ "webapp_owner": webapp_owner, "webapp_user": webapp_user, "cur_page": cur_page }) page_info, products = searcher.search_products( category_id, cur_page, product_name) page_info = page_info.to_dict() from eaglet.core import watchdog print '>>>>>>>>>>>>>>>>>>>>>>>>>>' watchdog.info(page_info) print '>>>>>>>>>>>>>>>>>>>>>>>>>>' if products is None: return { 'categories': simple_products.categories, 'products': [], 'category': {}, 'mall_config': webapp_owner.mall_config, "is_cache_pending": True, 'page_info': page_info } # TODO 拆分出来 category_dict = simple_products.category.to_dict('is_deleted') return { 'categories': simple_products.categories, 'products': products, 'category': category_dict, 'mall_config': webapp_owner.mall_config, "is_cache_pending": False, 'page_info': page_info } else: simple_products = SimpleProducts.get({ "webapp_owner": webapp_owner, "category_id": category_id, }) products = simple_products.products if product_name: # 商品搜索 searcher = ProductSearch.get({ "webapp_owner": webapp_owner, "webapp_user": webapp_user }) products = searcher.filter_products({ 'products': products, 'product_name': product_name }) category_dict = simple_products.category.to_dict('is_deleted') if products: page_info, products = paginator.paginate(products, cur_page, 6) else: page_info, products = paginator.paginate([], cur_page, 6) return { 'categories': simple_products.categories, 'products': products, 'category': category_dict, 'mall_config': webapp_owner.mall_config, # 老版本 "is_cache_pending": False, 'page_info': page_info.to_dict() }
def filter_products(self, query_dict, page_info, fill_options=None): db_models = mall_models.Product.select().dj_where(is_deleted=False) if query_dict.get('status') == 'verified': product_pool_ids = [ p.product_id for p in mall_models.ProductPool.select().dj_where( woid=query_dict['corp'].id, status__not=mall_models.PP_STATUS_DELETE) ] db_models = db_models.dj_where( id__in=product_pool_ids, is_accepted=True, status__not=mall_models.PRODUCT_STATUS['SUBMIT']) elif query_dict.get('status') == 'unverified': db_models = db_models.dj_where(is_accepted=False) elif query_dict.get('status') == 'updated': db_models = db_models.dj_where( is_accepted=True, status=mall_models.PRODUCT_STATUS['SUBMIT'], is_updated=True) if self.corp.is_weizoom_corp(): if query_dict.get('status') != 'verified': db_models = db_models.dj_where(status__in=[ mall_models.PRODUCT_STATUS['SUBMIT'], mall_models.PRODUCT_STATUS['REFUSED'] ]) else: db_models = db_models.dj_where(owner_id=query_dict['corp'].id) #筛选 filter = self.__get_filter_params(query_dict) product_name = filter.get('name') classification_name = filter.get('classification') status = filter.get('status') owner_name = filter.get('owner_name') if product_name: db_models = db_models.dj_where(name__icontains=product_name) if classification_name: classification_models = list( mall_models.Classification.select().dj_where( name__icontains=classification_name)) #能够查询出子分类的数据(默认二级) classification_models += list( mall_models.Classification.select().dj_where( father_id__in=[c.id for c in classification_models])) relation_models = mall_models.ClassificationHasProduct.select( ).dj_where( classification_id__in=[c.id for c in classification_models]) db_models = db_models.dj_where( id__in=[r.product_id for r in relation_models]) if not status == None: status = int(status) if status == self.FILTER_CONST['ALL']: pass if status in [ self.FILTER_CONST['NOT_YET'], self.FILTER_CONST['SUBMIT'] ]: db_models = db_models.dj_where(status=status, is_accepted=False) elif status == self.FILTER_CONST['POOL_REFUSED']: db_models = db_models.dj_where( status=mall_models.PRODUCT_STATUS['REFUSED'], is_accepted=False) elif status == self.FILTER_CONST['UPDATE_REFUSED']: db_models = db_models.dj_where( status=mall_models.PRODUCT_STATUS['REFUSED'], is_accepted=True, is_updated=True) elif status == self.FILTER_CONST['PASSED']: db_models = db_models.dj_where( status=mall_models.PRODUCT_STATUS['NOT_YET'], is_accepted=True) products = [] if owner_name: products = [Product(model) for model in db_models] self.__fill_product_details(products, {'with_supplier_info': True}) fill_options['with_supplier_info'] = False tmp_products = [] for product in products: if product.supplier_info and owner_name in product.supplier_info[ 'company_name']: tmp_products.append(product) if page_info: pageinfo, products = paginator.paginate( tmp_products, page_info.cur_page, page_info.count_per_page) else: pageinfo = None else: db_models = db_models.order_by(-mall_models.Product.id) if page_info: pageinfo, db_models = paginator.paginate( db_models, page_info.cur_page, page_info.count_per_page) else: pageinfo = None for model in db_models: pre_product = Product(model) products.append(pre_product) fill_options = fill_options if fill_options else {} self.__fill_product_details(products, fill_options) return pageinfo, products
def search_products(self, category_id, cur_page, search_name): try: self.__log_record(search_name) except: msg = unicode_full_stack() watchdog.alert(msg) cache_no_data = False webapp_owner = self.context['webapp_owner'] category_products_key = '{wo:%s}_{co:%s}_pids' % (webapp_owner.id, 0) if not cache_util.exists_key(category_products_key): # 发送消息让manager_cache缓存分组数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_category_product' data = {"corp_id": webapp_owner.id, "category_id": category_id} msgutil.send_message(topic_name, msg_name, data) cache_no_data = True if not cache_util.exists_key('all_simple_effective_products'): # TODO发送消息让manager_cache缓存所有简单商品数据 topic_name = settings.TOPIC_NAME msg_name = 'all_simple_effective_products' msgutil.send_message(topic_name, msg_name, {}) cache_no_data = True # 用于搜索的额所有有效商品数据 all_simple_products = redis_util.hgetall( 'all_simple_effective_products') # 分类所拥有的商品id category_product_ids = list( redis_util.lrange(category_products_key, 0, -1)) if category_product_ids and category_product_ids[0] == 'NONE': # 说明分组无商品 page_info, page_product_ids = paginator.paginate([], cur_page, 6) return page_info, [] # 搜索结果id product_ids = [] for product_id, product_name in all_simple_products.items(): if search_name in product_name and product_id in category_product_ids: product_ids.append(product_id) # 获取分页信息 page_info, page_product_ids = paginator.paginate( product_ids, cur_page, 6) if not page_product_ids: return page_info, [] # TODO 可抽取公用方法 keys = [ ':1:apiproduct_simple_detail_{pid:%s}' % product_id for product_id in page_product_ids ] redis_products = redis_util.mget(keys) # 缓存没有此商品详情的key,故需mall_cache_manager缓存数据 # 这里使用page_product_ids 严重注意这个问题,屌丝! no_redis_product_ids = [ page_product_ids[index] for index, r in enumerate(redis_products) if r is None ] if no_redis_product_ids: cache_no_data = True # 发送消息让manager_cache缓存分组数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_product_detail' data = { "corp_id": webapp_owner.id, "product_ids": no_redis_product_ids } msgutil.send_message(topic_name, msg_name, data) if cache_no_data: page_info, page_product_ids = paginator.paginate([], cur_page, 6) return page_info, None products = [pickle.loads(product) for product in redis_products] result = sorted(products, key=lambda k: page_product_ids.index(str(k.get('id')))) for product in result: temp_key = "customized_price_{wo:%s}_{pid:%s}" % ( webapp_owner.id, product.get('id')) customized_price = cache_util.get_cache(temp_key) if cache_util.get_cache(temp_key): product['display_price'] = customized_price return page_info, result
def __get_products_by_category(self, category_id, corp_id, cur_page): """ 根据商品分类获取商品简单信息 """ key = '{wo:%s}_{co:%s}_pids' % (corp_id, category_id) cache_no_data = False if not cache_util.exists_key(key): cache_no_data = True # 发送消息让manager_cache缓存分组数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_category_product' data = {"corp_id": corp_id, "category_id": category_id} msgutil.send_message(topic_name, msg_name, data) # if not cache_util.exists_key('all_simple_effective_products'): # # # TODO发送消息让manager_cache缓存所有简单商品数据 # topic_name = settings.TOPIC_NAME # msg_name = 'refresh_all_simple_products' # # msgutil.send_message(topic_name, msg_name, {}) # cache_no_data = True product_ids = list(redis_util.lrange(key, 0, -1)) if product_ids and product_ids[0] == 'NONE': # 说明分组无商品 page_info, page_product_ids = paginator.paginate([], cur_page, 6) return page_info, [] # 获取分页信息 page_info, page_product_ids = paginator.paginate( product_ids, cur_page, 6) # 有缓存缓存但是缓存里没有数据 if not page_product_ids and not cache_no_data: return page_info, [] if cache_no_data: # 缓存key都不在, return page_info, None keys = [ ':1:apiproduct_simple_detail_{pid:%s}' % product_id for product_id in page_product_ids ] redis_products = redis_util.mget(keys) # 缓存没有此商品详情的key,故需mall_cache_manager缓存数据 no_redis_product_ids = [ page_product_ids[index] for index, r in enumerate(redis_products) if r is None ] if no_redis_product_ids: cache_no_data = True # 发送消息让manager_cache缓存分组数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_product_detail' data = {"corp_id": corp_id, "product_ids": no_redis_product_ids} msgutil.send_message(topic_name, msg_name, data) if cache_no_data: # products is Nond 的时候要显示缓存无数据所以这里是返回None page_info, page_product_ids = paginator.paginate([], cur_page, 6) return page_info, None products = [pickle.loads(product) for product in redis_products] result = sorted(products, key=lambda k: page_product_ids.index(str(k.get('id')))) # 判断社群平台是否是"固定底价+溢价"类型平台 for product in result: temp_key = "customized_price_{wo:%s}_{pid:%s}" % ( corp_id, product.get('id')) customized_price = cache_util.get_cache(temp_key) if cache_util.get_cache(temp_key): product['display_price'] = customized_price return page_info, result
def get_products(self, page_info, fill_options=None, options=None, filters=None): """ 根据条件在商品池搜索商品 @return: """ type2filters = self.__split_filters(filters) #构建排序策略 order_fields = self.__get_product_order_fields(options) #在product_pool表中进行过滤 product_pool_filters = type2filters['product_pool'] #进行查询 if product_pool_filters: # 补充首次上架时间搜索值null判断 sync_at__range = product_pool_filters.get('sync_at__range') if sync_at__range: sync_at__range = list(sync_at__range) if not sync_at__range[0]: sync_at__range[0] = '1999-01-01' if not sync_at__range[1]: sync_at__range[1] = '2999-01-01' product_pool_filters['sync_at__range'] = sync_at__range pool_product_models = mall_models.ProductPool.select().dj_where( **product_pool_filters) else: pool_product_models = mall_models.ProductPool.select().dj_where( status__not=mall_models.PP_STATUS_DELETE) if len(order_fields) > 0: # 考虑可以使用内存排序,而不是mysql自己排序 pool_product_models = pool_product_models.order_by(*order_fields) #获取查询结果 product_ids = [] product2poolmodel = dict() for pool_product_model in pool_product_models: product_ids.append(pool_product_model.product_id) product2poolmodel[ pool_product_model.product_id] = pool_product_model copy_product_ids = copy.copy(product_ids) #在mall_product_model中进行过滤 product_model_filters = type2filters['product_model'] if product_model_filters: product_model_filters['product_id__in'] = product_ids # 只设置库存最小值,不设置库存最大值的情况下,才能将库存为无限的商品查询出来 stocks__lte = product_model_filters.get('stocks__lte') if stocks__lte and stocks__lte != u'999999999': product_model_filters[ 'stock_type'] = mall_models.PRODUCT_STOCK_TYPE_LIMIT product_model_models = mall_models.ProductModel.select().dj_where( **product_model_filters) product_ids = [model.product_id for model in product_model_models] # product_ids = sorted(list(set(temp_product_ids)), key=product_ids.index) #在mall_category_has_product中进行过滤 product_category_filters = type2filters['product_category'] if product_category_filters: product_category_filters['product_id__in'] = product_ids relations = mall_models.CategoryHasProduct.select().dj_where( **product_category_filters) product_ids = [relation.product_id for relation in relations] #在mall_product_sales中进行过滤 product_sales_filters = type2filters['product_sales'] if product_sales_filters: product_sales_filters['product_id__in'] = product_ids models = mall_models.ProductSales.select().dj_where( **product_sales_filters) sales__lte = product_sales_filters.get('sales__lte') sales__gte = product_sales_filters.get('sales__gte') # 不在mall_product_sales 的product temp_product_ids = [] # 在mall_product_sales中没有数据的商品销量也是0 if int(sales__gte) <= 0 and int(sales__lte) >= 0: all_product_sales_models = mall_models.ProductSales.select( ).dj_where(product_id__in=product_ids) all_product_ids = [ sales_model.product_id for sales_model in all_product_sales_models ] temp_product_ids = list( set(product_ids) - set(all_product_ids)) product_ids = [model.product_id for model in models] if temp_product_ids: product_ids += temp_product_ids #todo 根据供应商进行过滤 supplier_ids = None # product_supplier_filters = type2filters['product_supplier'] # supplier_ids = None # if product_supplier_filters: # from business.supplier.supplier_repository import SupplierRepository # suppliers = SupplierRepository.get(self.corp).get_suppliers() # # supplier_ids = [supplier.id for supplier in suppliers] #根据商品分类进行过滤 product_classification_filters = type2filters['product_classification'] if product_classification_filters: product_classification_filters['product_id__in'] = product_ids product_ids = [ relation.product_id for relation in mall_models.ClassificationHasProduct.select(). dj_where(**product_classification_filters) ] #根据商品标签进行过滤 product_label_filters = type2filters['product_label'] if product_label_filters: product_label_filters['product_id__in'] = product_ids product_ids = [ relation.product_id for relation in mall_models.ProductHasLabel.select().dj_where( **product_label_filters) ] # 根据cps推广进行过滤 product_promotion_filter_values = type2filters['product_promotion'] if product_promotion_filter_values: if product_ids: product_promotion_filter_values.update( {'product_id__in': product_ids}) product_ids = [ promote.product_id for promote in mall_models.PromoteDetail.select().dj_where( **product_promotion_filter_values) ] # product_ids = list(set(product_ids) - set(promoted_product_ids)) #在mall_product表中进行过滤 product_info_filters = type2filters['product_info'] if supplier_ids is not None: product_info_filters['supplier_id__in'] = supplier_ids with_base = fill_options.get('with_base', True) if not options.get('request_source' ) == 'unshelf_consignment': #商品池中的商品展示不需要按照毛利率排序 if product_info_filters: product_info_filters['id__in'] = product_ids if with_base: product_models = mall_models.Product.select().dj_where( **product_info_filters) else: product_models = [ mall_models.Product(id=product_id) for product_id in product_ids ] # mysql使用in查询会将in列表值先排序再二分搜索,固需要再次排序. product_models = sorted( product_models, key=lambda k: copy_product_ids.index(k.id)) else: # 对product_ids 进行内存排序,按照最原始查出来的顺序,并去掉重复数据 product_ids = sorted(list(set(product_ids)), key=copy_product_ids.index) if with_base: product_models = mall_models.Product.select().dj_where( id__in=product_ids) else: product_models = [ mall_models.Product(id=product_id) for product_id in product_ids ] product_models = sorted(product_models, key=lambda k: product_ids.index(k.id)) # 为了能够按照毛利率进行排序,首先得填充规格和效果通数据,再分页 products = [Product(model) for model in product_models] fill_product_detail_service = FillProductDetailService.get( self.corp) fill_product_detail_service.fill_detail( products, { 'with_product_model': True, 'with_model_property_info': True, 'with_cps_promotion_info': True }) if self.corp.is_community( ) and not self.corp.details.settlement_type == account_models.ACCOUNT_DIVIDE_TYPE_FIXED: products = sorted( products, key=lambda k: k.gross_profit_info['gross_profit_rate'], reverse=True) pageinfo, products = paginator.paginate(products, page_info.cur_page, page_info.count_per_page) #填充其他数据 fill_options['with_product_model'] = False fill_options['with_model_property_info'] = False fill_options['with_cps_promotion_info'] = False fill_product_detail_service.fill_detail(products, fill_options) else: if product_info_filters: product_info_filters['id__in'] = product_ids if with_base: product_models = mall_models.Product.select().dj_where( **product_info_filters) else: product_models = [ mall_models.Product(id=product_id) for product_id in product_ids ] # mysql使用in查询会将in列表值先排序再二分搜索,固需要再次排序. product_models = sorted( product_models, key=lambda k: copy_product_ids.index(k.id)) pageinfo, product_models = paginator.paginate( product_models, page_info.cur_page, page_info.count_per_page) else: # 对product_ids 进行内存排序,按照最原始查出来的顺序,并去掉重复数据 product_ids = sorted(list(set(product_ids)), key=copy_product_ids.index) pageinfo, product_ids = paginator.paginate( product_ids, page_info.cur_page, page_info.count_per_page) if with_base: product_models = mall_models.Product.select().dj_where( id__in=product_ids) else: product_models = [ mall_models.Product(id=product_id) for product_id in product_ids ] product_models = sorted(product_models, key=lambda k: product_ids.index(k.id)) products = [Product(model) for model in product_models] fill_product_detail_service = FillProductDetailService.get( self.corp) fill_product_detail_service.fill_detail(products, fill_options) result = [] for product in products: pool_product_model = product2poolmodel[product.id] if pool_product_model.type == mall_models.PP_TYPE_SYNC: product.create_type = 'sync' else: product.create_type = 'create' product.sync_at = pool_product_model.sync_at result.append(product) return result, pageinfo
def get(args): """ 会员订单列表 @param type """ webapp_user = args['webapp_user'] webapp_owner = args['webapp_owner'] count_per_page = int(args.get('count_per_page', DEFAULT_COUNT_PER_PAGE)) cur_page = int(args['cur_page']) order_type = int(args['order_type']) orders = Order.get_for_list_page({ 'order_type': order_type, 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, # 'cur_page': cur_page, # 'count_per_page': count_per_page }) # finished 1.团购 # finished 2.订单循环 # todo 3.评论 # todo 4.商品 order_id2group_info = Order.get_group_infos_for_orders({ 'orders': orders, 'woid': webapp_owner.id }) # 过滤已取消的团购订单,但优惠抵扣的显示 # orders = filter(lambda order: not(order.is_group_buy and order.status == mall_models.ORDER_STATUS_CANCEL) or order.pay_interface_type == mall_models.PAY_INTERFACE_PREFERENCE ,orders) orders = filter( lambda order: not (order_id2group_info[order.order_id] and order. status == mall_models.ORDER_STATUS_CANCEL) or order.pay_interface_type == mall_models.PAY_INTERFACE_PREFERENCE, orders) # 注意:所有排序、过滤操作需要在分页之前 pageinfo, orders = paginator.paginate(orders, cur_page, count_per_page) param_data = { 'woid': args['webapp_owner'].id, 'member_id': args['webapp_user'].member.id } get_order_review_json = [] resp = Resource.use('marketapp_apiserver').get({ 'resource': 'evaluate.get_order_evaluates', 'data': param_data }) if resp: code = resp["code"] if code == 200: get_order_review_json = resp["data"]['orders'] order_id2review = {} if get_order_review_json: for order_review in get_order_review_json: order_id2review[int(order_review["order_id"] )] = order_review["order_is_reviewed"] # order_id指的是order.id order_datas = [] for order in orders: if order_id2review.has_key(order.id): review_is_finished = order_id2review[order.id] else: review_is_finished = False data = { 'id': order.id, 'order_id': order.order_id, 'status': order.status, 'pay_interface_type': order.pay_interface_type, 'created_at': order.created_at.strftime('%Y.%m.%d %H:%M'), 'final_price': order.final_price, 'has_sub_order': order.has_sub_order, 'has_multi_sub_order': order.has_multi_sub_order, 'express_number': order.express_number, 'review_is_finished': review_is_finished, 'red_envelope': order.red_envelope, 'red_envelope_created': order.red_envelope_created, 'products': [], 'is_group_buy': bool(order_id2group_info[order.order_id]), 'order_group_info': order_id2group_info[order.order_id] } order_products = OrderProducts.get_for_order({ 'webapp_owner': webapp_owner, 'webapp_user': webapp_user, 'order': order }) total_product_count = 0 for order_product in order_products.products: product_data = { 'id': order_product.id, 'name': order_product.name, 'purchase_count': order_product.purchase_count, 'thumbnails_url': order_product.thumbnails_url, 'model': order_product.model.to_dict() if order_product.model else None } total_product_count += order_product.purchase_count data['products'].append(product_data) data['product_count'] = total_product_count data['pay_info'] = order.pay_info order_datas.append(data) order_config = OrderConfig.get_order_config( {'webapp_owner': webapp_owner}) return { 'orders': order_datas, 'order_config': order_config, 'page_info': pageinfo.to_dict() }
def get_products_by_page(self, supplier_id, webapp_owner, cur_page, cur_page_count): """ 获取供货商下所有商品 """ # TODO 发送消息并拆分代码 key = "supplier_products:{%s}" % supplier_id if not cache_util.exists_key(key): products = mall_models.Product.select().dj_where( supplier=supplier_id) product_ids = [product.id for product in products] redis_util.sadd(key, *product_ids) else: product_ids = redis_util.smemebers(key_name=key) # 获取该平台所有上架商品 all_product_key = '{wo:%s}_{co:%s}_pids' % (webapp_owner.id, 0) no_cache = False if not cache_util.exists_key(key): # 发送消息让manager_cache缓存分组数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_supplier_products' data = {"corp_id": webapp_owner.id, "category_id": 0} msgutil.send_message(topic_name, msg_name, data) no_cache = True all_product_ids = redis_util.lrange(all_product_key, 0, -1) all_product_ids = [] if all_product_ids is None else all_product_ids # 该平台该供货商所有商品id product_ids = list(set(product_ids).intersection(set(all_product_ids))) page_info, page_product_ids = paginator.paginate( product_ids, cur_page, cur_page_count) keys = [ ':1:apiproduct_simple_detail_{pid:%s}' % product_id for product_id in page_product_ids ] print keys, '--------------------------------jdkf' if not keys: page_info, page_product_ids = paginator.paginate([], cur_page, cur_page_count) return [], True, page_info redis_products = redis_util.mget(keys) # 缓存没有此商品详情的key,故需mall_cache_manager缓存数据 no_redis_product_ids = [ page_product_ids[index] for index, r in enumerate(redis_products) if r is None ] if no_redis_product_ids: # 发送消息让manager_cache缓存数据 topic_name = settings.TOPIC_NAME msg_name = 'refresh_product_detail' data = { "corp_id": webapp_owner.id, "product_ids": no_redis_product_ids } msgutil.send_message(topic_name, msg_name, data) no_cache = True if no_cache: page_info, page_product_ids = paginator.paginate([], cur_page, cur_page_count) return [], True, page_info # 返回商品数据 products = [pickle.loads(product) for product in redis_products] # result = sorted(products, key=lambda k: page_product_ids.index(str(k.get('id')))) return products, False, page_info