sys_model.js 6.5 KB

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