ration_section_tree.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. explanation: String,//说明
  16. ruleText: String,//计算规则
  17. isDeleted: Boolean
  18. });
  19. var rationChapterTreeModel = chapterTreeDb.model("std_ration_lib_ration_chapter_trees", rationChapterTreeSchema, "std_ration_lib_ration_chapter_trees");
  20. var counter = require('../../../public/counter/counter');
  21. var rationChapterTreeDAO = function(){};
  22. rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
  23. rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  24. if(data.length) callback(false,data);
  25. else if(err) callback("获取定额树错误!",false)
  26. else callback(false,false);
  27. })
  28. }
  29. rationChapterTreeDAO.prototype.getRationChapterTreeByRepId = function(repId,callback){
  30. rationChapterTreeModel.find({"rationRepId": repId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  31. if(data.length) callback(false,data);
  32. else if(err) callback("获取定额树错误!",false)
  33. else callback(false,false);
  34. })
  35. }
  36. rationChapterTreeDAO.prototype.createNewNode = function(libId, lastNodeId, nodeData,callback){
  37. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  38. nodeData.rationRepId = libId;
  39. nodeData.ID = result.value.sequence_value;
  40. var node = new rationChapterTreeModel(nodeData);
  41. node.save(function (err, result) {
  42. if (err) {
  43. callback("章节树ID错误!", false);
  44. } else {
  45. if (lastNodeId > 0) {
  46. rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  47. if (err) {
  48. callback("章节树ID错误!", false);
  49. } else {
  50. callback(false, result);
  51. }
  52. });
  53. } else callback(false, result);
  54. }
  55. });
  56. });
  57. },
  58. rationChapterTreeDAO.prototype.removeNodes = function(nodeIds, preNodeId, preNodeNextId, callback){
  59. var functions = [];
  60. if (preNodeId != -1) {
  61. functions.push((function(nodeId, nextId) {
  62. return function(cb) {
  63. rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  64. };
  65. })(preNodeId, preNodeNextId));
  66. }
  67. for (var i=0; i < nodeIds.length; i++) {
  68. functions.push((function(nodeId) {
  69. return function(cb) {
  70. rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  71. };
  72. })(nodeIds[i]));
  73. }
  74. async.parallel(functions, function(err, results) {
  75. callback(err, results);
  76. });
  77. }
  78. rationChapterTreeDAO.prototype.updateNodes = function(nodes,callback){
  79. var functions = [];
  80. for (var i=0; i < nodes.length; i++) {
  81. //var md = new rationChapterTreeModel(nodes[i]);
  82. //md.isNew = false;
  83. functions.push((function(doc) {
  84. return function(cb) {
  85. rationChapterTreeModel.update({ID: doc.ID}, doc, cb);
  86. };
  87. })(nodes[i]));
  88. }
  89. async.parallel(functions, function(err, results) {
  90. callback(err, results);
  91. });
  92. };
  93. module.exports = new rationChapterTreeDAO()