achievement.service.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { BaseService } from 'free-midway-component';
  5. import { Achievement } from '../../entity/platform/achievement.entity';
  6. import { Collection } from '../../entity/platform/collection.entity';
  7. import { User } from '../../entity/system/user.entity';
  8. import { get } from 'lodash';
  9. type modelType = ReturnModelType<typeof Achievement>;
  10. @Provide()
  11. export class AchievementService extends BaseService<modelType> {
  12. @InjectEntityModel(Achievement)
  13. model: modelType;
  14. @InjectEntityModel(Collection)
  15. cModel: ReturnModelType<typeof Collection>;
  16. @InjectEntityModel(User)
  17. uModel: ReturnModelType<typeof User>;
  18. // 成果列表
  19. async list(query) {
  20. const { skip = 0, limit = 0, is_use, status, ...condition } = query;
  21. const { one, two, thr, four, five, six } = condition;
  22. const info: any = { is_use, status };
  23. if (one) info.technology = { $in: one };
  24. if (two) info.field = { $in: two };
  25. if (thr) info.mature = { $in: thr };
  26. if (four) info.sell = { $in: four };
  27. if (five) {
  28. if (five === '1') info.money = '面议';
  29. if (five === '2') info.money = { $gte: '1', $lte: '10' };
  30. if (five === '3') info.money = { $gte: '10', $lte: '20' };
  31. if (five === '4') info.money = { $gte: '20', $lte: '100' };
  32. if (five === '5') info.money = { $gte: '100', $lte: '500' };
  33. if (five === '6') info.money = { $gte: '500', $lte: '1000' };
  34. if (five === '7') info.money = { $gte: '1000' };
  35. }
  36. if (six) info.area = { $in: six };
  37. const data = await this.model.find(info).skip(skip).limit(limit).lean();
  38. for (const val of data) {
  39. if (get(val, 'user')) {
  40. // 查询成果发布者
  41. const userData = await this.uModel.findById(val.user).lean();
  42. if (userData) Object.assign(val, { userName: userData.nick_name });
  43. }
  44. }
  45. const total = await this.model.count(info);
  46. return { data, total };
  47. }
  48. // 成果详情
  49. async detail(id) {
  50. const user = this.ctx.user;
  51. const data = { userInfo: {}, is_collection: false };
  52. const arr = await this.model.findById(id).lean();
  53. if (arr && get(arr, 'user')) {
  54. // 查询成果发布者
  55. const userData = await this.uModel.findById(arr.user).lean();
  56. if (userData) data.userInfo = { name: userData.nick_name || '', phone: userData.phone || '' };
  57. }
  58. if (arr && get(arr, '_id')) {
  59. if (user && user._id) {
  60. // 查询是否收藏该成果
  61. const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean();
  62. if (collection) data.is_collection = true;
  63. }
  64. }
  65. return { ...arr, ...data };
  66. }
  67. }