installation.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]});
  12. for(let feeItem of feeItems){
  13. let sids = [];
  14. for(let sec of feeItem.section){
  15. sids.push(sec.ID);
  16. }
  17. if(sids.length > 0){
  18. let sections = await installSectionModel.find({ID: {$in: sids}, $or: [{deleted: false}, {deleted: null}]});
  19. feeItem._doc.section = sections;
  20. }
  21. }
  22. callback(0, feeItems);
  23. }
  24. catch(err){
  25. callback(err, null);
  26. }
  27. }
  28. async updateSection(updateData, callback){
  29. try{
  30. for(let data of updateData){
  31. if(data.updateType === 'new'){
  32. await installSectionModel.create(data.updateData);
  33. }
  34. else if(data.updateType === 'update' && !data.updateData.deleted){
  35. await installSectionModel.update({ID: data.updateData.ID}, data.updateData);
  36. }
  37. else {
  38. await installSectionModel.remove({ID: data.updateData.ID});
  39. }
  40. }
  41. callback(0, null);
  42. }
  43. catch(err){
  44. callback(err, null);
  45. }
  46. }
  47. async updateFeeItem(updateData, callback){
  48. try{
  49. for(let data of updateData){
  50. if(data.updateType === 'new'){
  51. await installFeeItemModel.create(data.updateData);
  52. }
  53. else if(data.updateType === 'update' && !data.updateData.deleted){
  54. await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData);
  55. }
  56. else{
  57. await installFeeItemModel.remove({ID: data.updateData.ID});
  58. }
  59. }
  60. callback(0, null);
  61. }
  62. catch(err){
  63. callback(err, null);
  64. }
  65. }
  66. async batchUpdateInst(rationSection, inst, callback){
  67. try{
  68. for(let sectionId of rationSection){
  69. await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]},
  70. {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
  71. }
  72. callback(0, null);
  73. }
  74. catch(err){
  75. callback(err, null);
  76. }
  77. }
  78. }
  79. export default InstallationDao;