def _data_deal(cls, args, user): phoneNum = user['phoneNum'] today = datetime.date.today() tomorrow = (today + datetime.timedelta(days=1)).strftime('%m-%d') lottery_data = mongo[cls.collect_set].find()[0] data = {} for item in lottery_data['list']: key = cls.set + item['id'] if phoneNum in r.lrange(key, 0, -1): item['is_joined'] = 1 else: item['is_joined'] = 0 item['tip'] = '开奖时间 ' + str(tomorrow) + ' 10:00自动开奖' data[item['id']] = item return {'data': data, 'total_number': lottery_data['total_number']}
def __set_cache(cls, good_id, phoneNum): # 存入redis history_data = { 'good_id': good_id, 'timestamp': int(time.time()) } history_key = cls.history_set + ':' + phoneNum history_list = r.lrange(history_key, 0, -1) for val in history_list: if re.findall(good_id, val): r.lrem(history_key, 0, val) r.lpush(history_key, history_data) if r.llen(history_key) > 60: # 最大可存储60条浏览记录 r.rpop(history_key) # 设置过期时间为60天 r.expire(history_key, cls.expired_time * 4)
def _data_deal(cls, args, user): phoneNum = user['phoneNum'] good_id = args['id'] key = cls.set + good_id if phoneNum not in r.lrange(key, 0, -1): lottery_data = mongo[cls.collect_set].find()[0] lottery_data['total_number'] += 1 for good in range(len(lottery_data['list'])): if lottery_data['list'][good]['id'] == good_id: number = lottery_data['list'][good]['number'] + 1 lottery_data['list'][good]['number'] = number break mongo[cls.collect_set].update({'id': lottery_data['id']}, lottery_data) r.lpush(key, phoneNum) return '1' else: return '0'
def _data_deal(cls, args, user): data = [] history_key = cls.history_set + ':' + user['phoneNum'] history_list = r.lrange(history_key, 0, -1) for item in history_list: try: item_data = json.loads(item.replace("'", '"')) if item_data['good_id']: item_data['timestamp'] = time.strftime("%Y-%m-%d", time.localtime(item_data['timestamp'])) static = mongo[cls.static_set].find_one({'item_id': item_data['good_id']}, {'image_url_set': 1, 'short_name': 1, '_id': 1}) dynamic = mongo[cls.dynamic_set].find_one({'item_id': item_data['good_id']}, {'jumei_price': 1, 'market_price': 1, 'buyer_number_text': 1}) if static and dynamic: item_data['image'] = static['image_url_set']['single_many'][0]['800'] item_data['name'] = static['short_name'] item_data['discounted_price'] = dynamic['jumei_price'] item_data['original_price'] = dynamic['market_price'] item_data['buyer_numer'] = dynamic['buyer_number_text'] data.append(item_data) except: pass return {'data_list': data}
def _data_deal(cls, args, user): data = {} good_list = [] history_key = cls.history_set + ':' + user['phoneNum'] history_list = r.lrange(history_key, 0, 5) for item in history_list: item_id = json.loads(item.replace("'", '"'))['good_id'] temp = mongo[cls.category_set].find_one({'item_id': item_id}) category = temp['sub_category_id'] if temp else 0 if category != 0: good_list = good_list + list(mongo[cls.category_set].aggregate( [{ "$match": { 'sub_category_id': category } }, { '$sample': { 'size': 5 } }])) if len(good_list) > 10: # 随机取10个商品 good_list = random.sample(good_list, 10) else: home_good = r.keys(pattern='home_good:*') goods = random.sample(home_good, 10 - len(good_list)) for key in goods: good_list.append(r.hgetall(key)) for good in good_list: good['_id'] = str(good['_id']) for k in good.keys(): if not good[k]: good[k] = '' data[str(good['item_id'])] = good return data