sys_model.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/5/24
  7. * @version
  8. */
  9. import mongoose from 'mongoose';
  10. import schedule from 'node-schedule';
  11. import async from 'async';
  12. const projectModel = mongoose.model('projects');
  13. const projSettingModel = mongoose.model('proj_setting');
  14. const calcProgramModel = mongoose.model('calc_programs');
  15. const labourCoeModel = mongoose.model('labour_coes');
  16. const billsModel = mongoose.model('bills');
  17. const rationModel = mongoose.model('ration');
  18. const projGljModel = mongoose.model('glj_list');
  19. const rationGljModel = mongoose.model('ration_glj');
  20. const rationCoeMolde = mongoose.model('ration_coe');
  21. const installationModel = mongoose.model('installation_fee');
  22. const rationInstallationModel = mongoose.model('ration_installation');
  23. const rationTemplateModel = mongoose.model('ration_template')
  24. const quantityDetailModel = mongoose.model('quantity_detail');
  25. const unitPriceFileModel = mongoose.model('unit_price_file');
  26. const unitPriceModel = mongoose.model('unit_price');
  27. const mixRatioModel = mongoose.model('mix_ratio');
  28. const feeRateFileModel = mongoose.model('fee_rate_file');
  29. const feeRateModel = mongoose.model('fee_rates');
  30. //删除垃圾数据
  31. async function clearJunkData(callback){
  32. let functions = [];
  33. //获取彻底删除了的项目
  34. let junkProjs = await projectModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  35. let junkProjIds = [];
  36. for(let jProj of junkProjs){
  37. junkProjIds.push(jProj.ID);
  38. }
  39. if(junkProjIds.length > 0){
  40. //清除proj_setting
  41. functions.push(function(cb){
  42. projSettingModel.remove({projectID: {$in: junkProjIds}}, cb);
  43. });
  44. //清除calcProgram
  45. functions.push(function(cb){
  46. calcProgramModel.remove({projectID: {$in: junkProjIds}}, cb);
  47. });
  48. //清除labourCoe
  49. functions.push(function(cb){
  50. labourCoeModel.remove({projectID: {$in: junkProjIds}}, cb);
  51. });
  52. //清除bills
  53. functions.push(function(cb){
  54. billsModel.remove({projectID: {$in: junkProjIds}}, cb);
  55. });
  56. //清除ration
  57. functions.push(function(cb){
  58. rationModel.remove({projectID: {$in: junkProjIds}}, cb);
  59. });
  60. //清除project_glj
  61. functions.push(function(cb){
  62. projGljModel.remove({project_id: {$in: junkProjIds}}, cb);
  63. });
  64. //清除ration_glj
  65. functions.push(function(cb){
  66. rationGljModel.remove({projectID: {$in: junkProjIds}}, cb);
  67. });
  68. //清除ration_coe
  69. functions.push(function(cb){
  70. rationCoeMolde.remove({projectID: {$in: junkProjIds}}, cb);
  71. });
  72. //清除installation_fee
  73. functions.push(function(cb){
  74. installationModel.remove({projectID: {$in: junkProjIds}}, cb);
  75. });
  76. //清除ration_installation
  77. functions.push(function(cb){
  78. rationInstallationModel.remove({projectID: {$in: junkProjIds}}, cb);
  79. });
  80. //清除ration_installation
  81. functions.push(function(cb){
  82. rationTemplateModel.remove({projectID: {$in: junkProjIds}}, cb);
  83. });
  84. //清除quantity_detail
  85. functions.push(function(cb){
  86. quantityDetailModel.remove({projectID: {$in: junkProjIds}}, cb);
  87. });
  88. }
  89. //彻底删除了的单价文件
  90. let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  91. let junkUFIds = [];
  92. for(let jUF of junkUFs){
  93. junkUFIds.push(jUF.id);
  94. }
  95. if(junkUFIds.length > 0){
  96. functions.push(function(cb){
  97. unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  98. });
  99. functions.push(function(cb){
  100. mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  101. });
  102. }
  103. //彻底删除了的费率文件
  104. let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  105. let junkFFIds = [];
  106. for(let jFF of junkFFs){
  107. junkFFIds.push(jFF.feeRateID);
  108. }
  109. if(junkFFIds.length > 0){
  110. functions.push(function(cb){
  111. feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
  112. });
  113. }
  114. //清除
  115. if(functions.length > 0){
  116. async.parallel(functions, async function(err, result){
  117. if(!err){
  118. //清除项目
  119. await projectModel.remove({ID: {$in: junkProjIds}});
  120. //清除单价文件
  121. await unitPriceFileModel.remove({id: {$in: junkUFIds}});
  122. //清除费率文件
  123. await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
  124. }
  125. if(callback){
  126. callback(err);
  127. }
  128. });
  129. }
  130. else {
  131. callback(0);
  132. }
  133. }
  134. const sysSchedule = {
  135. clearJunkData: clearJunkData
  136. };
  137. //export {sysSchedule as default}
  138. module.exports = sysSchedule;