installation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Created by Zhong on 2018/1/24.
  3. */
  4. const mongoose = require('mongoose');
  5. const rationItemModel = mongoose.model('std_ration_lib_ration_items');
  6. const installFeeItemModel = mongoose.model('std_ration_lib_installation');
  7. const installSectionModel = mongoose.model('std_ration_lib_installationSection');
  8. class InstallationDao{
  9. async getInstallation(rationRepId, callback){
  10. try {
  11. const feeItems = await installFeeItemModel.find({ rationRepId }).lean();
  12. const feeItemMap = {};
  13. const sectionIds = [];
  14. feeItems.forEach(item => {
  15. feeItemMap[item.ID] = item;
  16. item.section.forEach(s => sectionIds.push(s.ID));
  17. item.section = [];
  18. });
  19. const sections = await installSectionModel.find({ID: {$in: sectionIds}});
  20. sections.forEach(section => {
  21. const matchFeeItem = feeItemMap[section.feeItemId];
  22. if (matchFeeItem) {
  23. matchFeeItem.section.push(section);
  24. }
  25. });
  26. if (!callback) {
  27. return feeItems;
  28. }
  29. callback(0, feeItems);
  30. }
  31. catch(err){
  32. if (!callback) {
  33. return [];
  34. }
  35. callback(err, null);
  36. }
  37. }
  38. /*async getInstallation(rationRepId, callback){
  39. try {
  40. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId});
  41. for(let feeItem of feeItems){
  42. let sids = [];
  43. for(let sec of feeItem.section){
  44. sids.push(sec.ID);
  45. }
  46. if(sids.length > 0){
  47. let sections = await installSectionModel.find({ID: {$in: sids}});
  48. feeItem._doc.section = sections;
  49. }
  50. }
  51. callback(0, feeItems);
  52. }
  53. catch(err){
  54. callback(err, null);
  55. }
  56. }*/
  57. async updateSection(updateData, libID, callback){
  58. try{
  59. const delIDs = [];
  60. for(let data of updateData){
  61. if(data.updateType === 'new'){
  62. await installSectionModel.create(data.updateData);
  63. }
  64. else if(data.updateType === 'update' && !data.updateData.deleted){
  65. await installSectionModel.update({ID: data.updateData.ID}, data.updateData);
  66. }
  67. else {
  68. delIDs.push(data.updateData.ID);
  69. await installSectionModel.remove({ID: data.updateData.ID});
  70. }
  71. }
  72. if (delIDs.length) {
  73. await rationItemModel.updateMany({rationRepId: libID}, {$pull: { rationInstList: { sectionId: { $in: delIDs } } }});
  74. }
  75. callback(0, null);
  76. }
  77. catch(err){
  78. callback(err, null);
  79. }
  80. }
  81. async updateFeeItem(updateData, callback){
  82. try{
  83. for(let data of updateData){
  84. if(data.updateType === 'new'){
  85. await installFeeItemModel.create(data.updateData);
  86. }
  87. else if(data.updateType === 'update' && !data.updateData.deleted){
  88. await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData);
  89. }
  90. else{
  91. await installFeeItemModel.remove({ID: data.updateData.ID});
  92. }
  93. }
  94. callback(0, null);
  95. }
  96. catch(err){
  97. callback(err, null);
  98. }
  99. }
  100. async batchUpdateInst(rationSection, inst, callback){
  101. try{
  102. for(let sectionId of rationSection){
  103. await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]},
  104. {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
  105. }
  106. callback(0, null);
  107. }
  108. catch(err){
  109. callback(err, null);
  110. }
  111. }
  112. }
  113. export default InstallationDao;