Beispiel #1
0
    def _get_tips(self, userid, customer_id):
        limit = 3
        all_sales = SpecialApi.get_all_sales() or []

        # 当前店铺创建的特卖
        consumed_sales = [sale for sale in all_sales
                               if str(sale['qf_uid']) == userid ]
        if consumed_sales:
            return {'title' : config.CONSUMED_SALE_TITLE,
                    'sales' : SpecialApi.tidy_sales(consumed_sales, 'consumed')[:limit]}

        # 置顶特卖
        head_sales = SpecialApi.get_head_sales() or []

        # 附近特卖
        user = apcli_ex('findUserByid', int(userid))
        near_sales = []
        if user and user.longitude and user.latitude:
            near_sales = SpecialApi.tidy_sales(all_sales,
                    mode='near', lng=user.longitude, lat=user.latitude) or []
            near_sales.sort(key=lambda x: x['dist'])

        sales = head_sales + near_sales
        if sales:
            return {'title' : config.NEAR_SALE_TITLE,
                    'sales' : sales[:3]}
        else:
            return {'title' : '', 'sales' : []}
Beispiel #2
0
    def _list(self, userid, page=0, pagesize=10):
        actvs = []
        with get_connection('qmm_wx') as db:
            where = {
                'qf_uid' : int(userid),
                'status' : ('!=', SpecialDefine.STATUS_DELETED),
                'atype' : 1
            }
            fields = ('id, title, daily_quantity, quantity, img, status, audit_status, price, create_time,'
                'business_title,redeem_start_date, redeem_end_date, redeem_start_time, redeem_end_time, remark')
            other = 'order by create_time desc limit %s offset %s' % (pagesize, page*pagesize)
            actvs = db.select('market_activity', where=where,
                               fields = fields, other=other)
        if not actvs: return []

        ids = [i['id'] for i in actvs]
        # 兑换数量
        sales = SpecialApi.get_actv_sales(ids)
        # 查看信息
        query_infos = SpecialApi.get_actv_pv(ids)

        ret = []
        online_time = '2016-10-28 00:00:00'
        for i in actvs:
            tmp = {}
            tmp['img'] = i['img']
            tmp['shopname'] = i['business_title']
            tmp['activity_id'] = i['id']
            tmp['goods_title'] = i['title']
            tmp['status'] = SpecialApi.get_actv_status(i) # 通知状态
            tmp['state'] = SpecialApi.get_actv_state(i) # 活动状态
            tmp['buy_count'] = int(sales.get(i['id']) or 0) # 兑换数量
            tmp['total_count'] = i['daily_quantity'] or i['quantity'] # 总数量
            if i['daily_quantity']:
                tmp['sales_count'] = tmp['total_count'] - i['quantity'] # 购买数量
            tmp['payment_count'] = i['price'] * tmp['buy_count']
            tmp['redeem_start_date'] = str(i['redeem_start_date'])[5:]
            tmp['redeem_start_time'] = str(i['redeem_start_time'])
            tmp['redeem_end_date'] = str(i['redeem_end_date'])[5:]
            tmp['redeem_end_time'] = str(i['redeem_end_time'])
            tmp['notify_datetime'] = str(i['redeem_start_date']) + ' 11:00'
            tmp['query_info'] = query_infos.get(i['id'])
            if str(i['create_time']) >= online_time:
                tmp['total_query'] = sum([t['count'] for t in tmp['query_info']])
            tmp['audit_info'] = i['remark'] or ''
            ret.append(tmp)

        return ret
Beispiel #3
0
    def POST(self):
        userid = self.user.userid
        if not SpecialApi.check_allow_create(userid):
            raise ParamError('禁止创建特卖通知!')

        args = self.req.input()
        # 参数
        params = {i : args.get(i) for i in ['title', 'descr', 'redeem_start_date',
                                            'redeem_end_date', 'img'] }

        params['redeem_start_time'] = args.get('redeem_start_time', "05:00")
        params['redeem_end_time'] = args.get('redeem_end_time', "23:00")
        params['quantity'] = int(args['quantity'])

        # 价格
        price, origin_price = int(args['price']), int(args['origin_price'])
        price_limit = int(redis_pool.get('_mchnt_api_sale_price_limit_') or 70000)
        if price > price_limit:
            raise ParamError('创建特卖失败')
        if price > origin_price * 0.8:
            raise ParamError('至少8折才有吸引力')
        params['price'] = price
        params['origin_price'] = origin_price

        result = self.create(**params)

        return self.write(success({'activity_id' : result}))
Beispiel #4
0
    def GET(self):
        sales = []
        d = {k:v.strip() for k, v in self.req.input().iteritems()}
        lng, lat = d.get('longitude'), d.get('latitude')
        limit = int(d.get('limit') or 100)

        if is_valid_num(lng) and is_valid_num(lat):
            all_sales = SpecialApi.get_all_sales()
            sales = SpecialApi.tidy_sales(all_sales,
                mode = 'near', lng = float(lng), lat = float(lat)) or []
            sales.sort(key=lambda x: x.get('dist'))

        ret = SpecialApi.get_head_sales() + sales[:limit]

        # 增加pv
        SpecialApi.add_actv_pv([i['activity_id'] for i in ret], d.get('query_from'))

        return self.write(success(ret))
