rpt_util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Created by Tony on 2017/3/24.
  3. */
  4. let mongoose = require('mongoose');
  5. //let rpt_cfg_mdl = mongoose.model('rpt_cfg');
  6. let cache = require('../../../public/cache/cacheUtil');
  7. //let Rpt_Cfg_Mdl = require('../models/rpt_cfg');
  8. let Rpt_Cfg_Mdl = mongoose.model('rpt_cfg');
  9. const RPT_CFG_GRP = 'rpt_cfg';
  10. module.exports = {
  11. setReportDefaultCache: function () {
  12. Rpt_Cfg_Mdl.find({userId: "Administrator"}, '-_id', function(err, cfgs){
  13. if(cfgs.length){
  14. cache.setCache(RPT_CFG_GRP,'admin_cfg',cfgs[0]);
  15. }
  16. })
  17. },
  18. getReportDefaultCache: function () {
  19. let rst = {ctrls: null, fonts: null, styles: null},
  20. admin_cfg = cache.getCache(RPT_CFG_GRP,'admin_cfg');
  21. ;
  22. rst.ctrls = admin_cfg.formats;
  23. rst.fonts = admin_cfg.fonts;
  24. rst.styles = admin_cfg.borders;
  25. admin_cfg = null;
  26. return rst;
  27. },
  28. getAvailablePageSizeCache: function() {
  29. const admin_cfg = cache.getCache(RPT_CFG_GRP,'admin_cfg');
  30. let rst = [];
  31. if (admin_cfg.hasOwnProperty('papers')) {
  32. rst.concat(admin_cfg.papers);
  33. } else {
  34. rst.push('A4');
  35. rst.push('A3');
  36. rst.push('LETTER');
  37. }
  38. return rst;
  39. },
  40. setReportCacheByUser: function (userId) {
  41. let me = this;
  42. let user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  43. if (!(user_cfg)) {
  44. Rpt_Cfg_Mdl.find({userId: userId}, '-_id', function(err, cfgs){
  45. if(cfgs.length){
  46. cache.setCache(RPT_CFG_GRP, userId + '_cfg',cfgs[0]);
  47. } else {
  48. me.setReportDefaultCache();
  49. }
  50. })
  51. }
  52. },
  53. getReportCacheByUser: function (userId, cb) {
  54. let me = this,
  55. rst = {ctrls: null, fonts: null, styles: null},
  56. user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  57. ;
  58. if (!(user_cfg)) {
  59. Rpt_Cfg_Mdl.find({userId: userId}, '-_id', function(err, cfgs){
  60. if(cfgs.length){
  61. cache.setCache(RPT_CFG_GRP, userId + '_cfg',cfgs[0]);
  62. user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  63. rst.ctrls = user_cfg.formats;
  64. rst.fonts = user_cfg.fonts;
  65. rst.styles = user_cfg.borders;
  66. user_cfg = null;
  67. cb(rst);
  68. } else {
  69. user_cfg = null;
  70. cb(me.getReportDefaultCache());
  71. }
  72. })
  73. } else {
  74. rst.ctrls = user_cfg.formats;
  75. rst.fonts = user_cfg.fonts;
  76. rst.styles = user_cfg.borders;
  77. user_cfg = null;
  78. cb(rst);
  79. }
  80. }
  81. }