expert.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const jwt = require('jsonwebtoken');
  6. // 专家
  7. class ExpertService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'expert');
  10. this.personal = this.ctx.model.Personal;
  11. this.model = this.ctx.model.Expert;
  12. }
  13. async query({ skip = 0, limit, ...condition } = {}) {
  14. condition = this.ctx.service.util.util.dealQuery(condition);
  15. const { code, status, company, name, phone } = condition;
  16. const query = {};
  17. if (code) query.code = code;
  18. if (status) query['expert.status'] = status;
  19. if (name)query.name = name;
  20. if (phone) query.phone = phone;
  21. if (company) {
  22. if (company === '中科系') query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
  23. else if (company === '其他')query['expert.company'] = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] };
  24. else query['expert.company'] = company;
  25. }
  26. // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
  27. if (!limit) limit = await this.model.count();
  28. let data = await this.personal.aggregate([
  29. { $match: { is_expert: true } },
  30. { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } },
  31. {
  32. $lookup: {
  33. from: 'expert',
  34. localField: '_id',
  35. foreignField: 'user_id',
  36. as: 'expert',
  37. },
  38. },
  39. { $unwind: '$expert' },
  40. { $match: query },
  41. { $skip: parseInt(skip) },
  42. { $limit: parseInt(limit) },
  43. ]);
  44. data = data.map(i => {
  45. const { expert, ...info } = _.omit(i, [ '_id' ]);
  46. const object = { ...expert, ...info };
  47. return object;
  48. });
  49. const totalSearch = await this.personal.aggregate([
  50. { $match: { is_expert: true } },
  51. { $project: { name: 1, phone: 1, email: 1, code: 1, is_expert: 1, expert: 1 } },
  52. {
  53. $lookup: {
  54. from: 'expert',
  55. localField: '_id',
  56. foreignField: 'user_id',
  57. as: 'expert',
  58. },
  59. },
  60. { $match: query },
  61. {
  62. $group: {
  63. _id: '$is_expert',
  64. total: { $sum: 1 },
  65. },
  66. },
  67. { $unwind: '$total' },
  68. ]);
  69. const total = _.get(_.head(totalSearch), 'total', 0);
  70. return { data, total };
  71. }
  72. async fetch({ user_id }) {
  73. const personal = await this.personal.findById(user_id, 'name phone email code');
  74. if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');
  75. let expert = await this.model.findOne({ user_id });
  76. if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息');
  77. expert = JSON.parse(JSON.stringify(expert));
  78. const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]);
  79. expert = { ...pinfo, ...expert };
  80. return expert;
  81. }
  82. /**
  83. * 创建用户
  84. * @param {Object} params 用户信息
  85. */
  86. async create({ password, ...data }) {
  87. data.password = { secret: password };
  88. const { phone } = data;
  89. // 检查手机号
  90. const num = await this.model.count({ phone, isdel: '0' });
  91. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有专家用户使用该手机号');
  92. return await this.model.create(data);
  93. }
  94. async delete({ id }) {
  95. const object = await this.model.findById(id);
  96. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  97. object.isdel = '1';
  98. await object.save();
  99. }
  100. }
  101. module.exports = ExpertService;