lookuser.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const moment = require('moment');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class LookuserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lookuser');
  11. this.model = this.ctx.model.Lookuser;
  12. this.rmodel = this.ctx.model.Lookrecord;
  13. }
  14. async create(data) {
  15. const { roomname, userid } = data;
  16. const lookuser = await this.model.findOne({ roomname, userid });
  17. const time = moment().format('YYYY-MM-DD HH:mm:ss');
  18. if (!lookuser) {
  19. data.createtime = time;
  20. await this.model.create(data);
  21. }
  22. const newdata = { roomid: data.roomid, roomname: data.roomname, userid, username: data.username, createtime: time, type: '0' };
  23. await this.rmodel.create(newdata);
  24. }
  25. async update({ id }, data) {
  26. const { switchrole } = data;
  27. const lookuser = await this.model.findById(id);
  28. if (!lookuser) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  30. }
  31. lookuser.switchrole = switchrole;
  32. const res = await lookuser.save();
  33. if (res) {
  34. const { mq } = this.ctx;
  35. if (mq) {
  36. const exchange = 'switch_role_' + res.userid;
  37. const parm = {
  38. durable: true,
  39. headers: {
  40. userid: res.userid,
  41. } };
  42. await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
  43. }
  44. }
  45. return res;
  46. }
  47. async roomcount(data) {
  48. const { roomname } = data;
  49. const total = await this.model.count({ roomname });
  50. return { total };
  51. }
  52. async userswichrole(data) {
  53. const { roomid, userid } = data;
  54. const res = await this.model.findOne({ roomid, userid });
  55. return res;
  56. }
  57. async updatexf(data) {
  58. const { roomid, isxf, userid, hosname, deptname, level, major } = data;
  59. const lookuser = await this.model.findOne({ roomid, userid });
  60. if (!lookuser) {
  61. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  62. }
  63. lookuser.isxf = isxf;
  64. lookuser.hosname = hosname;
  65. lookuser.deptname = deptname;
  66. lookuser.level = level;
  67. lookuser.major = major;
  68. const res = await lookuser.save();
  69. return res;
  70. }
  71. }
  72. module.exports = LookuserService;