rpt_tpl_tree_node_facade.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("engineerId") && 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};
  27. return await rpt_tpl_tree_mdl.update(filter, doc);
  28. }
  29. async function removeTree(compilationId, engineerId, userId, cb) {
  30. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId};
  31. return await rpt_tpl_tree_mdl.findAndModify(filter, [], { $set: { "isDeleted": true } }, {'new':true}, cb);
  32. }
  33. async function removeTreePhycically(compilationId, engineerId, userId) {
  34. let filter = {"compilationId": compilationId, "engineerId": engineerId, "userId": userId};
  35. return await rpt_tpl_tree_mdl.remove(filter);
  36. }
  37. async function findTplTree(compilationId, engineerIds, userIds) {
  38. //let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds};
  39. let filter = {"compilationId": compilationId, "engineerId": engineerIds, "userId": userIds, "$or": [{"isDeleted": null}, {"isDeleted": false}]};
  40. if (engineerIds instanceof Array) {
  41. filter.engineerId = {$in: engineerIds};
  42. }
  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. let expObj = {
  57. createNewTree: createNewTree,
  58. updateTree: updateTree,
  59. removeTree: removeTree,
  60. removeTreePhycically: removeTreePhycically,
  61. findTplTree: findTplTree,
  62. findTplTreeByOid: findTplTreeByOid
  63. };
  64. export {expObj as default};