ration_section_tree.js 9.8 KB

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