| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | /** * Created by Tony on 2017/9/14. */import mongoose from "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("engineerId") && 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, engineerIds, userIds) {    //let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds};    let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds, "$or": [{"isDeleted": null}, {"isDeleted": false}]};    if (engineerIds instanceof Array) {        filter.engineerId = {$in: engineerIds};    }    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;}let expObj = {    createNewTree:          createNewTree,    updateTree:             updateTree,    updateTreeInDetail:     updateTreeInDetail,    removeTree:             removeTree,    removeTreePhycically:   removeTreePhycically,    findTplTree:            findTplTree,    findTplTreeByOid:       findTplTreeByOid};export {expObj as default};
 |