zs 1 년 전
부모
커밋
14ac4ee242
2개의 변경된 파일95개의 추가작업 그리고 0개의 파일을 삭제
  1. 22 0
      src/controller/tool.controller.ts
  2. 73 0
      src/service/tool.service.ts

+ 22 - 0
src/controller/tool.controller.ts

@@ -0,0 +1,22 @@
+import { Controller, Get, Inject, Query } from '@midwayjs/decorator';
+import { ApiResponse, ApiTags } from '@midwayjs/swagger';
+import { ToolService } from '../service/tool.service';
+@ApiTags(['工具'])
+@Controller('/tool')
+export class ToolController {
+  @Inject()
+  service: ToolService;
+
+  @Get('/CollectionTotal')
+  @ApiResponse({})
+  async getCollectionTotal() {
+    const result = await this.service.getCollectionTotal();
+    return result;
+  }
+  @Get('/MyCollection')
+  @ApiResponse({})
+  async MyCollection(@Query() filter) {
+    const result = await this.service.getCollection(filter);
+    return result;
+  }
+}

+ 73 - 0
src/service/tool.service.ts

@@ -0,0 +1,73 @@
+import { Inject, Provide } from '@midwayjs/core';
+import { Context } from '@midwayjs/koa';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { Collection } from '../entity/platform/collection.entity';
+import { Achievement } from '../entity/platform/achievement.entity';
+import { Demand } from '../entity/platform/demand.entity';
+import { Match } from '../entity/platform/match.entity';
+import { Expert } from '../entity/users/expert.entity';
+import { Company } from '../entity/users/company.entity';
+import { Project } from '../entity/platform/project.entity';
+@Provide()
+export class ToolService {
+  @Inject()
+  ctx: Context;
+  @InjectEntityModel(Collection)
+  cModel: ReturnModelType<typeof Collection>;
+
+  @InjectEntityModel(Achievement)
+  aModel: ReturnModelType<typeof Achievement>;
+
+  @InjectEntityModel(Demand)
+  dModel: ReturnModelType<typeof Demand>;
+
+  @InjectEntityModel(Project)
+  pModel: ReturnModelType<typeof Project>;
+
+  @InjectEntityModel(Match)
+  mModel: ReturnModelType<typeof Match>;
+
+  @InjectEntityModel(Expert)
+  eModel: ReturnModelType<typeof Expert>;
+
+  @InjectEntityModel(Company)
+  companyModel: ReturnModelType<typeof Company>;
+
+  // 收藏总数
+  async getCollectionTotal() {
+    const user = this.ctx.user;
+    const actList = [
+      { label: '成果', value: 'achievement' },
+      { label: '需求', value: 'demand' },
+      { label: '项目', value: 'project' },
+      { label: '赛事', value: 'match' },
+      { label: '企业', value: 'company' },
+      { label: '专家', value: 'expert' },
+    ];
+    for (const val of actList) {
+      const total = await this.cModel.count({ user, type: val.value }).lean();
+      val.label = val.label + `(${total})`;
+    }
+    return actList;
+  }
+  // 我的收藏
+  async getCollection(filter) {
+    const user = this.ctx.user;
+    const { skip = 0, limit = 0, type } = filter;
+    let model;
+    const data = [];
+    if (type === 'achievement') model = this.aModel;
+    if (type === 'demand') model = this.dModel;
+    if (type === 'project') model = this.pModel;
+    if (type === 'match') model = this.mModel;
+    if (type === 'company') model = this.companyModel;
+    if (type === 'expert') model = this.eModel;
+    const list = await this.cModel.find({ user, type }).skip(skip).limit(limit).lean();
+    for (const val of list) {
+      data.push(await model.findById(val.source).lean());
+    }
+    const total = await this.cModel.count({ user, type }).lean();
+    return { data, total };
+  }
+}