sys_model.js 4.7 KB

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