'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const jwt = require('jsonwebtoken'); // 专家 class ExpertService extends CrudService { constructor(ctx) { super(ctx, 'expert'); this.personal = this.ctx.model.Personal; this.model = this.ctx.model.Expert; } async query({ skip = 0, limit, ...condition } = {}) { condition = this.ctx.service.util.util.dealQuery(condition); const { code, status, company, name, phone } = condition; const query = {}; if (code) query.code = code; if (status) query['expert.status'] = status; if (name)query.name = name; if (phone) query.phone = phone; if (company) { if (company === '中科系') query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] }; else if (company === '其他')query['expert.company'] = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] }; else query['expert.company'] = company; } // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit if (!limit) limit = await this.model.count(); let data = await this.personal.aggregate([ { $match: { is_expert: true } }, { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } }, { $lookup: { from: 'expert', localField: '_id', foreignField: 'user_id', as: 'expert', }, }, { $unwind: '$expert' }, { $match: query }, { $skip: parseInt(skip) }, { $limit: parseInt(limit) }, ]); data = data.map(i => { const { expert, ...info } = _.omit(i, [ '_id' ]); const object = { ...expert, ...info }; return object; }); const totalSearch = await this.personal.aggregate([ { $match: { is_expert: true } }, { $project: { name: 1, phone: 1, email: 1, code: 1, is_expert: 1, expert: 1 } }, { $lookup: { from: 'expert', localField: '_id', foreignField: 'user_id', as: 'expert', }, }, { $match: query }, { $group: { _id: '$is_expert', total: { $sum: 1 }, }, }, { $unwind: '$total' }, ]); const total = _.get(_.head(totalSearch), 'total', 0); return { data, total }; } async fetch({ user_id }) { const personal = await this.personal.findById(user_id, 'name phone email code'); if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息'); let expert = await this.model.findOne({ user_id }); if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息'); expert = JSON.parse(JSON.stringify(expert)); const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]); expert = { ...pinfo, ...expert }; return expert; } /** * 创建用户 * @param {Object} params 用户信息 */ async create({ password, ...data }) { data.password = { secret: password }; const { phone } = data; // 检查手机号 const num = await this.model.count({ phone, isdel: '0' }); if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有专家用户使用该手机号'); return await this.model.create(data); } async delete({ id }) { const object = await this.model.findById(id); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); object.isdel = '1'; await object.save(); } } module.exports = ExpertService;