rpt_tpl_tree_node_facade.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Created by Tony on 2017/9/14.
  3. */
  4. const mongoose = require("mongoose");
  5. let rpt_tpl_tree_mdl = mongoose.model("rpt_tpl_tree");
  6. const SYSTEM_USER_ID = -100;
  7. function validate(doc) {
  8. let rst = true;
  9. if (doc) {
  10. if (!(doc.hasOwnProperty("compilationId") && doc.hasOwnProperty("userId"))) {
  11. rst = false;
  12. }
  13. } else {
  14. rst = false;
  15. }
  16. return rst;
  17. }
  18. async function createNewTree(doc) {
  19. if (validate(doc)) {
  20. return await rpt_tpl_tree_mdl.create(doc)
  21. } else {
  22. return null;
  23. }
  24. }
  25. async function updateTree(compilationId, engineerId, userId, doc) {
  26. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  27. return await rpt_tpl_tree_mdl.update(filter, doc);
  28. }
  29. async function updateTreeInDetail(filter, updateStatement) {
  30. return await rpt_tpl_tree_mdl.update(filter, updateStatement);
  31. }
  32. async function removeTree(compilationId, engineerId, userId, cb) {
  33. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  34. return await rpt_tpl_tree_mdl.findAndModify(filter, [], { $set: { "isDeleted": true } }, {'new':true}, cb);
  35. }
  36. async function removeTreePhycically(compilationId, engineerId, userId) {
  37. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  38. return await rpt_tpl_tree_mdl.remove(filter);
  39. }
  40. async function findTplTree(compilationId, userIds) {
  41. //let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds};
  42. let filter = {"compilationId": compilationId, "userId": userIds, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  43. if (userIds instanceof Array) {
  44. filter.userId = {$in: userIds};
  45. }
  46. return await rpt_tpl_tree_mdl.find(filter);
  47. }
  48. async function findTplTreeByOid(objectId) {
  49. if (objectId) {
  50. //let filter = {"_id": ObjectId(objectId)};
  51. let id = mongoose.Types.ObjectId(objectId);
  52. let filter = {_id: id};
  53. return await rpt_tpl_tree_mdl.findOne(filter);
  54. } else return null;
  55. }
  56. async function findTplTreeByCompilation(compilationId) {
  57. let filter = {"compilationId": compilationId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  58. return await rpt_tpl_tree_mdl.find(filter);
  59. }
  60. let expObj = {
  61. createNewTree: createNewTree,
  62. updateTree: updateTree,
  63. updateTreeInDetail: updateTreeInDetail,
  64. removeTree: removeTree,
  65. removeTreePhycically: removeTreePhycically,
  66. findTplTree: findTplTree,
  67. findTplTreeByOid: findTplTreeByOid,
  68. findTplTreeByCompilation: findTplTreeByCompilation
  69. };
  70. module.exports = expObj;