room.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. module.exports = RoomService;