'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class RoomService extends CrudService { constructor(ctx) { super(ctx, 'room'); this.model = this.ctx.model.Room; this.rumodel = this.ctx.model.Roomuser; this.lookmodel = this.ctx.model.Lookuser; } async create(data) { const { name } = data; const room_ = await this.model.findOne({ name }); if (room_) { throw new BusinessError('此房间已经存在,请重新输入'); } const roomuser = await this.rumodel.findById(data.anchorid); if (!roomuser) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST); } data.username = roomuser.name; const res = await this.model.create(data); return res; } async findroomname(data) { const { roomname } = data; const res = await this.model.findOne({ name: roomname }); return res; } async query({ skip, limit, ...info }) { const total = await this.model.count(info); const rooms = await this.model .find(info) .skip(Number(skip)) .limit(Number(limit)); const data = []; for (const _room of rooms) { const room = _.cloneDeep(JSON.parse(JSON.stringify(_room))); const number = await this.lookmodel.count({ roomid: room._id }); room.number = number; data.push(room); } return { data, total }; } // 接收录播地址 async startrecord(data) { // 接收录播参数 const { stream_id } = data; console.log('视频回调--->' + data); if (stream_id) { const arr = stream_id.split('_'); const id = arr[1]; console.log('id--->' + id); const res = await this.model.findById(id); if (res) { res.stream_id = stream_id; res.url = data.video_url; res.file_id = data.file_id; res.ishis = '1'; await res.save(); } } } async updaterequid(id, requestid) { const res = await this.model.findById(id); if (res) { res.requestId = requestid; await res.save(); } return res; } async updateanchor(id, otheid) { const res = await this.model.findById(id); if (res) { res.otheid = otheid; const result = await res.save(); if (result) { const { mq } = this.ctx; if (mq) { const exchange = 'othe_publish_' + result.id; const parm = { durable: true, headers: { userid: result.id, } }; await mq.fanout(exchange, result.id, JSON.stringify(result), parm); } } } return res; } async roomquest(data) { const res = await this.model.findById(data.roomid); if (!res) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST); } res.queid = data.queid; res.isque = '1'; const result = await res.save(); if (result) { const { mq } = this.ctx; if (mq) { const exchange = 'quest_publish_' + res.id; const parm = { durable: true, headers: { userid: res.id, } }; await mq.fanout(exchange, res.id, JSON.stringify(res), parm); } } return result; } async roomquestclose(data) { const res = await this.model.findById(data.roomid); if (!res) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST); } res.isque = '0'; const result = await res.save(); return result; } async updateshmai({ id, shmaiid }) { const res = await this.model.findById(id); if (res) { res.shmaiid = shmaiid; const result = await res.save(); if (result) { const { mq } = this.ctx; if (mq) { const exchange = 'switch_shmai_' + id; const parm = { durable: true, headers: { userid: res.id, } }; await mq.fanout(exchange, res.id, JSON.stringify(res), parm); } } } return res; } async switchzjr({ id, switchzjr }) { const resmodel = await this.model.findById(id); if (resmodel) { resmodel.switchzjr = switchzjr; const result = await resmodel.save(); if (result) { const { mq } = this.ctx; if (mq) { const exchange = 'switch_zjr_' + id; const parm = { durable: true, headers: { userid: result.id, } }; await mq.fanout(exchange, result.id, JSON.stringify(result), parm); } } } return resmodel; } } module.exports = RoomService;