import { ResGetList } from "../shared/protocols/item/PtlGetList";

// let a = await getItemList('0_64e7024f1b2a9acc9e6900fb');
// console.log('a',a)

// let b = await getItemByItemId('0_64e7024f1b2a9acc9e6900fb',['27382','12']);
// console.log('b',b)

// let b1 = await getItemByItemId('0_64e7024f1b2a9acc9e6900fb','12');
// console.log('b1',b1)

// let c = await getItemNum('0_64e7024f1b2a9acc9e6900fb',['27382','12']);
// console.log('c',c)




/**
 * 从DB中获取玩家的物品数据
 * @param uid 玩家uid
 * @param itemIds itemid，可选
 * @returns 
 */
async function _getItemListFromDB(uid:string, itemIds?: string[], projection?: any) {
    let list: ResGetList['list'] = {};

    let where = {
        'uid': uid ,'num':{$ne:0}
    };
    if(itemIds?.length > 0){
        where['itemId'] = {$in: itemIds};
    }

    let arrList = await G.mongodb.collection('item').find(where,{
        //字段筛选
        projection: projection
    }).toArray();

    arrList.forEach(v => {
        list[v.itemId] = G.mongodb.conversionIdObj(v);
    });

    return list;
}


/**
 * 获取玩家的物品列表
 * @param uid 
 * @returns 
 */
export async function getItemList(uid:string) {
    return await _getItemListFromDB(uid);
}

/**
 * 获取指定玩家的指定itemid的物品信息
 * @param uid 
 * @param itemId 
 * @returns 
 */
export async function getItemByItemId(uid:string, itemId:string | string[]) {
    if (typeof itemId ==='string') {
        itemId = [itemId];
    }
    return await _getItemListFromDB(uid, itemId);
}

/**
 * 获取指定物品数量
 * @param uid 
 * @param itemId 
 * @returns 
 */
export async function getItemNum(uid:string, itemId:string | string[]){
    if (typeof itemId ==='string') {
        itemId = [itemId];
    }
    let list = await _getItemListFromDB(uid, itemId,{
        'num':1,
        'itemId':1
    });
    return list;
}