'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/1/25
 * @version
 */

import mongoose from "mongoose";
const uuidV1 = require('uuid/v1');
let moment = require("moment");
let progressiveModel = mongoose.model('std_progressive_lib');


let progressiveLib = {
    findByCondition:async function(conditions,options,single=true){
        if(single == true){
            return await progressiveModel.findOne(conditions,options);
        }else {
            return await  progressiveModel.find(conditions,options);
        }
    },
    addLib : async function (data){
        let now = new Date().getTime();
        let dateStr = moment(now).format('YYYY-MM-DD HH:mm:ss');
        let newLib = {
            creator: data.userAccount,
            createDate: now,
            recentOpr: [{operator: data.userAccount, operateDate: dateStr}],
            name: data.name,
        };
        newLib.ID = uuidV1();
        return await progressiveModel.create(newLib);
    },
    saveLib:async function(param) {
        return await progressiveModel.findOneAndUpdate(param.query,param.data,{new:true});
    },
    deleteLibByID:async function(ID){
        return await progressiveModel.deleteOne({ID:ID});
    },
};

export default progressiveLib