|
@@ -3,9 +3,59 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
|
|
|
import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
import { BaseService } from 'free-midway-component';
|
|
|
import { Record } from '../../entity/core/record.entity';
|
|
|
+import { get } from 'lodash';
|
|
|
+import { Video } from '../../entity/core/video.entity';
|
|
|
+import { User } from '../../entity/system/user.entity';
|
|
|
type modelType = ReturnModelType<typeof Record>;
|
|
|
@Provide()
|
|
|
export class RecordService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Record)
|
|
|
model: modelType;
|
|
|
+ @InjectEntityModel(Video)
|
|
|
+ vModel: ReturnModelType<typeof Video>;
|
|
|
+ @InjectEntityModel(User)
|
|
|
+ uModel: ReturnModelType<typeof User>;
|
|
|
+
|
|
|
+ // 特殊查询
|
|
|
+ async special(filter) {
|
|
|
+ const { skip = 0, limit = 0, user_name, video_name, ...info } = filter;
|
|
|
+ if (user_name) {
|
|
|
+ const userData = await this.uModel.findOne({ nick_name: user_name }).lean();
|
|
|
+ if (userData) info.user_id = get(userData, '_id');
|
|
|
+ }
|
|
|
+ if (video_name) {
|
|
|
+ const videoData = await this.vModel.findOne({ title: { $regex: video_name } }).lean();
|
|
|
+ if (videoData) info.video_id = get(videoData, '_id');
|
|
|
+ }
|
|
|
+ const data = await this.model.find(info).skip(skip).limit(limit).lean();
|
|
|
+ for (const val of data) {
|
|
|
+ if (get(val, 'user_id')) {
|
|
|
+ // 查询用户信息
|
|
|
+ const userData = await this.uModel.findById(val.user_id).lean();
|
|
|
+ if (userData) Object.assign(val, { user_name: userData.nick_name });
|
|
|
+ }
|
|
|
+ if (get(val, 'video_id')) {
|
|
|
+ // 查询视频信息
|
|
|
+ const videoData = await this.vModel.findById(val.video_id).lean();
|
|
|
+ if (videoData) Object.assign(val, { video_name: videoData.title });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const total = await this.model.count(info);
|
|
|
+ return { data, total };
|
|
|
+ }
|
|
|
+ // 特殊创建
|
|
|
+ async createBefore(data) {
|
|
|
+ const { user_id, video_id, ...info } = data;
|
|
|
+ let result;
|
|
|
+ const record = await this.model.findOne({ user_id, video_id }).lean();
|
|
|
+ if (record) {
|
|
|
+ if (get(record, 'time') < get(info, 'time')) {
|
|
|
+ await this.model.updateOne({ _id: get(record, '_id') }, info);
|
|
|
+ result = await this.model.findById(get(record, '_id'));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result = await this.model.create(data);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|