UserNotify.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Created by Administrator on 2015/4/15.
  3. * 文章标签对象
  4. */
  5. var mongoose = require('mongoose');
  6. var shortid = require('shortid');
  7. var Schema = mongoose.Schema;
  8. var User = require('./User');
  9. var Notify = require('./Notify');
  10. var UserNotifySchema = new Schema({
  11. _id: {
  12. type: String,
  13. unique: true,
  14. 'default': shortid.generate
  15. },
  16. isRead : {type: Boolean, default: false},
  17. user : {type: String, ref: 'User'}, // 用户消息所属者
  18. systemUser : { type: String, ref: 'AdminUser' }, // 用户消息所属者
  19. notify : {type: String, ref : 'Notify'}, // 关联的Notify
  20. date : { type: Date, default: Date.now }
  21. });
  22. UserNotifySchema.index({date: -1});
  23. UserNotifySchema.statics = {
  24. addNotifyByUsers : function(res,users,notify){
  25. if(users.length > 0){
  26. for(var i=0;i<users.length;i++){
  27. var userNotify = new UserNotify();
  28. userNotify.systemUser = users[i]._id;
  29. userNotify.notify = notify;
  30. userNotify.save(function(err){
  31. if(err){
  32. res.end(err);
  33. }
  34. });
  35. }
  36. }
  37. },
  38. setHasRead : function(msgId,callback){
  39. var idObj = msgId.split(',');
  40. var query ;
  41. if(idObj.length > 1){
  42. query = {'_id':{$in: idObj}};
  43. }else{
  44. query = {'_id':idObj[0]};
  45. }
  46. UserNotify.update(query,{$set:{'isRead':true}},{ multi: true },callback);
  47. },
  48. getNoReadNotifyCountByUserId : function(userId,type,callBack){
  49. if(userId){
  50. var msgQuery = {};
  51. if(type == 'user'){
  52. msgQuery = {'user': userId , 'isRead' : false};
  53. }else if(type == 'adminUser'){
  54. msgQuery = {'systemUser': userId , 'isRead' : false};
  55. }
  56. UserNotify.count(msgQuery,callBack);
  57. }else{
  58. callBack(0);
  59. }
  60. },
  61. getNotifyPaginationResult : function(req,res,userId){
  62. var msgQuery = {'user': userId };
  63. var page = parseInt(req.query.page);
  64. var limit = parseInt(req.query.limit);
  65. if (!page) page = 1;
  66. if (!limit) limit = 15;
  67. var order = req.query.order;
  68. var sq = {}, Str, A = 'problemID', B = 'asc';
  69. if (order) { //是否有排序请求
  70. Str = order.split('_');
  71. A = Str[0]; B = Str[1];
  72. sq[A] = B; //关联数组增加查询条件,更加灵活,因为A是变量
  73. } else {
  74. sq.date = -1; //默认排序查询条件
  75. }
  76. var startNum = (page - 1)*limit;
  77. var resultList = UserNotify.find(msgQuery).sort(sq).skip(startNum).limit(limit).populate('user').populate('notify').exec();
  78. var resultNum = UserNotify.find(msgQuery).count();
  79. // 分页参数
  80. var pageInfo = {
  81. "totalItems" : resultNum,
  82. "currentPage" : page,
  83. "limit" : limit,
  84. "startNum" : startNum +1
  85. };
  86. var datasInfo = {
  87. docs : resultList,
  88. pageInfo : pageInfo
  89. };
  90. return datasInfo;
  91. }
  92. };
  93. var UserNotify = mongoose.model("UserNotify",UserNotifySchema);
  94. module.exports = UserNotify;