/** * Created by Tony on 2017/9/14. */ const mongoose = require("mongoose"); let rpt_tpl_tree_mdl = mongoose.model("rpt_tpl_tree"); const SYSTEM_USER_ID = -100; function validate(doc) { let rst = true; if (doc) { if (!(doc.hasOwnProperty("compilationId") && doc.hasOwnProperty("userId"))) { rst = false; } } else { rst = false; } return rst; } async function createNewTree(doc) { if (validate(doc)) { return await rpt_tpl_tree_mdl.create(doc) } else { return null; } } async function updateTree(compilationId, engineerId, userId, doc) { let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]}; return await rpt_tpl_tree_mdl.update(filter, doc); } async function updateTreeInDetail(filter, updateStatement) { return await rpt_tpl_tree_mdl.update(filter, updateStatement); } async function removeTree(compilationId, engineerId, userId, cb) { let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]}; return await rpt_tpl_tree_mdl.findAndModify(filter, [], { $set: { "isDeleted": true } }, {'new':true}, cb); } async function removeTreePhycically(compilationId, engineerId, userId) { let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]}; return await rpt_tpl_tree_mdl.remove(filter); } async function findTplTree(compilationId, userIds) { //let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds}; let filter = {"compilationId": compilationId, "userId": userIds, "$or": [{"isDeleted": null}, {"isDeleted": false}]}; if (userIds instanceof Array) { filter.userId = {$in: userIds}; } return await rpt_tpl_tree_mdl.find(filter); } async function findTplTreeByOid(objectId) { if (objectId) { //let filter = {"_id": ObjectId(objectId)}; let id = mongoose.Types.ObjectId(objectId); let filter = {_id: id}; return await rpt_tpl_tree_mdl.findOne(filter); } else return null; } async function findTplTreeByCompilation(compilationId) { let filter = {"compilationId": compilationId, "$or": [{"isDeleted": null}, {"isDeleted": false}]}; return await rpt_tpl_tree_mdl.find(filter); } let expObj = { createNewTree: createNewTree, updateTree: updateTree, updateTreeInDetail: updateTreeInDetail, removeTree: removeTree, removeTreePhycically: removeTreePhycically, findTplTree: findTplTree, findTplTreeByOid: findTplTreeByOid, findTplTreeByCompilation: findTplTreeByCompilation }; module.exports = expObj;