sys_model.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. import moment from 'moment';
  12. const SMS = require('../../users/models/sms');
  13. const projectModel = mongoose.model('projects');
  14. const projSettingModel = mongoose.model('proj_setting');
  15. const calcProgramModel = mongoose.model('calc_programs');
  16. const labourCoeModel = mongoose.model('labour_coes');
  17. const billsModel = mongoose.model('bills');
  18. const rationModel = mongoose.model('ration');
  19. const projGljModel = mongoose.model('glj_list');
  20. const rationGljModel = mongoose.model('ration_glj');
  21. const rationCoeMolde = mongoose.model('ration_coe');
  22. const installationModel = mongoose.model('installation_fee');
  23. const rationInstallationModel = mongoose.model('ration_installation');
  24. const rationTemplateModel = mongoose.model('ration_template');
  25. const quantityDetailModel = mongoose.model('quantity_detail');
  26. const unitPriceFileModel = mongoose.model('unit_price_file');
  27. const unitPriceModel = mongoose.model('unit_price');
  28. const mixRatioModel = mongoose.model('mix_ratio');
  29. const feeRateFileModel = mongoose.model('fee_rate_file');
  30. const feeRateModel = mongoose.model('fee_rates');
  31. const compleRationSection = mongoose.model('complementary_ration_section_tree');
  32. const evaluateListModel = mongoose.model("evaluate_list");
  33. const bidListModel = mongoose.model("bid_evaluation_list");
  34. const contractorListModel = mongoose.model("contractor_list");
  35. const shareListModel = mongoose.model('share_list');
  36. const userModel = mongoose.model('users');
  37. const compilationModel = mongoose.model('compilation');
  38. //删除垃圾数据
  39. async function clearJunkData(callback){
  40. let functions = [];
  41. //获取彻底删除了的项目
  42. let junkProjs = await projectModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true}, '-_id ID').lean();
  43. let junkProjIds = [];
  44. for(let jProj of junkProjs){
  45. junkProjIds.push(jProj.ID);
  46. }
  47. if(junkProjIds.length > 0){
  48. //清除proj_setting
  49. functions.push(function(cb){
  50. projSettingModel.remove({projectID: {$in: junkProjIds}}, cb);
  51. });
  52. //清除calcProgram
  53. functions.push(function(cb){
  54. calcProgramModel.remove({projectID: {$in: junkProjIds}}, cb);
  55. });
  56. //清除labourCoe
  57. functions.push(function(cb){
  58. labourCoeModel.remove({projectID: {$in: junkProjIds}}, cb);
  59. });
  60. //清除bills
  61. functions.push(function(cb){
  62. billsModel.remove({projectID: {$in: junkProjIds}}, cb);
  63. });
  64. //清除ration
  65. functions.push(function(cb){
  66. rationModel.remove({projectID: {$in: junkProjIds}}, cb);
  67. });
  68. //清除project_glj
  69. functions.push(function(cb){
  70. projGljModel.remove({project_id: {$in: junkProjIds}}, cb);
  71. });
  72. //清除ration_glj
  73. functions.push(function(cb){
  74. rationGljModel.remove({projectID: {$in: junkProjIds}}, cb);
  75. });
  76. //清除ration_coe
  77. functions.push(function(cb){
  78. rationCoeMolde.remove({projectID: {$in: junkProjIds}}, cb);
  79. });
  80. //清除installation_fee
  81. functions.push(function(cb){
  82. installationModel.remove({projectID: {$in: junkProjIds}}, cb);
  83. });
  84. //清除ration_installation
  85. functions.push(function(cb){
  86. rationInstallationModel.remove({projectID: {$in: junkProjIds}}, cb);
  87. });
  88. //清除ration_installation
  89. functions.push(function(cb){
  90. rationTemplateModel.remove({projectID: {$in: junkProjIds}}, cb);
  91. });
  92. //清除quantity_detail
  93. functions.push(function(cb){
  94. quantityDetailModel.remove({projectID: {$in: junkProjIds}}, cb);
  95. });
  96. //清除暂估材料
  97. functions.push(function(cb){
  98. evaluateListModel.remove({projectID: {$in: junkProjIds}}, cb);
  99. });
  100. //清除评标材料
  101. functions.push(function(cb){
  102. bidListModel.remove({projectID: {$in: junkProjIds}}, cb);
  103. });
  104. //清除承包材料
  105. functions.push(function(cb){
  106. contractorListModel.remove({projectID: {$in: junkProjIds}}, cb);
  107. });
  108. }
  109. //彻底删除了的单价文件
  110. let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  111. let junkUFIds = [];
  112. for(let jUF of junkUFs){
  113. junkUFIds.push(jUF.id);
  114. }
  115. if(junkUFIds.length > 0){
  116. functions.push(function(cb){
  117. unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  118. });
  119. functions.push(function(cb){
  120. mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  121. });
  122. }
  123. //彻底删除了的费率文件
  124. let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  125. let junkFFIds = [];
  126. for(let jFF of junkFFs){
  127. junkFFIds.push(jFF.feeRateID);
  128. }
  129. if(junkFFIds.length > 0){
  130. functions.push(function(cb){
  131. feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
  132. });
  133. }
  134. //清除
  135. if(functions.length > 0){
  136. async.parallel(functions, async function(err, result){
  137. if(!err){
  138. //清除项目
  139. await projectModel.remove({ID: {$in: junkProjIds}});
  140. // 删除分享信息
  141. await shareListModel.remove({projectID: {$in: junkProjIds}});
  142. //清除单价文件
  143. await unitPriceFileModel.remove({id: {$in: junkUFIds}});
  144. //清除费率文件
  145. await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
  146. } else {
  147. console.log(err);
  148. }
  149. if(callback) callback(err);
  150. });
  151. }
  152. else {
  153. if(callback) callback(0);
  154. }
  155. }
  156. //删除假删除数据
  157. /*
  158. * 1.之前的删除造价书数据,都是做的假删除,这部分数据会一直存在并且随着项目重复使用越来越多,现要改为真删除,所以做这个清除功能。
  159. * 2.原假删除包括清单的所有类型(大项费用、分部、分项、补项、清单)、定额的所有类型(定额、数量单价、与定额同级的人材机)
  160. * */
  161. async function clearFakeData(callback) {
  162. let functions = [];
  163. //删除清单
  164. functions.push(function (cb) {
  165. billsModel.remove({deleteInfo: {$exists: true}}, cb);
  166. });
  167. //删除定额
  168. functions.push(function (cb) {
  169. rationModel.remove({deleteInfo: {$exists: true}}, cb);
  170. });
  171. //删除补充定额章节树
  172. functions.push(function (cb) {
  173. compleRationSection.remove({deleteInfo: {$exists: true}}, cb);
  174. });
  175. async.parallel(functions, async function(err, result){
  176. if(callback){
  177. callback(err);
  178. }
  179. });
  180. }
  181. /*
  182. * 每天定时清理用户限期的专业版编办,降为免费公用版。
  183. * */
  184. async function checkUserCompilationStatus(callback) {
  185. let functions = [];
  186. let today = moment(new Date()).format('YYYY-MM-DD');
  187. let userList = await userModel.find({upgrade_list: {$elemMatch:{ deadline: today }}});
  188. if (userList.length > 0) {
  189. for (let user of userList) {
  190. let ssoId = JSON.parse(JSON.stringify(user)).ssoId; // 坑啊,没法直接获取到user.ssoId,要转义
  191. for (let cul of user.upgrade_list) {
  192. if (cul.deadline === today) {
  193. // cul.deadline = '';
  194. cul.isUpgrade = false;
  195. }
  196. }
  197. functions.push(function (cb) {
  198. userModel.update({ssoId: ssoId}, {upgrade_list: user.upgrade_list}, { safe: true }, cb);
  199. });
  200. }
  201. }
  202. if(functions.length > 0){
  203. async.parallel(functions, async function(err, result){
  204. if(callback){
  205. callback(err);
  206. }
  207. });
  208. } else {
  209. if(callback) callback(0);
  210. }
  211. }
  212. /*
  213. * 为每天降为免费公用版的用户发送降级短信。
  214. * */
  215. // async function sendCompilationStatusSms(callback) {
  216. // let functions = [];
  217. // let today = moment(new Date()).format('YYYY-MM-DD');
  218. // let userList = await userModel.find({upgrade_list: {$elemMatch:{ deadline: today }}});
  219. // if (userList.length > 0) {
  220. // const Sms = new SMS();
  221. // for (let user of userList) {
  222. // // let ssoId = JSON.parse(JSON.stringify(user)).ssoId;
  223. // for (let cul of user.upgrade_list) {
  224. // if (cul.deadline === today) {
  225. // // cul.deadline = '';
  226. // // cul.isUpgrade = false;
  227. // // 发送短信
  228. // let compilationData = await compilationModel.findOne({_id: cul.compilationID});
  229. // await Sms.sendProductMsg(user.mobile, 2, user.real_name, compilationData.name, '');
  230. // }
  231. // }
  232. // // functions.push(function (cb) {
  233. // // userModel.update({ssoId: ssoId}, {upgrade_list: user.upgrade_list}, { safe: true }, cb);
  234. // // });
  235. // }
  236. // }
  237. // if(functions.length > 0){
  238. // async.parallel(functions, async function(err, result){
  239. // if(callback){
  240. // callback(err);
  241. // }
  242. // });
  243. // } else {
  244. // if(callback) callback(0);
  245. // }
  246. // }
  247. const sysSchedule = {
  248. clearJunkData,
  249. clearFakeData,
  250. checkUserCompilationStatus,
  251. // sendCompilationStatusSms,
  252. };
  253. //export {sysSchedule as default}
  254. module.exports = sysSchedule;