installation.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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'){
  35. await installSectionModel.update({ID: data.updateData.ID}, data.updateData);
  36. }
  37. }
  38. callback(0, null);
  39. }
  40. catch(err){
  41. callback(err, null);
  42. }
  43. }
  44. async updateFeeItem(updateData, callback){
  45. try{
  46. for(let data of updateData){
  47. if(data.updateType === 'new'){
  48. await installFeeItemModel.create(data.updateData);
  49. }
  50. else if(data.updateType === 'update'){
  51. await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData);
  52. }
  53. }
  54. callback(0, null);
  55. }
  56. catch(err){
  57. callback(err, null);
  58. }
  59. }
  60. async batchUpdateInst(rationSection, inst, callback){
  61. try{
  62. for(let sectionId of rationSection){
  63. await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]},
  64. {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
  65. }
  66. callback(0, null);
  67. }
  68. catch(err){
  69. callback(err, null);
  70. }
  71. }
  72. }
  73. export default InstallationDao;