rpt_util.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. setReportCacheByUser: function (userId) {
  29. let me = this;
  30. let user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  31. if (!(user_cfg)) {
  32. Rpt_Cfg_Mdl.find({userId: userId}, '-_id', function(err, cfgs){
  33. if(cfgs.length){
  34. cache.setCache(RPT_CFG_GRP, userId + '_cfg',cfgs[0]);
  35. } else {
  36. me.setReportDefaultCache();
  37. }
  38. })
  39. }
  40. },
  41. getReportCacheByUser: function (userId, cb) {
  42. let me = this,
  43. rst = {ctrls: null, fonts: null, styles: null},
  44. user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  45. ;
  46. if (!(user_cfg)) {
  47. Rpt_Cfg_Mdl.find({userId: userId}, '-_id', function(err, cfgs){
  48. if(cfgs.length){
  49. cache.setCache(RPT_CFG_GRP, userId + '_cfg',cfgs[0]);
  50. user_cfg = cache.getCache(RPT_CFG_GRP,userId + '_cfg');
  51. rst.ctrls = user_cfg.formats;
  52. rst.fonts = user_cfg.fonts;
  53. rst.styles = user_cfg.borders;
  54. user_cfg = null;
  55. cb(rst);
  56. } else {
  57. user_cfg = null;
  58. cb(me.getReportDefaultCache());
  59. }
  60. })
  61. } else {
  62. rst.ctrls = user_cfg.formats;
  63. rst.fonts = user_cfg.fonts;
  64. rst.styles = user_cfg.borders;
  65. user_cfg = null;
  66. cb(rst);
  67. }
  68. }
  69. }