room.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class RoomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'room');
  10. this.model = this.ctx.model.Room;
  11. this.rumodel = this.ctx.model.Roomuser;
  12. this.lookmodel = this.ctx.model.Lookuser;
  13. }
  14. async create(data) {
  15. const { name } = data;
  16. const room_ = await this.model.findOne({ name });
  17. if (room_) {
  18. throw new BusinessError('此房间已经存在,请重新输入');
  19. }
  20. const roomuser = await this.rumodel.findById(data.anchorid);
  21. if (!roomuser) {
  22. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  23. }
  24. data.username = roomuser.name;
  25. const res = await this.model.create(data);
  26. return res;
  27. }
  28. async findroomname(data) {
  29. const { roomname } = data;
  30. const res = await this.model.findOne({ name: roomname });
  31. return res;
  32. }
  33. async query({ skip, limit, ...info }) {
  34. const total = await this.model.count(info);
  35. const rooms = await this.model
  36. .find(info)
  37. .skip(Number(skip))
  38. .limit(Number(limit));
  39. const data = [];
  40. for (const _room of rooms) {
  41. const room = _.cloneDeep(JSON.parse(JSON.stringify(_room)));
  42. const number = await this.lookmodel.count({ roomid: room._id });
  43. room.number = number;
  44. data.push(room);
  45. }
  46. return { data, total };
  47. }
  48. // 接收录播地址
  49. async startrecord(data) {
  50. // 接收录播参数
  51. const { stream_id } = data;
  52. const arr = stream_id.split('_');
  53. const id = arr[1];
  54. console.log('id--->' + id);
  55. const res = await this.model.findById(id);
  56. if (res) {
  57. res.stream_id = stream_id;
  58. res.url = data.video_url;
  59. res.file_id = data.file_id;
  60. res.ishis = '1';
  61. await res.save();
  62. }
  63. return res;
  64. }
  65. async updaterequid(id, requestid) {
  66. const res = await this.model.findById(id);
  67. if (res) {
  68. res.requestId = requestid;
  69. await res.save();
  70. }
  71. return res;
  72. }
  73. }
  74. module.exports = RoomService;