1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { Provide } from '@midwayjs/decorator';
- import { InjectEntityModel } from '@midwayjs/typegoose';
- import { ReturnModelType } from '@typegoose/typegoose';
- import { BaseService } from 'free-midway-component';
- import { Demand } from '../../entity/platform/demand.entity';
- import { Collection } from '../../entity/platform/collection.entity';
- import { User } from '../../entity/system/user.entity';
- import { get } from 'lodash';
- type modelType = ReturnModelType<typeof Demand>;
- @Provide()
- export class DemandService extends BaseService<modelType> {
- @InjectEntityModel(Demand)
- model: modelType;
- @InjectEntityModel(Collection)
- cModel: ReturnModelType<typeof Collection>;
- @InjectEntityModel(User)
- uModel: ReturnModelType<typeof User>;
- // 需求列表
- 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 };
- // 如果 one 不是一个数组,你可能需要手动处理
- if (one) {
- if (!Array.isArray(one)) info.field = one;
- else info.field = { $all: one };
- }
- if (two) {
- if (!Array.isArray(two)) info.method = two;
- else info.method = { $all: two };
- }
- if (thr) {
- if (!Array.isArray(thr)) info.area = thr;
- else info.area = { $all: thr };
- }
- if (four) {
- if (!Array.isArray(four)) info.demand_status = four;
- else info.demand_status = { $all: 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')) {
- // 查询是否收藏该需求
- const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean();
- if (collection) data.is_collection = true;
- }
- return { ...arr, ...data };
- }
- }
|