sectionTreeModel.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. let counter = require('../../../public/counter/counter');
  7. let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model;
  8. class SectionTreeDao {
  9. getNewTreeID(callback){
  10. counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function (err, result) {
  11. if(err){
  12. callback(err, null);
  13. }
  14. else {
  15. callback(0, result.sequence_value);
  16. }
  17. });
  18. }
  19. //获取补充定额拼接章节树
  20. async getRationTree(userID, rationRepId, callback){
  21. try{
  22. let stdSectionTree = await stdSectionTreeModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]});
  23. let compleSectionTree = await compleRationSectionTreeModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null});
  24. let dropPids = [], rstCompleSectionTree = [];
  25. //mark std
  26. for(let i = 0, len = stdSectionTree.length; i < len; i++){
  27. stdSectionTree[i]._doc.type = 'std';
  28. }
  29. for(let i = 0, len = compleSectionTree.length; i < len; i++){
  30. //mark complementary
  31. compleSectionTree[i]._doc.type = 'complementary';
  32. if(compleSectionTree[i]['isFirst']){
  33. let updateSection = getUpdateSection(compleSectionTree[i]['ParentID'], stdSectionTree);
  34. if(updateSection) {
  35. updateSection._doc.NextSiblingID = compleSectionTree[i]['ID'];
  36. }
  37. else if(!updateSection && compleSectionTree[i]['ParentID'] !== -1){
  38. dropPids.push(compleSectionTree[i]['ParentID']);
  39. }
  40. }
  41. }
  42. function getUpdateSection(pid, datas){
  43. for(let i = 0, len = datas.length; i < len; i++){
  44. if(datas[i]['ParentID'] === pid && datas[i]['NextSiblingID'] === -1){
  45. return datas[i];
  46. }
  47. }
  48. return null;
  49. }
  50. //返回父节点未被删除的
  51. for(let i = 0, len = compleSectionTree.length; i < len; i++){
  52. if(dropPids.indexOf(compleSectionTree[i]['ParentID']) === -1){
  53. rstCompleSectionTree.push(compleSectionTree[i]);
  54. }
  55. }
  56. callback(0, stdSectionTree.concat(rstCompleSectionTree));
  57. }
  58. catch (err){
  59. callback(err, null);
  60. }
  61. }
  62. async updateSection(userID, compilationId, updateData, callback){
  63. try{
  64. for(let i = 0, len = updateData.length; i < len; i++){
  65. let updateObj = updateData[i];
  66. if(updateObj.updateType === 'new'){
  67. updateObj.updateData.userId = userID;
  68. updateObj.updateData.compilationId = compilationId;
  69. await compleRationSectionTreeModel.create(updateObj.updateData);
  70. }
  71. else if(updateObj.updateType === 'update'){
  72. await compleRationSectionTreeModel.update({userId: userID, rationRepId: updateObj.updateData.rationRepId, ID: updateObj.updateData.ID}, updateObj.updateData);
  73. }
  74. }
  75. callback(0, 'success');
  76. }
  77. catch(err){
  78. callback(err, null);
  79. }
  80. }
  81. }
  82. export default SectionTreeDao;