installation.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, callback){
  58. try{
  59. for(let data of updateData){
  60. if(data.updateType === 'new'){
  61. await installSectionModel.create(data.updateData);
  62. }
  63. else if(data.updateType === 'update' && !data.updateData.deleted){
  64. await installSectionModel.update({ID: data.updateData.ID}, data.updateData);
  65. }
  66. else {
  67. await installSectionModel.remove({ID: data.updateData.ID});
  68. }
  69. }
  70. callback(0, null);
  71. }
  72. catch(err){
  73. callback(err, null);
  74. }
  75. }
  76. async updateFeeItem(updateData, callback){
  77. try{
  78. for(let data of updateData){
  79. if(data.updateType === 'new'){
  80. await installFeeItemModel.create(data.updateData);
  81. }
  82. else if(data.updateType === 'update' && !data.updateData.deleted){
  83. await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData);
  84. }
  85. else{
  86. await installFeeItemModel.remove({ID: data.updateData.ID});
  87. }
  88. }
  89. callback(0, null);
  90. }
  91. catch(err){
  92. callback(err, null);
  93. }
  94. }
  95. async batchUpdateInst(rationSection, inst, callback){
  96. try{
  97. for(let sectionId of rationSection){
  98. await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]},
  99. {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
  100. }
  101. callback(0, null);
  102. }
  103. catch(err){
  104. callback(err, null);
  105. }
  106. }
  107. }
  108. export default InstallationDao;