ration_section_tree.js 3.7 KB

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