sys_model.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. //删除垃圾数据
  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. divideModel.remove({projectID: {$in: junkProjIds}}, cb);
  99. });
  100. }
  101. //彻底删除了的单价文件
  102. let junkUFs = await unitPriceFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  103. let junkUFIds = [];
  104. for(let jUF of junkUFs){
  105. junkUFIds.push(jUF.id);
  106. }
  107. if(junkUFIds.length > 0){
  108. functions.push(function(cb){
  109. unitPriceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  110. });
  111. functions.push(function(cb){
  112. mixRatioModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  113. });
  114. functions.push(function(cb){
  115. freightModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  116. });
  117. functions.push(function(cb){
  118. originalModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  119. });
  120. functions.push(function(cb){
  121. comElectrovalenceModel.remove({unit_price_file_id: {$in: junkUFIds}}, cb);
  122. });
  123. }
  124. //彻底删除了的费率文件
  125. let junkFFs = await feeRateFileModel.find({'deleteInfo.deleted': true, 'deleteInfo.completeDeleted': true});
  126. let junkFFIds = [];
  127. for(let jFF of junkFFs){
  128. junkFFIds.push(jFF.feeRateID);
  129. }
  130. if(junkFFIds.length > 0){
  131. functions.push(function(cb){
  132. feeRateModel.remove({ID: {$in: junkFFIds}}, cb);
  133. });
  134. }
  135. //清除
  136. if(functions.length > 0){
  137. async.parallel(functions, async function(err, result){
  138. if(!err){
  139. //清除项目
  140. await projectModel.remove({ID: {$in: junkProjIds}});
  141. //清除单价文件
  142. await unitPriceFileModel.remove({id: {$in: junkUFIds}});
  143. //清除费率文件
  144. await feeRateFileModel.remove({feeRateID: {$in: junkFFIds}});
  145. }
  146. if(callback) callback(err);
  147. });
  148. }
  149. else {
  150. if(callback) callback(0);
  151. }
  152. }
  153. //删除假删除数据
  154. /*
  155. * 1.之前的删除造价书数据,都是做的假删除,这部分数据会一直存在并且随着项目重复使用越来越多,现要改为真删除,所以做这个清除功能。
  156. * 2.原假删除包括清单的所有类型(大项费用、分部、分项、补项、清单)、定额的所有类型(定额、数量单价、与定额同级的人材机)
  157. * */
  158. async function clearFakeData(callback) {
  159. let functions = [];
  160. //删除清单
  161. functions.push(function (cb) {
  162. billsModel.remove({deleteInfo: {$exists: true}}, cb);
  163. });
  164. //删除定额
  165. functions.push(function (cb) {
  166. rationModel.remove({deleteInfo: {$exists: true}}, cb);
  167. });
  168. //删除补充定额章节树
  169. functions.push(function (cb) {
  170. compleRationSection.remove({deleteInfo: {$exists: true}}, cb);
  171. });
  172. async.parallel(functions, async function(err, result){
  173. if(callback){
  174. callback(err);
  175. }
  176. });
  177. }
  178. /*
  179. * 每天定时清理用户限期的专业版编办,降为免费公用版。
  180. * */
  181. async function checkUserCompilationStatus(callback) {
  182. let functions = [];
  183. let today = moment(new Date()).format('YYYY-MM-DD');
  184. let userList = await userModel.find({upgrade_list: {$elemMatch:{ deadline: today }}});
  185. if (userList.length > 0) {
  186. for (let user of userList) {
  187. let ssoId = JSON.parse(JSON.stringify(user)).ssoId; // 坑啊,没法直接获取到user.ssoId,要转义
  188. for (let cul of user.upgrade_list) {
  189. if (cul.deadline === today) {
  190. // cul.deadline = '';
  191. cul.isUpgrade = false;
  192. }
  193. }
  194. functions.push(function (cb) {
  195. userModel.update({ssoId: ssoId}, {upgrade_list: user.upgrade_list}, { safe: true }, cb);
  196. });
  197. }
  198. }
  199. if(functions.length > 0){
  200. async.parallel(functions, async function(err, result){
  201. if(callback){
  202. callback(err);
  203. }
  204. });
  205. } else {
  206. if(callback) callback(0);
  207. }
  208. }
  209. /*
  210. * 为每天降为免费公用版的用户发送降级短信。
  211. * */
  212. async function sendCompilationStatusSms(callback) {
  213. let functions = [];
  214. let today = moment(new Date()).format('YYYY-MM-DD');
  215. let userList = await userModel.find({upgrade_list: {$elemMatch:{ deadline: today }}});
  216. if (userList.length > 0) {
  217. const Sms = new SMS();
  218. for (let user of userList) {
  219. let ssoId = JSON.parse(JSON.stringify(user)).ssoId;
  220. for (let cul of user.upgrade_list) {
  221. if (cul.deadline === today) {
  222. cul.deadline = '';
  223. // cul.isUpgrade = false;
  224. // 发送短信
  225. let compilationData = await compilationModel.findOne({_id: cul.compilationID});
  226. await Sms.sendProductMsg(user.mobile, 2, user.real_name, compilationData.name, '');
  227. }
  228. }
  229. functions.push(function (cb) {
  230. userModel.update({ssoId: ssoId}, {upgrade_list: user.upgrade_list}, { multi: true }, cb);
  231. });
  232. }
  233. }
  234. if(functions.length > 0){
  235. async.parallel(functions, async function(err, result){
  236. if(callback){
  237. callback(err);
  238. }
  239. });
  240. } else {
  241. if(callback) callback(0);
  242. }
  243. }
  244. const sysSchedule = {
  245. clearJunkData,
  246. clearFakeData,
  247. checkUserCompilationStatus,
  248. sendCompilationStatusSms,
  249. };
  250. //export {sysSchedule as default}
  251. module.exports = sysSchedule;