ration_section_tree.js 11 KB

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