Esempio n. 1
0
def express_auto_push(db, redis, message_raw, distr_shop_id):
    express_info = json.loads(message_raw, object_hook=json_hook)
    logging.info('taobao_express_update consumer pop taobao tid =%s ' %
                 express_info.tid)
    app_info = json.loads(db.get(
        'select taobao_api_info from distributor_shop where id = %s',
        distr_shop_id).taobao_api_info,
                          object_hook=json_hook)
    express_company = Taobao('taobao.logistics.offline.send')
    express_company.set_app_info(app_info.app_key, app_info.app_secret_key)
    express_company.set_session(app_info.session)

    response = express_company.sync_fetch(**express_info)
    express_company.parse_response(response)

    if express_company.is_ok():
        logging.info('outer order express info update success. order %s',
                     express_info.tid)
        redis.lrem(options.queue_taobao_express_processing, 0, express_info)
    else:
        logging.error('outer order express info update failure. order %s',
                      express_info.tid)
Esempio n. 2
0
def sync(db, products):
    for product in products:
        if not product.taobao_api_info:
            logging.error('sync_ktv_sku: invalid taobao_api_info. product_id: %s', product.product_id)
            continue
        taobao_api_info = json.loads(product.taobao_api_info, object_hook=json_hook)

        iid = product.distributor_goods_id
        taobao = Taobao('taobao.item.get')
        taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key)
        response = taobao.sync_fetch(fields='sku', num_iid=iid)
        taobao.parse_response(response)
        if not taobao.is_ok():
            logging.error('sync_ktv_sku: get taobao item info failed. goods_distributor_shop_id: %s', product.gds_id)
            if taobao.error.sub_code == 'isv.item-get-service-error:ITEM_NOT_FOUND':
                #  商品在淘宝不存在,于是在我们系统中设置为逻辑删除
                logging.error('sync_ktv_sku: product not exist. goods_distributor_shop_id: %s', product.gds_id)
                db.execute('update goods_distributor_shop set deleted=1 where id=%s', product.gds_id)
            continue

        local_sku_list = build_taobao_sku(db, product.shop_id, product.product_id)
        remote_sku_list = []
        invalid_skus = []
        if 'skus' in taobao.message.item and 'sku' in taobao.message.item.skus:
            for sku in taobao.message.item.skus.sku:
                tmp = sku_split_re.split(sku.properties_name)
                # 例: 27426219:3442354:包厢房型:小包;-1:-1:欢唱时间:17点至20点;-2:-45:日期:12月11日(周三)
                tb_sku = TaobaoSku(
                    room_type='',
                    dt=None,
                    price=Decimal(sku.price),
                    quantity=sku.quantity,
                    start_time=0,
                    duration=0,
                    sku_id=sku.sku_id
                )
                if len(tmp) != 12:
                    invalid_skus.append(tb_sku)
                    continue
                tb_sku.parse_taobao_property(tmp[3].encode('utf-8'), tmp[7].encode('utf-8'), tmp[11].encode('utf-8'))
                remote_sku_list.append(tb_sku)

        add_sku_list, delete_sku_list, update_price_sku_list, update_quantity_sku_list = diff_local_and_remote(
            local_sku_list, remote_sku_list
        )
        delete_sku_list.extend(invalid_skus)

        remote_price_set = set([sku.price for sku in remote_sku_list])

        for sku in delete_sku_list:
            if sku.price in remote_price_set:
                remote_price_set.remove(sku.price)
            taobao = Taobao('taobao.item.sku.delete')
            taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key)
            taobao.set_session(taobao_api_info.session)
            response = taobao.sync_fetch(
                num_iid=iid,
                properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date),
                item_price=str(min(remote_price_set) if remote_price_set else sku.price)
            )
            taobao.parse_response(response)
            logging.info('sync_ktv_sku: %s DELETE[%s] %s %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL',
                         sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8'))

        for sku in add_sku_list:
            remote_price_set.add(sku.price)
            taobao = Taobao('taobao.item.sku.add')
            taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key)
            taobao.set_session(taobao_api_info.session)

            end = sku.start_time + sku.duration
            end = end - 24 if end >= 24 else end
            response = taobao.sync_fetch(
                num_iid=iid,
                properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date),
                quantity=sku.quantity,
                price=sku.price,
                outer_id=sku.room_type+sku.date.strftime('%Y%m%d')+str(sku.start_time*100+end),
                item_price=str(min(remote_price_set))
            )
            taobao.parse_response(response)
            logging.info('sync_ktv_sku: %s ADD[%s] %s %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL',
                         sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8'))

        if update_quantity_sku_list:
            taobao = Taobao('taobao.skus.quantity.update')
            taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key)
            taobao.set_session(taobao_api_info.session)
            response = taobao.sync_fetch(
                num_iid=iid,
                skuid_quantities=';'.join(['%s:%s' % (sku.sku_id, sku.quantity) for sku in update_quantity_sku_list])
            )
            taobao.parse_response(response)
            logging.info('sync_ktv_sku: UPDATE_QUANTITY[%s] %s', 'OK' if taobao.is_ok() else 'FAIL',
                         taobao.get_field('skuid_quantities'))

        for sku in update_price_sku_list:
            taobao = Taobao('taobao.item.sku.price.update')
            taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key)
            taobao.set_session(taobao_api_info.session)
            response = taobao.sync_fetch(
                num_iid=iid,
                properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date),
                quantity=sku.quantity,
                price=sku.price,
                item_price=sku.price,  # 此处不好确定此时的该商品所有SKU的最低价格,所以就填了当前价格
            )
            taobao.parse_response(response)
            logging.info('sync_ktv_sku: %s UPDATE_PRICE[%s] %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL',
                         sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8'))
Esempio n. 3
0
def taobao_order(db, redis, distributor_order_id, message_raw):
    """
    处理淘宝的分销订单队列
    :param db
    :param redis
    :param distributor_order_id: 分销订单id
    :param message_raw: redis 队列元素值
    :type db:torndb.Connection
    :type redis:redis.client.StrictRedis
    :type distributor_order_id: int
    """

    distributor_order = db.get('select * from distributor_order where id=%s', distributor_order_id)
    params = json.loads(distributor_order.message, object_hook=json_hook)
    shop = db.get('select * from distributor_shop where taobao_seller_id=%s', params.taobao_sid)
    api_info = json.loads(shop.taobao_api_info, object_hook=json_hook)
    if not distributor_order.order_id:
        # 如果还没生成一百券订单
        goods_link_id = int(params.outer_iid)
        # 找到关联的商品
        goods_info = db.get('select g.* from goods g, goods_distributor_shop gds '
                            'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s',
                            shop.id, goods_link_id)
        if not goods_info and goods_link_id < 20000:
            goods_info = db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id)

        if not goods_info:
            logging.error('taobao order consume failed: goods not found. link_id: %s', goods_link_id)
            return

        taobao = Taobao('taobao.trade.get')
        taobao.set_app_info(api_info.app_key, api_info.app_secret_key)
        taobao.set_session(api_info.session)
        response = taobao.sync_fetch(tid=distributor_order.order_no,
                                     fields='total_fee,payment,orders.payment,orders.num,'
                                            'orders.sku_properties_name,orders.price')
        taobao.parse_response(response)

        order_payment = Decimal(taobao.message.trade.payment)
        # 创建订单
        order_id, order_no = new_distributor_order(db, shop.id, Decimal(taobao.message.trade.total_fee),
                                                   order_payment, params.mobile)
        result = new_distributor_item(db, order_id, order_no, order_payment / int(params.num),
                                      int(params.num), goods_info, params.mobile, shop.id, params.num_iid, None, False)

        if not result.ok:
            logging.error('taobao order consume failed. %s', result.msg)
            return
        else:
            db.execute('update orders set distributor_order_id=%s where id = %s',
                       distributor_order_id, result.order_id)
            db.execute('update distributor_order set order_id=%s where id=%s',
                       result.order_id, distributor_order_id)
        ktv_order_result = create_ktv_order(db, order_id, params)
    else:
        order_id = distributor_order.order_id
        ktv_order_result = []

    coupons = db.query('select c.sn as coupon_sn from item i, item_coupon c where i.id=c.item_id and i.order_id=%s',
                       order_id)

    # 告诉淘宝我们已发货
    taobao = Taobao('taobao.vmarket.eticket.send')
    taobao.set_app_info(api_info.app_key, api_info.app_secret_key)
    if api_info.app_key == options.taobao_kunran_app_key:  # 如果是码商,要加上码商的信息
        taobao.add_field('codemerchant_id', options.taobao_kunran_id)
        taobao.set_session(api_info.merchant_session)
    else:
        taobao.set_session(api_info.session)
    response = taobao.sync_fetch(order_id=params.order_id, token=params.token,
                                 verify_codes=','.join(['%s:1' % item.coupon_sn for item in coupons]))
    logging.info('tell taobao coupon send response: %s', response)
    taobao.parse_response(response)

    if taobao.is_ok():
        logging.info('taobao order complete. distributor_order_id: %s', distributor_order_id)
        all_order_items = db.query('select * from order_item where order_id=%s', order_id)
        for item in all_order_items:
            CouponSMSMessage(db, redis, order_item=item).remark('淘宝订单短信发送').send()
        redis.lrem(options.queue_distributor_order_processing, 0, message_raw)
        #只要ktv预订时间有,就给门店经理发送ktv预订的包厢信息
        for ktv_info in ktv_order_result:
            sku = ktv_info['sku']
            phone_numbers = ktv_info['manager_mobile']
            for phone in (phone_numbers.split(',') if phone_numbers else []):
                content = str(sku.date) + ktv_info['shop_name'] + '预订【' + str(params.mobile) + \
                    sku.room_name + " (" + str(params.num) + "间)" + sku.human_time_range + "】"
                #给经理发送短信告知预订信息
                logging.info('send message to manager,phone:%s,content:%s', phone, content)
                SMSMessage(content, phone).send(redis)
    else:
        logging.error('tell taobao coupon send failed: %s', taobao.error)