ration_section_tree.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Created by Tony on 2017/4/21.
  3. */
  4. const mongoose = require('mongoose');
  5. let async = require('async');
  6. let moment = require('moment');
  7. let counter = require('../../../public/counter/counter');
  8. let rationRepositoryDao = require('./repository_map');
  9. const rationChapterTreeModel = mongoose.model('std_ration_lib_ration_chapter_trees');
  10. const rationModel = mongoose.model('std_ration_lib_ration_items');
  11. var rationChapterTreeDAO = function(){};
  12. rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
  13. rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  14. if(data.length) callback(0,data);
  15. else if(err) callback("获取定额树错误!",[])
  16. else callback(0,[]);
  17. })
  18. }
  19. rationChapterTreeDAO.prototype.getNewRationTreeID = function (callback) {
  20. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  21. if(err){
  22. callback(err, null);
  23. }
  24. else{
  25. callback(0, result.sequence_value);
  26. }
  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(0,data);
  32. else if(err) callback("获取定额树错误!",[])
  33. else callback(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.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.updateExplanation = function (lastOpr, repId, nodeId, explanation, callback) {
  112. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {explanation: explanation}}, 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.updateRuleText = function (lastOpr, repId, nodeId, ruleText, callback) {
  124. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {ruleText: ruleText}}, 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.updateSituation = function (lastOpr, repId, nodeId, situation, callback) {
  136. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {jobContentSituation: situation}}, function (err, result) {
  137. if(err){
  138. callback(err);
  139. }
  140. else{
  141. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  142. callback(null);
  143. });
  144. }
  145. });
  146. };
  147. rationChapterTreeDAO.prototype.updateAnnoSituation = function (lastOpr, repId, nodeId, situation, callback) {
  148. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {annotationSituation: situation}}, function (err, result) {
  149. if(err){
  150. callback(err);
  151. }
  152. else{
  153. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  154. callback(null);
  155. });
  156. }
  157. });
  158. };
  159. rationChapterTreeDAO.prototype.updateNodes = function(lastOpr, updateData,callback){
  160. var functions = [];
  161. for (var i=0; i < updateData.length; i++) {
  162. //var md = new rationChapterTreeModel(nodes[i]);
  163. //md.isNew = false;
  164. functions.push((function(doc) {
  165. return function(cb) {
  166. if(doc.updateType === 'update' && !doc.updateData.isDeleted){
  167. rationChapterTreeModel.update({rationRepId: doc.updateData.rationRepId, ID: doc.updateData.ID}, doc.updateData, function (err) {
  168. if(err){
  169. cb(err);
  170. }
  171. else {
  172. cb(null);
  173. }
  174. });
  175. }
  176. else if(doc.updateType === 'update' && doc.updateData.isDeleted){
  177. rationChapterTreeModel.remove({rationRepId: doc.updateData.rationRepId, ID: doc.updateData.ID}, function (err) {
  178. if(err){
  179. cb(err);
  180. }
  181. else {
  182. rationModel.remove({sectionId: doc.updateData.ID}, function(err){
  183. if(err){
  184. cb(err);
  185. }
  186. else {
  187. cb(null);
  188. }
  189. });
  190. }
  191. });
  192. }
  193. else if(doc.updateType === 'new'){
  194. rationChapterTreeModel.create(doc.updateData, function (err) {
  195. if(err){
  196. cb(err);
  197. }
  198. else {
  199. cb(null);
  200. }
  201. });
  202. }
  203. };
  204. })(updateData[i]));
  205. }
  206. if(updateData.length > 0){
  207. functions.push((function () {
  208. return function (cb) {
  209. rationRepositoryDao.updateOprArr({ID: updateData[0].updateData.rationRepId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  210. if(err){
  211. cb(err);
  212. }
  213. else{
  214. cb(null);
  215. }
  216. })
  217. }
  218. })());
  219. }
  220. async.parallel(functions, function(err, results) {
  221. if(!err){
  222. err = 0;
  223. }
  224. callback(err, results);
  225. });
  226. };
  227. module.exports = new rationChapterTreeDAO()