sys_model.js 4.5 KB

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