ration_section_tree.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. const compleRationSectionTemp = mongoose.model('complementary_ration_section_templates');
  12. var rationChapterTreeDAO = function(){};
  13. rationChapterTreeDAO.prototype.sectionTemplateCount = async function (compilationId) {
  14. return await compleRationSectionTemp.find({compilationId}).count();
  15. };
  16. rationChapterTreeDAO.prototype.initSectionTemplate = async function (rationLibId, compilationId) {
  17. let sectionTempCount = await compleRationSectionTemp.find({compilationId}).count();
  18. if (sectionTempCount > 0) {
  19. await compleRationSectionTemp.remove({compilationId});
  20. }
  21. let bulks = [];
  22. let stdRationSection = await rationChapterTreeModel.find({rationRepId: rationLibId});
  23. for (let data of stdRationSection) {
  24. data._doc.compilationId = compilationId;
  25. bulks.push({insertOne: {document: data}});
  26. }
  27. if (bulks.length > 0) {
  28. await compleRationSectionTemp.bulkWrite(bulks);
  29. }
  30. };
  31. rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
  32. rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  33. if(data.length) callback(0,data);
  34. else if(err) callback("获取定额树错误!",[])
  35. else callback(0,[]);
  36. })
  37. }
  38. rationChapterTreeDAO.prototype.getNewRationTreeID = function (callback) {
  39. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  40. if(err){
  41. callback(err, null);
  42. }
  43. else{
  44. callback(0, result.sequence_value);
  45. }
  46. });
  47. };
  48. rationChapterTreeDAO.prototype.getRationChapterTreeByRepId = function(repId,callback){
  49. rationChapterTreeModel.find({"rationRepId": repId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  50. if(data.length) callback(0,data);
  51. else if(err) callback("获取定额树错误!",[])
  52. else callback(false,[]);
  53. })
  54. }
  55. rationChapterTreeDAO.prototype.createNewNode = function(lastOpr, libId, lastNodeId, nodeData,callback){
  56. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
  57. nodeData.rationRepId = libId;
  58. nodeData.ID = result.sequence_value;
  59. var node = new rationChapterTreeModel(nodeData);
  60. async.parallel([
  61. function (cb) {
  62. node.save(function (err, result) {
  63. if (err) {
  64. cb("章节树ID错误!", false);
  65. } else {
  66. if (lastNodeId > 0) {
  67. rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  68. if (err) {
  69. cb("章节树ID错误!", false);
  70. } else {
  71. cb(false, result);
  72. }
  73. });
  74. } else cb(false, result);
  75. }
  76. });
  77. },
  78. function (cb) {
  79. rationRepositoryDao.updateOprArr({ID: libId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  80. if(err){
  81. cb(err);
  82. }
  83. else{
  84. cb(null);
  85. }
  86. });
  87. }
  88. ], function (err, result) {
  89. if(err){
  90. callback("章节树ID错误!", false);
  91. }
  92. else{
  93. callback(false, result[0]);
  94. }
  95. });
  96. });
  97. },
  98. rationChapterTreeDAO.prototype.removeNodes = function(repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  99. var functions = [];
  100. if (preNodeId != -1) {
  101. functions.push((function(nodeId, nextId) {
  102. return function(cb) {
  103. rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  104. };
  105. })(preNodeId, preNodeNextId));
  106. }
  107. for (var i=0; i < nodeIds.length; i++) {
  108. functions.push((function(nodeId) {
  109. return function(cb) {
  110. rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  111. };
  112. })(nodeIds[i]));
  113. }
  114. functions.push((function () {
  115. return function (cb) {
  116. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  117. if(err){
  118. cb(err);
  119. }
  120. else{
  121. cb(null);
  122. }
  123. })
  124. }
  125. })());
  126. async.parallel(functions, function(err, results) {
  127. callback(err, results);
  128. });
  129. }
  130. rationChapterTreeDAO.prototype.updateExplanation = function (lastOpr, repId, nodeId, explanation, callback) {
  131. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {explanation: explanation}}, function (err, result) {
  132. if(err){
  133. callback(err);
  134. }
  135. else{
  136. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  137. callback(null);
  138. });
  139. }
  140. });
  141. };
  142. rationChapterTreeDAO.prototype.updateRuleText = function (lastOpr, repId, nodeId, ruleText, callback) {
  143. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {ruleText: ruleText}}, function (err, result) {
  144. if(err){
  145. callback(err);
  146. }
  147. else{
  148. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  149. callback(null);
  150. });
  151. }
  152. });
  153. };
  154. rationChapterTreeDAO.prototype.updateSituation = function (lastOpr, repId, nodeId, situation, callback) {
  155. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {jobContentSituation: situation}}, function (err, result) {
  156. if(err){
  157. callback(err);
  158. }
  159. else{
  160. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  161. callback(null);
  162. });
  163. }
  164. });
  165. };
  166. rationChapterTreeDAO.prototype.updateAnnoSituation = function (lastOpr, repId, nodeId, situation, callback) {
  167. rationChapterTreeModel.update({rationRepId: repId, ID: nodeId}, {$set: {annotationSituation: situation}}, function (err, result) {
  168. if(err){
  169. callback(err);
  170. }
  171. else{
  172. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  173. callback(null);
  174. });
  175. }
  176. });
  177. };
  178. rationChapterTreeDAO.prototype.updateNodes = function(lastOpr, updateData,callback){
  179. var functions = [];
  180. for (var i=0; i < updateData.length; i++) {
  181. //var md = new rationChapterTreeModel(nodes[i]);
  182. //md.isNew = false;
  183. functions.push((function(doc) {
  184. return function(cb) {
  185. if(doc.updateType === 'update' && !doc.updateData.isDeleted){
  186. rationChapterTreeModel.update({rationRepId: doc.updateData.rationRepId, ID: doc.updateData.ID}, doc.updateData, function (err) {
  187. if(err){
  188. cb(err);
  189. }
  190. else {
  191. cb(null);
  192. }
  193. });
  194. }
  195. else if(doc.updateType === 'update' && doc.updateData.isDeleted){
  196. rationChapterTreeModel.remove({rationRepId: doc.updateData.rationRepId, ID: doc.updateData.ID}, function (err) {
  197. if(err){
  198. cb(err);
  199. }
  200. else {
  201. rationModel.remove({sectionId: doc.updateData.ID}, function(err){
  202. if(err){
  203. cb(err);
  204. }
  205. else {
  206. cb(null);
  207. }
  208. });
  209. }
  210. });
  211. }
  212. else if(doc.updateType === 'new'){
  213. rationChapterTreeModel.create(doc.updateData, function (err) {
  214. if(err){
  215. cb(err);
  216. }
  217. else {
  218. cb(null);
  219. }
  220. });
  221. }
  222. };
  223. })(updateData[i]));
  224. }
  225. if(updateData.length > 0){
  226. functions.push((function () {
  227. return function (cb) {
  228. rationRepositoryDao.updateOprArr({ID: updateData[0].updateData.rationRepId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  229. if(err){
  230. cb(err);
  231. }
  232. else{
  233. cb(null);
  234. }
  235. })
  236. }
  237. })());
  238. }
  239. async.parallel(functions, function(err, results) {
  240. if(!err){
  241. err = 0;
  242. }
  243. callback(err, results);
  244. });
  245. };
  246. module.exports = new rationChapterTreeDAO()