'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; 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(); } return res; } async updaterequid(id, requestid) { const res = await this.model.findById(id); if (res) { res.requestId = requestid; await res.save(); } return res; } } module.exports = RoomService;