|
@@ -3,9 +3,45 @@ 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, ...condition } = query;
|
|
|
+ const data = await this.model.find(condition).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(condition);
|
|
|
+ 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 || '' };
|
|
|
+
|
|
|
+ const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean();
|
|
|
+ if (collection) data.is_collection = true;
|
|
|
+ }
|
|
|
+ return { ...arr, ...data };
|
|
|
+ }
|
|
|
}
|