'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; } 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 } = data; const lookuser = await this.model.findOne({ roomid, userid }); if (!lookuser) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST); } lookuser.isxf = isxf; const res = await lookuser.save(); return res; } } module.exports = LookuserService;