123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const assert = require('assert');
- const _ = require('lodash');
- // 培训问诊表
- class TrainliveService extends CrudService {
- constructor(ctx) {
- super(ctx, 'trainlive');
- this.model = this.ctx.model.Trainlive;
- }
- /**
- * 重写创建,room_id自动生成
- * @param {Object} body 数据
- */
- async create(body) {
- const last = await this.model.findOne().sort({ room_id: -1 });
- let room_id = '3001',
- password = '3001';
- if (last) {
- room_id = parseInt(_.get(last, 'room_id', '3000')) + 1;
- password = room_id;
- }
- body.room_id = room_id;
- body.password = password;
- return await this.model.create(body);
- }
- /**
- * 为培训问诊添加参加用户
- * @param {Object} {id} 培训问诊的数据id
- * @param {Array} {users} 培训问诊的参加用户
- */
- async addUser({ id }, { users }) {
- const object = await this.model.findById(id);
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
- object.user_data.push(...users);
- await object.save();
- }
- /**
- * 更改培训问诊下指定用户的数据
- * @param {Object} {id} 培训问诊的数据id
- * @param {Array} {users} 用户的数据
- */
- async updateUser({ id }, { users }) {
- const object = await this.model.findById(id);
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
- for (const user of users) {
- const { _id, ...info } = user;
- if (_id) {
- // 存在_id,修改
- const oldData = object.user_data.id(ObjectId(_id));
- if (!oldData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下指定的参会人员信息!');
- const keys = Object.keys(info);
- for (const key of keys) {
- oldData[key] = info[key];
- }
- }
- }
- await object.save();
- }
- /**
- *移除培训问诊下指定用户
- * @param {Object} {id} 培训问诊的数据id
- * @param {Object} {users} 用户的数据id集合
- */
- async deleteUser({ id }, { users }) {
- const object = await this.model.findById(id);
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
- object.user_data = object.user_data.filter(f => !users.find(uf => ObjectId(uf).equals(f._id)));
- await object.save();
- }
- /**
- * 参会人员登陆
- * @param {Object} {id} 培训问诊的数据id
- * @param {Object} user 参会人员的信息,手机号和密码
- */
- async login({ id }, { user_phone, user_password }) {
- assert(user_phone, '缺少登陆的参会人员 手机号');
- assert(user_password, '缺少登陆的参会人员 密码');
- const object = await this.model.findById(id);
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定的培训问诊信息!');
- const user = object.user_data.find(f => f.user_phone === user_phone);
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到培训问诊下该手机号的用户信息!');
- const is_login = await this.app.redis.get(`trainlive/${user._id}`);
- if (is_login) throw new BusinessError(ErrorCode.BUSINESS, '用户已登录');
- if (user.user_password !== user_password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '用户密码错误!');
- await this.app.redis.set(`trainlive/${user._id}`, user);
- }
- /**
- * 参会人员注销
- * @param {String} {id} 参会人员id
- */
- async logout({ id }) {
- await this.app.redis.del(`trainlive/${id}`);
- }
- }
- module.exports = TrainliveService;
|