/**
 * Created by zhang on 2018/9/11.
 */

import mongoose from "mongoose";

const uuidV1 = require('uuid/v1');
let moment = require("moment");
let calcProgramModel = mongoose.model("std_calc_programs");
let compilationModel = mongoose.model("compilation");
const _ = require('lodash');
let counter = require('../../../public/counter/counter');

let calcProgramLib = {
    findByCondition:async function(conditions,options,single=true){
        if(single == true){
            return await calcProgramModel.findOne(conditions,options);
        }else {
            return await  calcProgramModel.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 IDcounter = await counter.counterDAO.getIDAfterCount(counter.moduleName.stdCalcProgram,1);
            let newLib = {
                creator: data.userAccount,
                createDate: now,
                recentOpr: [{operator: data.userAccount, operateDate: dateStr}],
                libName: data.name,
                displayName:data.displayName,
                compilationId: data.compilationId,
                compilationName: compilation.name,
                templates:[]
            };
            newLib.ID = IDcounter.sequence_value;
            return await calcProgramModel.create(newLib);
        }else {
            throw  new Error("没有找到该编办！");
        }

    },
    saveLib:async function(param) {
        return await calcProgramModel.findOneAndUpdate(param.query,param.data,{new:true});
    },
    deleteLibByID:async function(ID){
        return await calcProgramModel.deleteOne({ID:ID});
    },
};

export default calcProgramLib