sys_model.js 6.4 KB

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