lookuser.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 LookuserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'lookuser');
  10. this.model = this.ctx.model.Lookuser;
  11. this.rmodel = this.ctx.model.Room;
  12. }
  13. async create(data) {
  14. const { roomname, userid } = data;
  15. const lookuser = await this.model.findOne({ roomname, userid });
  16. if (!lookuser) {
  17. await this.model.create(data);
  18. }
  19. }
  20. async query({ skip, limit, ...info }) {
  21. const total = await this.rmodel.count(info);
  22. const rooms = await this.rmodel.find(info).skip(Number(skip)).limit(Number(limit));
  23. const data = [];
  24. for (const _room of rooms) {
  25. const room = _.cloneDeep(JSON.parse(JSON.stringify(_room)));
  26. const number = await this.model.count({ roomid: room._id });
  27. room.number = number;
  28. data.push(room);
  29. }
  30. return { data, total };
  31. }
  32. }
  33. module.exports = LookuserService;