def upload_sku(): ''' excel导入卖家sku ''' if request.method == 'POST': upload_data = request.get_records(field_name='file') # 检查卖家sku是否重复 sku_list = [item['SKU'] for item in upload_data if item['SKU'] != ''] for i in sku_list: if sku_list.count(i) > 1: result = {'status': 1, 'msg': '导入失败,卖家SKU:{}重复了'.format(i)} return Response(json.dumps(result), mimetype='application/json') # sku导入处理 match_sum, not_match_sum, forty_more_sum = 0, 0, 0 for item in upload_data: ob = Shop.objects(goods_url=item['商品链接']) if not ob: not_match_sum += 1 elif len(str(item['SKU'])) > 40: forty_more_sum += 1 else: ob.update_one(set__sku_id=str(item['SKU'])) match_sum += 1 result = { 'status': 0, 'msg': '导入成功!{0}个商品匹配成功,{1}个商品链接无法匹配,{2}个商品的SKU超过40个字符,插入失败'\ .format(match_sum, not_match_sum, forty_more_sum) } return Response(json.dumps(result), mimetype='application/json') return '''
def add_order_list(): ''' 批量或逐个加入采购单 ''' redis = RedisClient() user_login_dict = redis.get_login_info_by_img(session['QR']) cookie = user_login_dict.get('cookie') # 获取登录cookie # 查询参数 ids_json = request.args.get( 'ids' ) or '{"ids": ["5b7a6a093f3e1cceb46c84da", "5b7a6a023f3e1cceb46c84c1", "5b7a6c943f3e1cceb46c9f0e"]}' ids_dict = json.loads(ids_json) ids = ids_dict['ids'] order_list = [] if len(OrderCart.objects) < 50: for id in ids: if not OrderCart.objects(_id=ObjectId(id)): # 排除重复加入的订单 order_dict = Shop.objects( _id=ObjectId(id)).first().to_mongo().to_dict() order_list.append(order_dict) if order_list: final_order_list = taobao_query(order_list, cookie) for order_dict in final_order_list: OrderCart(**order_dict).save() result = {'status': 0, 'msg': '加入成功'} else: result = {'status': 1, 'msg': '商品已经在采购单里面了,无需重复添加'} else: result = {'status': 1, 'msg': '加入失败,最多只能加入50个商品'} return Response(json.dumps(result), mimetype='application/json')
def save_sku(): ''' 保存sku至shop商品库 ''' id = request.args.get('id') sku_id = request.args.get('skuId') shop_order = request.args.get('shopOrder') try: o_id = ObjectId(id) Shop.objects(_id=o_id).update_one(set__sku_id=sku_id) result = {'status': 0, 'msg': '请求成功'} except Exception: current_app.logger.error("sku can't be saved!", exc_info=True) result = {'status': 1, 'msg': '保存失败,网络有点问题,请稍后重试'} return Response(json.dumps(result), mimetype='application/json')
def search_view(): ''' 采购单关键词联想搜索 ''' # 查询字符串 goods_title = request.args.get('goodsTitle') sku_id = request.args.get('skuId') shop_list = [] if goods_title: queryset = Shop.objects( __raw__={'goods_title': { '$regex': goods_title }}) # 查询商品名称 for ob in queryset: ob_dict = ob.to_mongo().to_dict() shop_dict = { 'id': str(ob_dict['_id']), 'goodsTitle': ob_dict['goods_title'], 'skuId': ob_dict['sku_id'] } shop_list.append(shop_dict) if len(shop_list) == 10: # 取前10条记录 break if sku_id: queryset = Shop.objects(__raw__={'sku_id': { '$regex': sku_id }}) # 查询商家sku for ob in queryset: ob_dict = ob.to_mongo().to_dict() shop_dict = { 'id': str(ob_dict['_id']), 'goodsTitle': ob_dict['goods_title'], 'skuId': ob_dict['sku_id'] } shop_list.append(shop_dict) if len(shop_list) == 10: break if shop_list: result = {'status': 0, 'data': {'list': shop_list}, 'msg': '请求成功'} else: result = {'status': 1, 'msg': '没有找到相关商品'} return Response(json.dumps(result), mimetype='application/json')
def ShopCollection(id): categories = InternCategoriesShop(id) shop = Shop.objects().filter(id=id) for t in shop: items = { "id": str(t['id']), "name": t['name'], "email": t['email'], "nit": t['nit'], "phone": t['phone'], "address": t['address'], "shipping": t['shipping'], "_categories": json.loads(categories) } return items, 200
def Shops(): items = Shop.objects().to_json() return jsonify(items), 200
def ShopNit(id): items = Shop.objects().filter(nit=id).to_json() return Response(items, mimetype="application/json", status=200)
def ShopId(id): items = Shop.objects().get(id=id).to_json() return Response(items, mimetype="application/json", status=200)
def get_shop_list(): ''' 商品库信息组合去重及商品库模糊搜索功能 ''' # 查询参数 ww_id = request.args.get('wwId') or '' store_name = request.args.get('storeName') or '' goods_title = request.args.get('goodsTitle') or '' sku_id = request.args.get('skuId') or '' # 分页参数 page_num = int(request.args.get('pageNum') or 1) page_size = int(request.args.get('pageSize') or 20) # 从历史订单数据库更新数据到商品库,不覆盖原有数据,保证卖家sku的完整保留 one_record = Shop.objects.order_by('-_id').first() # 筛选shop中_id最大值的记录 max_id = one_record['_id'] if one_record else None if max_id: # 取出历史订单orders中大于此_id的记录 query_set = Orders.objects(_id__gt=max_id) else: # 商品库为空,取出历史订单所有记录 query_set = Orders.objects() if query_set: # 将查询结果去重并保存至shop中 only_records = query_set.aggregate( # 组合去重 { '$group': { '_id': {'goods_url': "$goods_url", 'goods_sku1': "$goods_sku1", 'goods_sku2': "$goods_sku2"}, 'id': {'$max': "$_id"}, # 取_id最大值 'goods_url': {'$first': "$goods_url"}, 'goods_title': {'$first': "$goods_title"}, 'goods_img': {'$first': "$goods_img"}, 'store_name': {'$first': "$store_name"}, 'ww_name': {'$first': "$ww_name"}, 'real_per_price': {'$first': "$real_per_price"}, 'goods_sku1': {'$first': "$goods_sku1"}, 'goods_sku2': {'$first': "$goods_sku2"} } } ) for item in only_records: item.pop('_id') item['_id'] = item.pop('id') item['sku_id'] = '' if not max_id: # 商品库为空,直接保存 Shop(**item).save() else: # 商品库不为空,查询无重复再保存 if not Shop.objects(goods_url=item['goods_url'], goods_sku1=item['goods_sku1'], goods_sku2=item['goods_sku2']): Shop(**item).save() # 商品库展示 if ww_id or store_name or goods_title or sku_id: # 条件查询(模糊查询) page_data = Shop.objects( Q(__raw__={'ww_name': { '$regex': ww_id }}) & Q(__raw__={'store_name': { '$regex': store_name }}) & Q(__raw__={'goods_title': { '$regex': goods_title }}) & Q(__raw__={'sku_id': { '$regex': sku_id }})).paginate(page=page_num, per_page=page_size) else: # 一般情况(无查询) page_data = Shop.objects().paginate(page=page_num, per_page=page_size) list_all_query = [] for item in page_data.items: new_each_result = OrderedDict() for k, v in MAPPING.items(): new_each_result[k] = str(item[v]) if item[v] else '' list_all_query.append(new_each_result) result = { 'status': 0, 'total': page_data.total, 'pageSize': page_size, 'pageNum': page_data.page, 'data': { 'list': list_all_query }, 'msg': '请求成功' } return Response(json.dumps(result), mimetype='application/json')
def ShopSlug(slug): items = Shop.objects().filter(slug=slug).to_json() return Response(items, mimetype="application/json", status=200)