project.service.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 { Project } from '../../entity/platform/project.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 Project>;
  10. @Provide()
  11. export class ProjectService extends BaseService<modelType> {
  12. @InjectEntityModel(Project)
  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 } = condition;
  22. const info: any = { is_use, status };
  23. if (one) info.field = { $in: one };
  24. if (two) info.cooperate = { $in: two };
  25. if (thr) info.area = { $in: thr };
  26. if (four) info.maturity = { $in: four };
  27. const data = await this.model.find(info).skip(skip).limit(limit).lean();
  28. for (const val of data) {
  29. if (get(val, 'user')) {
  30. // 查询项目发布者
  31. const userData = await this.uModel.findById(val.user).lean();
  32. if (userData) Object.assign(val, { userName: userData.nick_name });
  33. }
  34. }
  35. const total = await this.model.count(info);
  36. return { data, total };
  37. }
  38. //项目详情
  39. async detail(id) {
  40. const user = this.ctx.user;
  41. const data = { userInfo: {}, is_collection: false };
  42. const arr = await this.model.findById(id).lean();
  43. if (arr && get(arr, 'user')) {
  44. // 查询项目发布者
  45. const userData = await this.uModel.findById(arr.user).lean();
  46. if (userData) data.userInfo = { name: userData.nick_name || '', phone: userData.phone || '' };
  47. }
  48. if (arr && get(arr, '_id')) {
  49. if (user && user._id) {
  50. // 查询是否收藏该项目
  51. const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean();
  52. if (collection) data.is_collection = true;
  53. }
  54. }
  55. return { ...arr, ...data };
  56. }
  57. }