import { Match } from '../../entity/platform/match.entity';
import { get } from 'lodash';
import { Collection } from '../../entity/platform/collection.entity';
import { Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, Repository } from 'typeorm';
import { BaseService } from '../../frame/BaseService';
@Provide()
export class MatchService extends BaseService<Match> {
  @InjectEntityModel(Match)
  model: Repository<Match>;
  @InjectEntityModel(Collection)
  cModel: Repository<Collection>;

  // 比赛详情
  async detail(id) {
    const user = this.ctx.user;
    const data = { userInfo: {}, is_collection: false };
    // const arr = await this.model.findById(id).lean();
    const arr = await this.model.findOne({ where: { id: Equal(id) } });
    if (arr && get(arr, 'id') && user) {
      // 查询是否收藏该比赛
      // const collection = await this.cModel.findOne({ user: user._id, source: arr._id }).lean();
      const collection = await this.cModel.findOne({ where: { user: Equal(user.id), source: Equal(arr.id) } });
      if (collection) data.is_collection = true;
    }
    return { ...arr, ...data };
  }
}
