import { dataModels } from "./models/modelInit";



export async function findToPage(tableEnumValue:string, param:object, files:object, pageNumber:number, pageSize?) {
    pageSize = pageSize || 10;//默认每页10个
    
    let data = await dataModels[tableEnumValue].find(param, files).skip((pageNumber - 1) *pageSize).limit(pageSize);
    
    return data;
}

export async function findToSortToPage(tableEnumValue:string, param:object, files:object, sortParam, pageNumber:number, pageSize?) {
    pageSize = pageSize || 10;//默认每页10个
    sortParam["_id"] = 1;
    let data = await dataModels[tableEnumValue].find(param, files).sort(sortParam).skip((pageNumber - 1) *pageSize).limit(pageSize);
    
    return data;
}

export async function findCount(tableEnumValue:string, param:object) {
    let data = await dataModels[tableEnumValue].findCount(param);
    return data;
}

export async function findOnce(tableEnumValue:string, param:object, files?) {
    let data = await dataModels[tableEnumValue].findOne(param, files);
    return data;
}

export async function findOnceToSort(tableEnumValue:string, param:object, sortParam, files?) {
    let data = await dataModels[tableEnumValue].findOne(param, files).sort(sortParam);
    return data;
}

export async function find(tableEnumValue:string, param, files?) {
    files = files || [];
    let data = await dataModels[tableEnumValue].find(param, files);
    return data;
}

export async function findToSort(tableEnumValue:string, param, sortParam, files?) {
    files = files || [];
    return await dataModels[tableEnumValue].find(param, files).sort(sortParam);
}


/**
 * 统计数据
 * @param tableEnumValue 
 * @param param 
 * @param group 
 * @returns 
 */
export async function statsData(tableEnumValue:string, param, group) {
    let dataList = await dataModels[tableEnumValue].aggregate([
        {"$match":param},
        {"$group":group}
    ]);

    return dataList;
}