import { Provide } from '@midwayjs/decorator'; import { InjectEntityModel } from '@midwayjs/typegoose'; import { ReturnModelType } from '@typegoose/typegoose'; import { BaseService } from 'free-midway-component'; import { Project } from '../../entity/platform/project.entity'; import { Collection } from '../../entity/platform/collection.entity'; import { User } from '../../entity/system/user.entity'; import { get } from 'lodash'; type modelType = ReturnModelType; @Provide() export class ProjectService extends BaseService { @InjectEntityModel(Project) model: modelType; @InjectEntityModel(Collection) cModel: ReturnModelType; @InjectEntityModel(User) uModel: ReturnModelType; // 项目列表 async list(query) { const { skip = 0, limit = 0, is_use, status, ...condition } = query; const { one, two, thr, four } = condition; const info: any = { is_use, status }; if (one) info.field = { $in: one }; if (two) info.cooperate = { $in: two }; if (thr) info.area = { $in: thr }; if (four) info.maturity = { $in: four }; const data = await this.model.find(info).skip(skip).limit(limit).lean(); for (const val of data) { if (get(val, 'user')) { // 查询项目发布者 const userData = await this.uModel.findById(val.user).lean(); if (userData) Object.assign(val, { userName: userData.nick_name }); } } const total = await this.model.count(info); return { data, total }; } //项目详情 async detail(id) { const user = this.ctx.user; const data = { userInfo: {}, is_collection: false }; const arr = await this.model.findById(id).lean(); if (arr && get(arr, 'user')) { // 查询项目发布者 const userData = await this.uModel.findById(arr.user).lean(); if (userData) data.userInfo = { name: userData.nick_name || '', phone: userData.phone || '' }; } if (arr && get(arr, '_id')) { if (user && user._id) { // 查询是否收藏该项目 const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean(); if (collection) data.is_collection = true; } } return { ...arr, ...data }; } }