installation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // 安装增加费库映射(12修缮定额需要直接用18定额的安装数据)
  10. getInstLibID(libID) {
  11. return +libID === 248 ? 214 : +libID;
  12. }
  13. async getInstallation(rationRepId, callback) {
  14. try {
  15. rationRepId = this.getInstLibID(rationRepId);
  16. const feeItems = await installFeeItemModel.find({ rationRepId }).lean();
  17. const feeItemMap = {};
  18. const sectionIds = [];
  19. feeItems.forEach(item => {
  20. feeItemMap[item.ID] = item;
  21. item.section.forEach(s => sectionIds.push(s.ID));
  22. item.section = [];
  23. });
  24. const sections = await installSectionModel.find({ ID: { $in: sectionIds } });
  25. sections.forEach(section => {
  26. const matchFeeItem = feeItemMap[section.feeItemId];
  27. if (matchFeeItem) {
  28. matchFeeItem.section.push(section);
  29. }
  30. });
  31. if (!callback) {
  32. return feeItems;
  33. }
  34. callback(0, feeItems);
  35. }
  36. catch (err) {
  37. if (!callback) {
  38. return [];
  39. }
  40. callback(err, null);
  41. }
  42. }
  43. /*async getInstallation(rationRepId, callback){
  44. try {
  45. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId});
  46. for(let feeItem of feeItems){
  47. let sids = [];
  48. for(let sec of feeItem.section){
  49. sids.push(sec.ID);
  50. }
  51. if(sids.length > 0){
  52. let sections = await installSectionModel.find({ID: {$in: sids}});
  53. feeItem._doc.section = sections;
  54. }
  55. }
  56. callback(0, feeItems);
  57. }
  58. catch(err){
  59. callback(err, null);
  60. }
  61. }*/
  62. async updateSection(updateData, libID, callback) {
  63. try {
  64. const delIDs = [];
  65. for (let data of updateData) {
  66. if (data.updateData.rationRepId) {
  67. data.updateData.rationRepId = this.getInstLibID(data.updateData.rationRepId);
  68. }
  69. if (data.updateType === 'new') {
  70. await installSectionModel.create(data.updateData);
  71. }
  72. else if (data.updateType === 'update' && !data.updateData.deleted) {
  73. await installSectionModel.update({ ID: data.updateData.ID }, data.updateData);
  74. }
  75. else {
  76. delIDs.push(data.updateData.ID);
  77. await installSectionModel.remove({ ID: data.updateData.ID });
  78. }
  79. }
  80. if (delIDs.length) {
  81. await rationItemModel.updateMany({ rationRepId: libID }, { $pull: { rationInstList: { sectionId: { $in: delIDs } } } });
  82. }
  83. callback(0, null);
  84. }
  85. catch (err) {
  86. callback(err, null);
  87. }
  88. }
  89. async updateFeeItem(updateData, callback) {
  90. try {
  91. for (let data of updateData) {
  92. if (data.updateData.rationRepId) {
  93. data.updateData.rationRepId = this.getInstLibID(data.updateData.rationRepId);
  94. }
  95. if (data.updateType === 'new') {
  96. await installFeeItemModel.create(data.updateData);
  97. }
  98. else if (data.updateType === 'update' && !data.updateData.deleted) {
  99. await installFeeItemModel.update({ ID: data.updateData.ID }, data.updateData);
  100. }
  101. else {
  102. await installFeeItemModel.remove({ ID: data.updateData.ID });
  103. }
  104. }
  105. callback(0, null);
  106. }
  107. catch (err) {
  108. callback(err, null);
  109. }
  110. }
  111. async batchUpdateInst(rationSection, inst, callback) {
  112. try {
  113. for (let sectionId of rationSection) {
  114. await rationItemModel.update({ sectionId: sectionId, $or: [{ isDeleted: null }, { isDeleted: false }] },
  115. { $addToSet: { rationInstList: { feeItemId: inst.feeItemId, sectionId: inst.sectionId } } }, { multi: true });
  116. }
  117. callback(0, null);
  118. }
  119. catch (err) {
  120. callback(err, null);
  121. }
  122. }
  123. }
  124. export default InstallationDao;