/**
 * Created by zhang on 2018/9/3.
 */
import mongoose from "mongoose";
const uuidV1 = require('uuid/v1');
let moment = require("moment");
let monomerTemplateModel = mongoose.model('std_monomer_template');
let compilationModel = mongoose.model('compilation');


let monomerTemplateLib = {
    findByCondition:async function(conditions,options,single=true){
        if(single == true){
            return await monomerTemplateModel.findOne(conditions,options);
        }else {
            return await  monomerTemplateModel.find(conditions,options);
        }
    },
    addLib : async function (data){
        let now = new Date().getTime();
        let dateStr = moment(now).format('YYYY-MM-DD HH:mm:ss');
        //取编办信息
        let compilation = await compilationModel.findOne({_id:data.compilationId});
        if(compilation){
            let newLib = {
                creator: data.userAccount,
                createDate: now,
                recentOpr: [{operator: data.userAccount, operateDate: dateStr}],
                name: data.name,
                compilationID: data.compilationId,
                compilationName: compilation.name,
            };
            newLib.ID = uuidV1();
            return await monomerTemplateModel.create(newLib);
        }else {
            throw  new Error("没有找到该编办！");
        }
    },
    saveLib:async function(param) {
        console.log('par===============================')
        console.log(param)
        return await monomerTemplateModel.findOneAndUpdate(param.query,param.data);
    },
    deleteLibByID:async function(ID){
        return await monomerTemplateModel.deleteOne({ID:ID});
    },
};

export default monomerTemplateLib