trainlive.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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} {room_id,password} 房间号,密码
  32. */
  33. async login({ room_id, password }) {
  34. const object = await this.model.findOne({ room_id });
  35. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  36. if (object.password !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误!');
  37. return object;
  38. }
  39. /**
  40. * 为培训问诊添加参加用户
  41. * @param {Object} {id} 培训问诊的数据id
  42. * @param {Array} {users} 培训问诊的参加用户
  43. */
  44. async addUser({ id }, { users }) {
  45. const object = await this.model.findById(id);
  46. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  47. object.user_data.push(...users);
  48. await object.save();
  49. }
  50. /**
  51. * 更改培训问诊下指定用户的数据
  52. * @param {Object} {id} 培训问诊的数据id
  53. * @param {Array} {users} 用户的数据
  54. */
  55. async updateUser({ id }, { users }) {
  56. const object = await this.model.findById(id);
  57. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  58. for (const user of users) {
  59. const { _id, ...info } = user;
  60. if (_id) {
  61. // 存在_id,修改
  62. const oldData = object.user_data.id(ObjectId(_id));
  63. if (!oldData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下指定的参会人员信息!');
  64. const keys = Object.keys(info);
  65. for (const key of keys) {
  66. oldData[key] = info[key];
  67. }
  68. }
  69. }
  70. await object.save();
  71. }
  72. /**
  73. *移除培训问诊下指定用户
  74. * @param {Object} {id} 培训问诊的数据id
  75. * @param {Object} {users} 用户的数据id集合
  76. */
  77. async deleteUser({ id }, { users }) {
  78. const object = await this.model.findById(id);
  79. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  80. object.user_data = object.user_data.filter(f => !users.find(uf => ObjectId(uf).equals(f._id)));
  81. await object.save();
  82. }
  83. /**
  84. * 参会人员登陆
  85. * @param {Object} {id} 培训问诊的数据id
  86. * @param {Object} user 参会人员的信息,手机号和密码
  87. */
  88. async userLogin({ id }, { user_phone, user_password }) {
  89. assert(user_phone, '缺少登陆的参会人员 手机号');
  90. assert(user_password, '缺少登陆的参会人员 密码');
  91. const object = await this.model.findById(id);
  92. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
  93. const user = object.user_data.find(f => f.user_phone === user_phone);
  94. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下该手机号的用户信息!');
  95. const is_login = await this.app.redis.get(`trainlive/${user._id}`);
  96. if (is_login) throw new BusinessError(ErrorCode.BUSINESS, '用户已登录');
  97. if (user.user_password !== user_password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '用户密码错误!');
  98. await this.app.redis.set(`trainlive/${user._id}`, user);
  99. }
  100. /**
  101. * 参会人员注销
  102. * @param {String} {id} 参会人员id
  103. */
  104. async userLogout({ id }) {
  105. await this.app.redis.del(`trainlive/${id}`);
  106. }
  107. }
  108. module.exports = TrainliveService;