sectionTreeModel.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Created by Zhong on 2017/12/21.
  3. */
  4. const mongoose = require('mongoose');
  5. const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree');
  6. const compleRationModel = mongoose.model('complementary_ration_items');
  7. let counter = require('../../../public/counter/counter');
  8. let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model;
  9. const uuidV1 = require('uuid/v1');
  10. const sectionTemplateModel = mongoose.model('complementary_ration_section_templates');
  11. class SectionTreeDao {
  12. //从补充定额章节树模板中拷贝数据到用户的补充定额章节树中
  13. async copyDataFromTemplate(userId, compilationId){
  14. const srcCompilationID = '5b52b027fd3bb0000b257cf8';
  15. // 如果已经有数据则不再生成
  16. const count = await compleRationSectionTreeModel.count({userId, compilationId});
  17. if (count) {
  18. return;
  19. }
  20. let templateData = await sectionTemplateModel.find({compilationId: srcCompilationID});
  21. if (templateData.length > 0) {
  22. let insertDatas = [],
  23. uuidMapping = {};
  24. //将ID替换成UUID
  25. for (let temData of templateData) {
  26. uuidMapping[temData.ID] = uuidV1();
  27. }
  28. for(let temData of templateData) {
  29. let insertD = {
  30. userId: userId,
  31. compilationId: compilationId,
  32. name: temData.name,
  33. ID: uuidMapping[temData.ID],
  34. ParentID: uuidMapping[temData.ParentID] || -1,
  35. NextSiblingID: uuidMapping[temData.NextSiblingID] || -1,
  36. };
  37. insertDatas.push(insertD);
  38. }
  39. //插入数据
  40. await compleRationSectionTreeModel.insertMany(insertDatas);
  41. }
  42. }
  43. getNewTreeID(callback){
  44. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function (err, result) {
  45. if(err){
  46. callback(err, null);
  47. }
  48. else {
  49. callback(0, result.sequence_value);
  50. }
  51. });
  52. }
  53. async getComplementaryTree (userId, compilationId) {
  54. const treeData = await compleRationSectionTreeModel.find({userId, compilationId}).lean();
  55. return treeData;
  56. }
  57. async getRationTree(userId, compilationId, rationRepId, type, callback) {
  58. //区分要获取的是标准的数据还是补充的数据
  59. const rationLibType = {
  60. complementary: 0,
  61. std: 1
  62. };
  63. try {
  64. let treeData;
  65. if (type === rationLibType.complementary) {
  66. treeData = await compleRationSectionTreeModel.find({userId: userId, compilationId: compilationId, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}).lean();
  67. } else {
  68. treeData = await stdSectionTreeModel.find({rationRepId: rationRepId}).lean();
  69. }
  70. callback(0, treeData);
  71. } catch (err) {
  72. console.log(err);
  73. callback(1, null);
  74. }
  75. }
  76. //获取补充定额拼接章节树
  77. /* async getRationTree(userID, rationRepId, callback){
  78. try{
  79. let stdSectionTree = await stdSectionTreeModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]});
  80. let compleSectionTree = await compleRationSectionTreeModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null});
  81. let dropPids = [], rstCompleSectionTree = [];
  82. //mark std
  83. for(let i = 0, len = stdSectionTree.length; i < len; i++){
  84. stdSectionTree[i]._doc.type = 'std';
  85. }
  86. for(let i = 0, len = compleSectionTree.length; i < len; i++){
  87. //mark complementary
  88. compleSectionTree[i]._doc.type = 'complementary';
  89. if(compleSectionTree[i]['isFirst']){
  90. let updateSection = getUpdateSection(compleSectionTree[i]['ParentID'], stdSectionTree);
  91. if(updateSection) {
  92. updateSection._doc.NextSiblingID = compleSectionTree[i]['ID'];
  93. }
  94. else if(!updateSection && compleSectionTree[i]['ParentID'] !== -1){
  95. dropPids.push(compleSectionTree[i]['ParentID']);
  96. }
  97. }
  98. }
  99. function getUpdateSection(pid, datas){
  100. for(let i = 0, len = datas.length; i < len; i++){
  101. if(datas[i]['ParentID'] === pid && datas[i]['NextSiblingID'] === -1){
  102. return datas[i];
  103. }
  104. }
  105. return null;
  106. }
  107. //返回父节点未被删除的
  108. for(let i = 0, len = compleSectionTree.length; i < len; i++){
  109. if(dropPids.indexOf(compleSectionTree[i]['ParentID']) === -1){
  110. rstCompleSectionTree.push(compleSectionTree[i]);
  111. }
  112. }
  113. callback(0, stdSectionTree.concat(rstCompleSectionTree));
  114. }
  115. catch (err){
  116. callback(err, null);
  117. }
  118. }*/
  119. async updateSection(userID, compilationId, updateData, callback){
  120. try{
  121. for(let i = 0, len = updateData.length; i < len; i++){
  122. let updateObj = updateData[i];
  123. if(updateObj.updateType === 'new'){
  124. updateObj.updateData.userId = userID;
  125. updateObj.updateData.compilationId = compilationId;
  126. await compleRationSectionTreeModel.create(updateObj.updateData);
  127. }
  128. else if(updateObj.updateType === 'update'){
  129. if(!updateObj.updateData.deleteInfo){
  130. await compleRationSectionTreeModel.update({userId: userID, ID: updateObj.updateData.ID}, updateObj.updateData);
  131. } else {
  132. await compleRationSectionTreeModel.remove({userId: userID, ID: updateObj.updateData.ID});
  133. }
  134. }
  135. }
  136. callback(0, 'success');
  137. }
  138. catch(err){
  139. callback(err, null);
  140. }
  141. }
  142. }
  143. module.exports = SectionTreeDao;