ration_section_tree.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. let rationRepositoryDao = require('./repository_map');
  10. let moment = require('moment');
  11. var rationChapterTreeSchema = new Schema({//章节树 //生成唯一id改为sectionID 改成string
  12. rationRepId: Number,
  13. ID:Number,
  14. ParentID:Number,
  15. NextSiblingID:Number,
  16. name: 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(lastOpr, 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. async.parallel([
  42. function (cb) {
  43. node.save(function (err, result) {
  44. if (err) {
  45. cb("章节树ID错误!", false);
  46. } else {
  47. if (lastNodeId > 0) {
  48. rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  49. if (err) {
  50. cb("章节树ID错误!", false);
  51. } else {
  52. cb(false, result);
  53. }
  54. });
  55. } else cb(false, result);
  56. }
  57. });
  58. },
  59. function (cb) {
  60. rationRepositoryDao.updateOprArr({ID: libId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  61. if(err){
  62. cb(err);
  63. }
  64. else{
  65. cb(null);
  66. }
  67. });
  68. }
  69. ], function (err, result) {
  70. if(err){
  71. callback("章节树ID错误!", false);
  72. }
  73. else{
  74. callback(false, result[0]);
  75. }
  76. });
  77. });
  78. },
  79. rationChapterTreeDAO.prototype.removeNodes = function(repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  80. var functions = [];
  81. if (preNodeId != -1) {
  82. functions.push((function(nodeId, nextId) {
  83. return function(cb) {
  84. rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  85. };
  86. })(preNodeId, preNodeNextId));
  87. }
  88. for (var i=0; i < nodeIds.length; i++) {
  89. functions.push((function(nodeId) {
  90. return function(cb) {
  91. rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  92. };
  93. })(nodeIds[i]));
  94. }
  95. functions.push((function () {
  96. return function (cb) {
  97. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  98. if(err){
  99. cb(err);
  100. }
  101. else{
  102. cb(null);
  103. }
  104. })
  105. }
  106. })());
  107. async.parallel(functions, function(err, results) {
  108. callback(err, results);
  109. });
  110. }
  111. rationChapterTreeDAO.prototype.updateNodes = function(repId, lastOpr, nodes,callback){
  112. var functions = [];
  113. for (var i=0; i < nodes.length; i++) {
  114. //var md = new rationChapterTreeModel(nodes[i]);
  115. //md.isNew = false;
  116. functions.push((function(doc) {
  117. return function(cb) {
  118. rationChapterTreeModel.update({ID: doc.ID}, doc, cb);
  119. };
  120. })(nodes[i]));
  121. }
  122. functions.push((function () {
  123. return function (cb) {
  124. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  125. if(err){
  126. cb(err);
  127. }
  128. else{
  129. cb(null);
  130. }
  131. })
  132. }
  133. })());
  134. async.parallel(functions, function(err, results) {
  135. callback(err, results);
  136. });
  137. };
  138. module.exports = new rationChapterTreeDAO()