ration_section_tree.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. explanation: String,//说明
  18. ruleText: String,//计算规则
  19. isDeleted: Boolean
  20. });
  21. var rationChapterTreeModel = chapterTreeDb.model("std_ration_lib_ration_chapter_trees", rationChapterTreeSchema, "std_ration_lib_ration_chapter_trees");
  22. var counter = require('../../../public/counter/counter');
  23. var rationChapterTreeDAO = function(){};
  24. rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
  25. rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  26. if(data.length) callback(false,data);
  27. else if(err) callback("获取定额树错误!",false)
  28. else callback(false,false);
  29. })
  30. }
  31. rationChapterTreeDAO.prototype.getRationChapterTreeByRepId = function(repId,callback){
  32. rationChapterTreeModel.find({"rationRepId": repId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  33. if(data.length) callback(false,data);
  34. else if(err) callback("获取定额树错误!",false)
  35. else callback(false,false);
  36. })
  37. }
  38. rationChapterTreeDAO.prototype.createNewNode = function(lastOpr, libId, lastNodeId, nodeData,callback){
  39. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  40. nodeData.rationRepId = libId;
  41. nodeData.ID = result.value.sequence_value;
  42. var node = new rationChapterTreeModel(nodeData);
  43. async.parallel([
  44. function (cb) {
  45. node.save(function (err, result) {
  46. if (err) {
  47. cb("章节树ID错误!", false);
  48. } else {
  49. if (lastNodeId > 0) {
  50. rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  51. if (err) {
  52. cb("章节树ID错误!", false);
  53. } else {
  54. cb(false, result);
  55. }
  56. });
  57. } else cb(false, result);
  58. }
  59. });
  60. },
  61. function (cb) {
  62. rationRepositoryDao.updateOprArr({ID: libId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  63. if(err){
  64. cb(err);
  65. }
  66. else{
  67. cb(null);
  68. }
  69. });
  70. }
  71. ], function (err, result) {
  72. if(err){
  73. callback("章节树ID错误!", false);
  74. }
  75. else{
  76. callback(false, result[0]);
  77. }
  78. });
  79. });
  80. },
  81. rationChapterTreeDAO.prototype.removeNodes = function(repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  82. var functions = [];
  83. if (preNodeId != -1) {
  84. functions.push((function(nodeId, nextId) {
  85. return function(cb) {
  86. rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  87. };
  88. })(preNodeId, preNodeNextId));
  89. }
  90. for (var i=0; i < nodeIds.length; i++) {
  91. functions.push((function(nodeId) {
  92. return function(cb) {
  93. rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  94. };
  95. })(nodeIds[i]));
  96. }
  97. functions.push((function () {
  98. return function (cb) {
  99. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  100. if(err){
  101. cb(err);
  102. }
  103. else{
  104. cb(null);
  105. }
  106. })
  107. }
  108. })());
  109. async.parallel(functions, function(err, results) {
  110. callback(err, results);
  111. });
  112. }
  113. rationChapterTreeDAO.prototype.updateExplanation = function (lastOpr, repId, nodeId, explanation, callback) {
  114. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {explanation: explanation}}, function (err, result) {
  115. if(err){
  116. callback(err);
  117. }
  118. else{
  119. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  120. callback(null);
  121. });
  122. }
  123. });
  124. };
  125. rationChapterTreeDAO.prototype.updateRuleText = function (lastOpr, repId, nodeId, ruleText, callback) {
  126. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {ruleText: ruleText}}, function (err, result) {
  127. if(err){
  128. callback(err);
  129. }
  130. else{
  131. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  132. callback(null);
  133. });
  134. }
  135. });
  136. };
  137. rationChapterTreeDAO.prototype.updateNodes = function(repId, lastOpr, nodes,callback){
  138. var functions = [];
  139. for (var i=0; i < nodes.length; i++) {
  140. //var md = new rationChapterTreeModel(nodes[i]);
  141. //md.isNew = false;
  142. functions.push((function(doc) {
  143. return function(cb) {
  144. rationChapterTreeModel.update({ID: doc.ID}, doc, cb);
  145. };
  146. })(nodes[i]));
  147. }
  148. functions.push((function () {
  149. return function (cb) {
  150. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  151. if(err){
  152. cb(err);
  153. }
  154. else{
  155. cb(null);
  156. }
  157. })
  158. }
  159. })());
  160. async.parallel(functions, function(err, results) {
  161. callback(err, results);
  162. });
  163. };
  164. module.exports = new rationChapterTreeDAO()