trainlive.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const assert = require('assert');
  6. const _ = require('lodash');
  7. // 培训问诊表
  8. class TrainliveService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'trainlive');
  11. this.model = this.ctx.model.Trainlive;
  12. }
  13. /**
  14. * 重写创建,room_id自动生成
  15. * @param {Object} body 数据
  16. */
  17. async create(body) {
  18. const last = await this.model.findOne().sort({ room_id: -1 });
  19. let room_id = '3001',
  20. password = '3001';
  21. if (last) {
  22. room_id = parseInt(_.get(last, 'room_id', '3000')) + 1;
  23. password = room_id;
  24. }
  25. body.room_id = room_id;
  26. body.password = password;
  27. return await this.model.create(body);
  28. }
  29. /**
  30. * 为培训问诊添加参加用户
  31. * @param {Object} {id} 培训问诊的数据id
  32. * @param {Array} {users} 培训问诊的参加用户
  33. */
  34. async addUser({ id }, { users }) {
  35. const object = await this.model.findById(id);
  36. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  37. object.user_data.push(...users);
  38. await object.save();
  39. }
  40. /**
  41. * 更改培训问诊下指定用户的数据
  42. * @param {Object} {id} 培训问诊的数据id
  43. * @param {Array} {users} 用户的数据
  44. */
  45. async updateUser({ id }, { users }) {
  46. const object = await this.model.findById(id);
  47. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  48. for (const user of users) {
  49. const { _id, ...info } = user;
  50. if (_id) {
  51. // 存在_id,修改
  52. const oldData = object.user_data.id(ObjectId(_id));
  53. if (!oldData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下指定的参会人员信息!');
  54. const keys = Object.keys(info);
  55. for (const key of keys) {
  56. oldData[key] = info[key];
  57. }
  58. }
  59. }
  60. await object.save();
  61. }
  62. /**
  63. *移除培训问诊下指定用户
  64. * @param {Object} {id} 培训问诊的数据id
  65. * @param {Object} {users} 用户的数据id集合
  66. */
  67. async deleteUser({ id }, { users }) {
  68. const object = await this.model.findById(id);
  69. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  70. object.user_data = object.user_data.filter(f => !users.find(uf => ObjectId(uf).equals(f._id)));
  71. await object.save();
  72. }
  73. /**
  74. * 参会人员登陆
  75. * @param {Object} {id} 培训问诊的数据id
  76. * @param {Object} user 参会人员的信息,手机号和密码
  77. */
  78. async login({ id }, { user_phone, user_password }) {
  79. assert(user_phone, '缺少登陆的参会人员 手机号');
  80. assert(user_password, '缺少登陆的参会人员 密码');
  81. const object = await this.model.findById(id);
  82. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  83. const user = object.user_data.find(f => f.user_phone === user_phone);
  84. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下该手机号的用户信息!');
  85. const is_login = await this.app.redis.get(`trainlive/${user._id}`);
  86. if (is_login) throw new BusinessError(ErrorCode.BUSINESS, '用户已登录');
  87. if (user.user_password !== user_password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '用户密码错误!');
  88. await this.app.redis.set(`trainlive/${user._id}`, user);
  89. }
  90. /**
  91. * 参会人员注销
  92. * @param {String} {id} 参会人员id
  93. */
  94. async logout({ id }) {
  95. await this.app.redis.del(`trainlive/${id}`);
  96. }
  97. }
  98. module.exports = TrainliveService;