ration_section_tree.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Created by Tony on 2017/4/21.
  3. */
  4. var mongoose = require("mongoose");
  5. var async = require("async");
  6. var rationChapterTreeModel = mongoose.model("std_ration_lib_ration_chapter_trees");
  7. var counter = require('../../../public/counter/counter');
  8. var rationChapterTreeDAO = function(){};
  9. rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
  10. rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  11. if(data.length) callback(false,data);
  12. else if(err) callback("获取定额树错误!",false)
  13. else callback(false,false);
  14. })
  15. }
  16. rationChapterTreeDAO.prototype.getRationChapterTreeByRepId = function(repId,callback){
  17. rationChapterTreeModel.find({"rationRepId": repId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  18. if(data.length) callback(false,data);
  19. else if(err) callback("获取定额树错误!",false)
  20. else callback(false,false);
  21. })
  22. }
  23. rationChapterTreeDAO.prototype.createNewNode = function(libId, lastNodeId, nodeData,callback){
  24. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  25. nodeData.rationRepId = libId;
  26. nodeData.ID = result.value.sequence_value;
  27. var node = new rationChapterTreeModel(nodeData);
  28. node.save(function (err, result) {
  29. if (err) {
  30. callback("章节树ID错误!", false);
  31. } else {
  32. if (lastNodeId > 0) {
  33. rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  34. if (err) {
  35. callback("章节树ID错误!", false);
  36. } else {
  37. callback(false, result);
  38. }
  39. });
  40. } else callback(false, result);
  41. }
  42. });
  43. });
  44. },
  45. rationChapterTreeDAO.prototype.removeNodes = function(nodeIds, preNodeId, preNodeNextId, callback){
  46. var functions = [];
  47. if (preNodeId != -1) {
  48. functions.push((function(nodeId, nextId) {
  49. return function(cb) {
  50. rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  51. };
  52. })(preNodeId, preNodeNextId));
  53. }
  54. for (var i=0; i < nodeIds.length; i++) {
  55. functions.push((function(nodeId) {
  56. return function(cb) {
  57. rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  58. };
  59. })(nodeIds[i]));
  60. }
  61. async.parallel(functions, function(err, results) {
  62. callback(err, results);
  63. });
  64. }
  65. rationChapterTreeDAO.prototype.updateNodes = function(nodes,callback){
  66. var functions = [];
  67. for (var i=0; i < nodes.length; i++) {
  68. //var md = new rationChapterTreeModel(nodes[i]);
  69. //md.isNew = false;
  70. functions.push((function(doc) {
  71. return function(cb) {
  72. rationChapterTreeModel.update({ID: doc.ID}, doc, cb);
  73. };
  74. })(nodes[i]));
  75. }
  76. async.parallel(functions, function(err, results) {
  77. callback(err, results);
  78. });
  79. };
  80. rationChapterTreeDAO.prototype.getRationChapter = function (repId, chapterID, callback) {
  81. if (callback) {
  82. rationChapterTreeModel.findOne({rationRepId: repId, ID: chapterID}, '-_id').exec()
  83. .then(function (result, err) {
  84. if (err) {
  85. callback(1, '找不到定额章节' , null);
  86. } else {
  87. callback(0, '', result);
  88. }
  89. });
  90. return null;
  91. } else {
  92. return rationChapterTreeModel.findOne({rationRepId: repId, ID: chapterID}, '-_id').exec();
  93. }
  94. }
  95. module.exports = {
  96. Model: rationChapterTreeModel,
  97. Dao: new rationChapterTreeDAO()
  98. };
  99. //module.exports = new rationChapterTreeDAO()