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