Exemplo n.º 1
0
def create_ktv_order(db, order_id, params):
    order_items = db.query('select * from order_item where order_id=%s', order_id)
    # "sku_properties":"包厢房型:小包;欢唱时间:17点至20点;日期:6月1日(周三)"
    ktv_order_result = []

    if not 'sku_properties' in params:
        return ktv_order_result

    sku = TaobaoSku('', None, 0, 0, 0, 0, 0)
    tmp = re.split(r'[:;]', params.sku_properties)
    sku.parse_taobao_property(tmp[1].encode('utf-8'), tmp[3].encode('utf-8'), tmp[5].encode('utf-8'))

    # 7天内过期
    expire_at = datetime.fromordinal(sku.date.toordinal())
    expire_at = expire_at + timedelta(days=7, seconds=-1)

    for order_item in order_items:
        is_ktv = db.get('select 1 from ktv_product_goods where goods_id=%s', order_item.goods_id)
        if not is_ktv:
            continue
        db.execute('update item_coupon ic, item i set ic.expire_at=%s where ic.item_id=i.id and i.order_item_id=%s',
                   expire_at, order_item.id)
        product_goods = db.get('select kp.shop_id, kp.product_id, ss.name shop_name, ss.manager_mobile '
                               'from ktv_product_goods kp left join supplier_shop ss '
                               'on kp.shop_id = ss.id where kp.goods_id=%s', order_item.goods_id)

        db.execute('insert into ktv_order set created_at=NOW(), deal_at=NOW(), '
                   'room_type=%s, scheduled_day=%s, scheduled_time=%s, status="DEAL",'
                   'goods_id=%s, order_item_id=%s, shop_id=%s, product_id=%s',
                   sku.room_type, sku.date, sku.start_time, order_item.goods_id,
                   order_item.id, product_goods.shop_id, product_goods.product_id)

        ktv_order_params = {
            'shop_name': product_goods.shop_name,
            'manager_mobile': product_goods.manager_mobile,
            'sku': sku
        }
        ktv_order_result.append(ktv_order_params)

    return ktv_order_result
Exemplo 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'))