sys_model.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. const compleRationSection = mongoose.model('complementary_ration_section_tree');
  31. //删除垃圾数据
  32. async function clearJunkData(callback){
  33. let functions = [];
  34. //获取彻底删除了的项目
  35. let junkProjs = await projectModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  36. let junkProjIds = [];
  37. for(let jProj of junkProjs){
  38. junkProjIds.push(jProj.ID);
  39. }
  40. if(junkProjIds.length > 0){
  41. //清除proj_setting
  42. functions.push(function(cb){
  43. projSettingModel.remove({projectID: {$in: junkProjIds}}, cb);
  44. });
  45. //清除calcProgram
  46. functions.push(function(cb){
  47. calcProgramModel.remove({projectID: {$in: junkProjIds}}, cb);
  48. });
  49. //清除labourCoe
  50. functions.push(function(cb){
  51. labourCoeModel.remove({projectID: {$in: junkProjIds}}, cb);
  52. });
  53. //清除bills
  54. functions.push(function(cb){
  55. billsModel.remove({projectID: {$in: junkProjIds}}, cb);
  56. });
  57. //清除ration
  58. functions.push(function(cb){
  59. rationModel.remove({projectID: {$in: junkProjIds}}, cb);
  60. });
  61. //清除project_glj
  62. functions.push(function(cb){
  63. projGljModel.remove({project_id: {$in: junkProjIds}}, cb);
  64. });
  65. //清除ration_glj
  66. functions.push(function(cb){
  67. rationGljModel.remove({projectID: {$in: junkProjIds}}, cb);
  68. });
  69. //清除ration_coe
  70. functions.push(function(cb){
  71. rationCoeMolde.remove({projectID: {$in: junkProjIds}}, cb);
  72. });
  73. //清除installation_fee
  74. functions.push(function(cb){
  75. installationModel.remove({projectID: {$in: junkProjIds}}, cb);
  76. });
  77. //清除ration_installation
  78. functions.push(function(cb){
  79. rationInstallationModel.remove({projectID: {$in: junkProjIds}}, cb);
  80. });
  81. //清除ration_installation
  82. functions.push(function(cb){
  83. rationTemplateModel.remove({projectID: {$in: junkProjIds}}, cb);
  84. });
  85. //清除quantity_detail
  86. functions.push(function(cb){
  87. quantityDetailModel.remove({projectID: {$in: junkProjIds}}, cb);
  88. });
  89. }
  90. //彻底删除了的单价文件
  91. let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  92. let junkUFIds = [];
  93. for(let jUF of junkUFs){
  94. junkUFIds.push(jUF.id);
  95. }
  96. if(junkUFIds.length > 0){
  97. functions.push(function(cb){
  98. unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  99. });
  100. functions.push(function(cb){
  101. mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  102. });
  103. }
  104. //彻底删除了的费率文件
  105. let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  106. let junkFFIds = [];
  107. for(let jFF of junkFFs){
  108. junkFFIds.push(jFF.feeRateID);
  109. }
  110. if(junkFFIds.length > 0){
  111. functions.push(function(cb){
  112. feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
  113. });
  114. }
  115. //清除
  116. if(functions.length > 0){
  117. async.parallel(functions, async function(err, result){
  118. if(!err){
  119. //清除项目
  120. await projectModel.remove({ID: {$in: junkProjIds}});
  121. //清除单价文件
  122. await unitPriceFileModel.remove({id: {$in: junkUFIds}});
  123. //清除费率文件
  124. await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
  125. }
  126. if(callback) callback(err);
  127. });
  128. }
  129. else {
  130. if(callback) callback(0);
  131. }
  132. }
  133. //删除假删除数据
  134. /*
  135. * 1.之前的删除造价书数据,都是做的假删除,这部分数据会一直存在并且随着项目重复使用越来越多,现要改为真删除,所以做这个清除功能。
  136. * 2.原假删除包括清单的所有类型(大项费用、分部、分项、补项、清单)、定额的所有类型(定额、数量单价、与定额同级的人材机)
  137. * */
  138. async function clearFakeData(callback) {
  139. let functions = [];
  140. //删除清单
  141. functions.push(function (cb) {
  142. billsModel.remove({deleteInfo: {$exists: true}}, cb);
  143. });
  144. //删除定额
  145. functions.push(function (cb) {
  146. rationModel.remove({deleteInfo: {$exists: true}}, cb);
  147. });
  148. //删除补充定额章节树
  149. functions.push(function (cb) {
  150. compleRationSection.remove({deleteInfo: {$exists: true}}, cb);
  151. });
  152. async.parallel(functions, async function(err, result){
  153. if(callback){
  154. callback(err);
  155. }
  156. });
  157. }
  158. const sysSchedule = {
  159. clearJunkData,
  160. clearFakeData
  161. };
  162. //export {sysSchedule as default}
  163. module.exports = sysSchedule;