Beispiel #5
0
    def _trans_input(self):
        d = {k:v.strip() for k, v in self.req.input().iteritems()}
        userid = int(self.user.userid)
        # 若在黑名单
        if redis_pool.sismember('_mchnt_api_sale_limit_userids_', userid):
            raise ParamError('修改特卖活动失败')

        actv, actv_id = None, d.get('id') or ''
        with get_connection('qmm_wx') as db:
            where = {'qf_uid' : userid, 'id' : actv_id, 'atype' : SpecialDefine.ATYPE_SALE}
            actv = db.select_one('market_activity', where=where)
        # 若活动不存在
        if not actv:
            raise ParamError('活动不存在')

        # 审核状态
        state = SpecialApi.get_actv_state(actv)

        # 进行中
        if  state == SpecialDefine.STATE_ON:
            data = {i:d.get(i, '')for i in ['descr', 'img', 'quantity', 'redeem_start_time', 'redeem_end_time']}
            data['buyable_end_time'] = d['redeem_end_time']
            data['online_time'] = str(actv['redeem_start_date']) + ' ' + data['redeem_start_time']
            data['offline_time'] = str(actv['redeem_end_date']) + ' ' + data['redeem_end_time']

        # 审核失败
        elif state == SpecialDefine.STATE_REJECT:
            data = {i:d.get(i, '') for i in ['descr', 'img', 'quantity', 'title', 'price', 'origin_price']}
            if not is_valid_int(data['price']) and not is_valid_int(data['origin_price']):
                raise ParamError('价格必须为整数')
            # 价格限制
            price_limit = int(redis_pool.get('_mchnt_api_sale_price_limit_') or 70000)
            if int(data['price']) > price_limit:
                raise ParamError('修改特卖活动失败')

            data['redeem_start_date'] = d.get('redeem_start_date')
            data['redeem_end_date'] = d.get('redeem_end_date')
            data['redeem_start_time'] = d.get('redeem_start_time', "05:00")
            data['redeem_end_time'] = d.get('redeem_end_time', "23:00")
            data['buyable_start_date'] = time.strftime('%Y-%m-%d')
            data['buyable_end_date'] = d.get('redeem_end_date')
            data['buyable_start_time'] = time.strftime('%H:%M:%S')
            data['buyable_end_time'] = d['redeem_end_time']
            data['online_time'] = data['redeem_start_date'] + ' ' + data['redeem_start_time']
            data['offline_time'] = data['redeem_end_date'] + ' ' + data['redeem_end_time']
        else:
            raise ParamError('该活动不允许修改')

        if not is_valid_int(data['quantity']):
            raise ParamError('库存必须为整数')

        data['quantity'] = max(data['quantity'], 0)
        data['daily_quantity']  =  actv['daily_quantity'] or actv['quantity']
        data['daily_quantity'] += int(data['quantity']) - actv['quantity']
        data['audit_status'] = SpecialDefine.AUDIT_STATUS_PLACED

        return {'actv_id' : actv['id'], 'data' : data}
Beispiel #6
0
    def GET(self):
        d = self._trans_input()
        ret = {'notify_list' : [], 'total_count' : 0}

        userids = []
        with get_connection('qf_mchnt') as db:
            consumed_shops = db.select(
                        table = 'member',
                        where = {
                            'customer_id' : d['customer_id']
                        }, fields='userid') or []

            userids = [i['userid'] for i in consumed_shops]

        if userids:
            all_sales = SpecialApi.get_all_sales() or []
            consumed_sales = [sale for sale in all_sales if sale['qf_uid'] in userids]
            ret['notify_list'] = SpecialApi.tidy_sales(consumed_sales,
                    mode = 'consumed', lng = d['lng'], lat = d['lat'])
            ret['total_count'] = len(ret['notify_list'])

        return self.write(success(ret))
Beispiel #7
0
    def GET(self):
        d = {k:v.strip() for k, v in self.req.input().iteritems()}
        userid = self.user.ses.get('userid')
        page = int(d.get('page', 0))
        pagesize = int(d.get('pagesize', 10))

        ret = {}
        # 是否能够创建特卖活动
        ret['allow_create'] = SpecialApi.check_allow_create(userid)
        # 获取特卖活动列表
        ret['activity_list'] = self._list(userid, page, pagesize)
        # 当前时间挫
        ret['timestamp'] = int(time.time())

        return self.write(success(ret))
Beispiel #8
0
    def _summary(self, userid):
        actvs = []
        with get_connection('qmm_wx') as db:
            where = {
                'qf_uid' : int(userid),
                'status' : ('!=', SpecialDefine.STATUS_DELETED),
                'atype' : 1
            }
            fields = 'id, price'
            actvs = db.select('market_activity', where=where, fields=fields)
        if not actvs: return {'tx_count':0, 'payment_count':0}

        sales = SpecialApi.get_actv_sales([i['id'] for i in actvs])
        tx_count = sum(sales.values())
        payment_count = sum(sales[i['id']] * i['price'] for i in actvs)

        return {'tx_count':tx_count, 'payment_count':payment_count}