rpt_tpl_tree_node_facade.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Created by Tony on 2017/9/14.
  3. */
  4. import mongoose from "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. let filter = {"compilationId": compilationId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  28. return await rpt_tpl_tree_mdl.update(filter, doc);
  29. }
  30. async function updateTreeInDetail(filter, updateStatement) {
  31. return await rpt_tpl_tree_mdl.update(filter, updateStatement);
  32. }
  33. async function removeTree(compilationId, engineerId, userId, cb) {
  34. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  35. return await rpt_tpl_tree_mdl.findAndModify(filter, [], { $set: { "isDeleted": true } }, {'new':true}, cb);
  36. }
  37. async function removeTreePhycically(compilationId, engineerId, userId) {
  38. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  39. return await rpt_tpl_tree_mdl.remove(filter);
  40. }
  41. async function findTplTree(compilationId, userIds) {
  42. //let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds};
  43. let filter = {"compilationId": compilationId, "userId": userIds, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  44. if (userIds instanceof Array) {
  45. filter.userId = {$in: userIds};
  46. }
  47. return await rpt_tpl_tree_mdl.find(filter);
  48. }
  49. async function findTplTreeByOid(objectId) {
  50. if (objectId) {
  51. //let filter = {"_id": ObjectId(objectId)};
  52. let id = mongoose.Types.ObjectId(objectId);
  53. let filter = {_id: id};
  54. return await rpt_tpl_tree_mdl.findOne(filter);
  55. } else return null;
  56. }
  57. async function findTplTreeByCompilation(compilationId) {
  58. let filter = {"compilationId": compilationId, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  59. return await rpt_tpl_tree_mdl.find(filter);
  60. }
  61. let expObj = {
  62. createNewTree: createNewTree,
  63. updateTree: updateTree,
  64. updateTreeInDetail: updateTreeInDetail,
  65. removeTree: removeTree,
  66. removeTreePhycically: removeTreePhycically,
  67. findTplTree: findTplTree,
  68. findTplTreeByOid: findTplTreeByOid,
  69. findTplTreeByCompilation: findTplTreeByCompilation
  70. };
  71. export {expObj as default};