sys_model.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. let divideModel = mongoose.model("divide_setting");
  34. //删除垃圾数据
  35. async function clearJunkData(callback){
  36. let functions = [];
  37. //获取彻底删除了的项目
  38. let junkProjs = await projectModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true}, '-_id ID').lean();
  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. functions.push(function(cb){
  94. divideModel.remove({projectID: {$in: junkProjIds}}, cb);
  95. });
  96. }
  97. //彻底删除了的单价文件
  98. let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  99. let junkUFIds = [];
  100. for(let jUF of junkUFs){
  101. junkUFIds.push(jUF.id);
  102. }
  103. if(junkUFIds.length > 0){
  104. functions.push(function(cb){
  105. unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  106. });
  107. functions.push(function(cb){
  108. mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  109. });
  110. functions.push(function(cb){
  111. freightModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  112. });
  113. functions.push(function(cb){
  114. originalModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  115. });
  116. functions.push(function(cb){
  117. comElectrovalenceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  118. });
  119. }
  120. //彻底删除了的费率文件
  121. let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  122. let junkFFIds = [];
  123. for(let jFF of junkFFs){
  124. junkFFIds.push(jFF.feeRateID);
  125. }
  126. if(junkFFIds.length > 0){
  127. functions.push(function(cb){
  128. feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
  129. });
  130. }
  131. //清除
  132. if(functions.length > 0){
  133. async.parallel(functions, async function(err, result){
  134. if(!err){
  135. //清除项目
  136. await projectModel.remove({ID: {$in: junkProjIds}});
  137. //清除单价文件
  138. await unitPriceFileModel.remove({id: {$in: junkUFIds}});
  139. //清除费率文件
  140. await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
  141. }
  142. if(callback) callback(err);
  143. });
  144. }
  145. else {
  146. if(callback) callback(0);
  147. }
  148. }
  149. //删除假删除数据
  150. /*
  151. * 1.之前的删除造价书数据,都是做的假删除,这部分数据会一直存在并且随着项目重复使用越来越多,现要改为真删除,所以做这个清除功能。
  152. * 2.原假删除包括清单的所有类型(大项费用、分部、分项、补项、清单)、定额的所有类型(定额、数量单价、与定额同级的人材机)
  153. * */
  154. async function clearFakeData(callback) {
  155. let functions = [];
  156. //删除清单
  157. functions.push(function (cb) {
  158. billsModel.remove({deleteInfo: {$exists: true}}, cb);
  159. });
  160. //删除定额
  161. functions.push(function (cb) {
  162. rationModel.remove({deleteInfo: {$exists: true}}, cb);
  163. });
  164. //删除补充定额章节树
  165. functions.push(function (cb) {
  166. compleRationSection.remove({deleteInfo: {$exists: true}}, cb);
  167. });
  168. async.parallel(functions, async function(err, result){
  169. if(callback){
  170. callback(err);
  171. }
  172. });
  173. }
  174. const sysSchedule = {
  175. clearJunkData,
  176. clearFakeData
  177. };
  178. //export {sysSchedule as default}
  179. module.exports = sysSchedule;