def get_visits(): """获取访问量""" db_conn = ToMongo() col = db_conn.get_col('visits') # 获取29天的时间差 date = (datetime.datetime.now() - datetime.timedelta(days=29)) # 查询大于时间差的数据 before_30_day = get_30_day_before_timestamp() data = list(col.find({'date': {'$gte': before_30_day}})) # https://blog.csdn.net/qq_42184799/article/details/86311804 # collections.OrderedDict()字典按插入顺序排序 day_list = collections.OrderedDict() # 获取时间列表['04-25', '04-26', '04-27', '04-28'] for i in range(0, 30): day = date.strftime("%m-%d") day_list[day] = 0 date = date + datetime.timedelta(days=1) for d in data: d_farmat = format_m_d(d['date']) if d_farmat in day_list: day_list[d_farmat] = len(d['users_id']) x = [] y = [] for k, v in day_list.items(): x.append(k) y.append(v) db_conn.close_conn() return x, y
def choice_book_type(num=11, all=False): my_conn = ToMongo() type_list = list(my_conn.get_col('books').distinct('first_type')) my_conn.close_conn() if all: return type_list return sample(type_list, num)
def get_order(order_no): """获取订单详情""" conn = ToMongo() order = conn.get_col('order').find_one({'order_no': order_no}) books = order.get('books') new_books = [] for book in books: book_info = conn.get_col('books').find_one( {'_id': ObjectId(book.get('book_id'))}) book_num = book.get('book_num') new_books.append({'book': book_info, 'book_num': book_num}) order['create_time'] = format_time_second(order.get('create_time')) order['books'] = new_books conn.close_conn() return order
def handle_refund(order_no, status, msg): """退款""" conn = ToMongo() # 还原库存 query = {'order_no': order_no} restore_stock(query, conn) # 更新订单状态 result = conn.update('order', { 'order_no': order_no, 'orders_status': status }, {"$set": { 'orders_status': 5 }}) if result.modified_count: pass else: msg.update({'error': '更新失败'}) conn.close_conn()
def index_model(): """主页内容""" mydb = ToMongo() # 轮播 random_time = int(str(time.time())[-3:]) books = get_reader_recommend(mydb, random_time) new_books = list( mydb.get_col('books').find({ 'is_off_shelf': 0 }).sort("create_time", -1).sort("hits", -1).limit(12)) book_top = list( mydb.get_col('books').find({ 'is_off_shelf': 0 }).sort("pub_time", -1).limit(5)) book_top2 = list( mydb.get_col('books').find({ 'is_off_shelf': 0 }).sort("sales", -1).limit(5)) mydb.close_conn() return books, new_books, book_top, book_top2
def uptate_status_inc(order_no, status, msg): conn = ToMongo() new = {"$inc": {'orders_status': 1}} if status == 0: new = { "$inc": { 'orders_status': 1 }, "$addToSet": { 'logistics': { 'create_time': get_now(), 'info': '商品已经下单' } } } elif status == 1: new = { "$inc": { 'orders_status': 1 }, "$addToSet": { 'logistics': { 'create_time': get_now(), 'info': '包裹正在等待揽收' } } } result = conn.update('order', { 'order_no': order_no, 'orders_status': status }, new, is_one=False) if result.modified_count: pass else: msg.update({'error': '更新失败'}) conn.close_conn()
def get_price(): """ # https://cloud.tencent.com/developer/article/1406368 # 字符串转为数值 # https://www.jb51.net/article/98385.htm # aggregate查询同字段的区间值 :return: """ pipeline = [ { '$group': { '_id': "", 'max': { '$max': '$price' }, 'min': { '$min': '$price' } } }, ] db_conn = ToMongo() max_min = list(db_conn.get_col('books').aggregate(pipeline)) max = float(max_min[0]['max']) min = max_min[0]['min'] list_n = get_n_avg(min, max) book_num = [] sales_num = [] for i in list_n: # print(i[0],i[1]) if i[1] != max: qurey = [{ '$match': { 'price': { '$gte': i[0], '$lt': i[1] } } }, { '$sort': { 'price': -1 } }] r = db_conn.get_col('books').aggregate(qurey) else: qurey = [{ '$match': { 'price': { '$gte': i[0], '$lte': i[1] } } }, { '$sort': { 'price': -1 } }] r = db_conn.get_col('books').aggregate(qurey) r = list(r) book_num.append(len(r)) sales = 0 for i in r: try: if i['sales']: sales = sales + i['sales'] except: pass sales_num.append(sales) db_conn.close_conn() return book_num, sales_num, list_n
def orders_query_model(page, page_size, order_status): query = {} if order_status == 0: # 待付款订单 query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }, { 'create_time': { '$gte': get_before_day() } }] } elif order_status == 1: query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }] } elif order_status == 2: query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }] } elif order_status == 3: query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }] } elif order_status == 4: query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }] } elif order_status == 5: # 失效订单 query = {"$or": [{'orders_status': order_status}]} elif order_status == 6: query = { '$and': [{ 'orders_status': order_status }, { 'is_effective': 1 }] } db_conn = ToMongo() order = db_conn.get_col('order').find(query).sort('create_time', -1).skip( (page - 1) * page_size).limit(page_size) total = db_conn.get_col('order').find(query).count() order_list = list(order) books = [] for order in order_list: order['create_time'] = format_time_second(order.get('create_time')) for book in order.get('books'): id = book.get('book_id') book_num = book.get('book_num') book = db_conn.get_col('books').find_one({'_id': ObjectId(id)}) books.append({'book': book, 'num': book_num}) db_conn.close_conn() return order_list, total, books