sectionTreeModel.js 3.5 KB

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