123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const moment = require('moment');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class LookuserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'lookuser');
- this.model = this.ctx.model.Lookuser;
- this.rmodel = this.ctx.model.Lookrecord;
- this.umodel = this.ctx.model.Roomuser;
- }
- async create(data) {
- const { roomname, userid } = data;
- const lookuser = await this.model.findOne({ roomname, userid });
- const time = moment().format('YYYY-MM-DD HH:mm:ss');
- if (!lookuser) {
- data.createtime = time;
- await this.model.create(data);
- }
- const newdata = { roomid: data.roomid, roomname: data.roomname, userid, username: data.username, createtime: time, type: '0' };
- await this.rmodel.create(newdata);
- }
- async update({ id }, data) {
- const { switchrole } = data;
- const lookuser = await this.model.findById(id);
- if (!lookuser) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- }
- lookuser.switchrole = switchrole;
- const res = await lookuser.save();
- if (res) {
- const { mq } = this.ctx;
- if (mq) {
- const exchange = 'switch_role_' + res.userid;
- const parm = {
- durable: true,
- headers: {
- userid: res.userid,
- } };
- await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
- }
- }
- return res;
- }
- async roomcount(data) {
- const { roomname } = data;
- const total = await this.model.count({ roomname });
- return { total };
- }
- async userswichrole(data) {
- const { roomid, userid } = data;
- const res = await this.model.findOne({ roomid, userid });
- return res;
- }
- async updatexf(data) {
- const { roomid, isxf, userid, hosname, deptname, level, major } = data;
- const lookuser = await this.model.findOne({ roomid, userid });
- if (!lookuser) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- }
- lookuser.isxf = isxf;
- lookuser.hosname = hosname;
- lookuser.deptname = deptname;
- lookuser.level = level;
- lookuser.major = major;
- const res = await lookuser.save();
- return res;
- }
- async query({ skip, limit, ...info }) {
- const total = await this.model.count(info);
- const lusers = await this.model
- .find(info)
- .skip(Number(skip))
- .limit(Number(limit));
- const data = [];
- for (const _luser of lusers) {
- const luser = _.cloneDeep(JSON.parse(JSON.stringify(_luser)));
- const user = await this.umodel.findById(_luser.userid);
- if (user) {
- luser.idnumber = user.number;
- luser.isjc = user.isjc;
- luser.isxf = user.isxf;
- luser.address = user.address;
- luser.age = user.age;
- luser.gender = user.gender;
- if (user.hosname) {
- luser.hosname = user.hosname;
- }
- if (user.deptname) {
- luser.deptname = user.deptname;
- }
- if (user.level) {
- luser.level = user.level;
- }
- if (user.major) {
- luser.major = user.major;
- }
- }
- const lrecords = await this.rmodel.find({ roomid: _luser.roomid, userid: _luser.userid }).sort({ createtime: -1 }).limit(1);
- if (lrecords.length > 0) {
- const lrecord = lrecords[0];
- luser.exittime = lrecord.createtime;
- const start_date = moment(luser.createtime, 'YYYY-MM-DD HH:mm:ss');
- const end_date = moment(lrecord.createtime, 'YYYY-MM-DD HH:mm:ss');
- // 秒
- const seconds = end_date.diff(start_date, 'seconds');
- // 分钟
- const mintus = (seconds / 60);
- luser.looktime = mintus;
- }
- data.push(luser);
- }
- return { data, total };
- }
- }
- module.exports = LookuserService;
|