sys_model.js 10 KB

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