ration_section_tree.js 6.2 KB

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