lookuser.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. this.umodel = this.ctx.model.Roomuser;
  14. }
  15. async create(data) {
  16. const { roomname, userid } = data;
  17. const lookuser = await this.model.findOne({ roomname, userid });
  18. const time = moment().format('YYYY-MM-DD HH:mm:ss');
  19. if (!lookuser) {
  20. data.createtime = time;
  21. await this.model.create(data);
  22. }
  23. const newdata = { roomid: data.roomid, roomname: data.roomname, userid, username: data.username, createtime: time, type: '0' };
  24. await this.rmodel.create(newdata);
  25. }
  26. async update({ id }, data) {
  27. const { switchrole } = data;
  28. const lookuser = await this.model.findById(id);
  29. if (!lookuser) {
  30. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  31. }
  32. lookuser.switchrole = switchrole;
  33. const res = await lookuser.save();
  34. if (res) {
  35. const { mq } = this.ctx;
  36. if (mq) {
  37. const exchange = 'switch_role_' + res.userid;
  38. const parm = {
  39. durable: true,
  40. headers: {
  41. userid: res.userid,
  42. } };
  43. await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
  44. }
  45. }
  46. return res;
  47. }
  48. async roomcount(data) {
  49. const { roomname } = data;
  50. const total = await this.model.count({ roomname });
  51. return { total };
  52. }
  53. async userswichrole(data) {
  54. const { roomid, userid } = data;
  55. const res = await this.model.findOne({ roomid, userid });
  56. return res;
  57. }
  58. async updatexf(data) {
  59. const { roomid, isxf, userid, hosname, deptname, level, major } = data;
  60. const lookuser = await this.model.findOne({ roomid, userid });
  61. if (!lookuser) {
  62. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  63. }
  64. lookuser.isxf = isxf;
  65. lookuser.hosname = hosname;
  66. lookuser.deptname = deptname;
  67. lookuser.level = level;
  68. lookuser.major = major;
  69. const res = await lookuser.save();
  70. return res;
  71. }
  72. async query({ skip, limit, ...info }) {
  73. const total = await this.model.count(info);
  74. const lusers = await this.model
  75. .find(info)
  76. .skip(Number(skip))
  77. .limit(Number(limit));
  78. const data = [];
  79. for (const _luser of lusers) {
  80. const luser = _.cloneDeep(JSON.parse(JSON.stringify(_luser)));
  81. const user = await this.umodel.findById(_luser.userid);
  82. if (user) {
  83. luser.idnumber = user.number;
  84. luser.isjc = user.isjc;
  85. luser.isxf = user.isxf;
  86. luser.address = user.address;
  87. luser.age = user.age;
  88. luser.gender = user.gender;
  89. if (user.hosname) {
  90. luser.hosname = user.hosname;
  91. }
  92. if (user.deptname) {
  93. luser.deptname = user.deptname;
  94. }
  95. if (user.level) {
  96. luser.level = user.level;
  97. }
  98. if (user.major) {
  99. luser.major = user.major;
  100. }
  101. }
  102. const lrecords = await this.rmodel.find({ roomid: _luser.roomid, userid: _luser.userid }).sort({ createtime: -1 }).limit(1);
  103. if (lrecords.length > 0) {
  104. const lrecord = lrecords[0];
  105. luser.exittime = lrecord.createtime;
  106. const start_date = moment(luser.createtime, 'YYYY-MM-DD HH:mm:ss');
  107. const end_date = moment(lrecord.createtime, 'YYYY-MM-DD HH:mm:ss');
  108. // 秒
  109. const seconds = end_date.diff(start_date, 'seconds');
  110. // 分钟
  111. const mintus = (seconds / 60);
  112. luser.looktime = mintus;
  113. }
  114. data.push(luser);
  115. }
  116. return { data, total };
  117. }
  118. }
  119. module.exports = LookuserService;