import { Score } from '../../entity/platform/score.entity';
import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, Equal } from 'typeorm';
import { BaseServiceV2 } from '../../frame/BaseServiceV2';
import { get } from 'lodash';
import { SignService } from './sign.service';
import { MatchPathService } from './matchPath.service';
import { ErrorCode, ServiceError } from '../../error/service.error';
@Provide()
export class ScoreService extends BaseServiceV2 {
  @InjectEntityModel(Score)
  model: Repository<Score>;

  @Inject()
  matchPathService: MatchPathService;

  @Inject()
  signService: SignService;

  // 分数检查
  async createExamine(data) {
    const { match, matchPath, sign } = data;
    const result = await this.model.findOne({ where: { matchPath: Equal(matchPath), sign: Equal(sign), match: Equal(match) } });
    if (result) throw new ServiceError(ErrorCode.SERVICE_COPY);
  }

  /**
   * 填充赛事信息
   * @param {object} data 数据
   */
  async fillName(data) {
    const { matchPath, sign } = data;
    // 流程名称
    const matchPathInfo = await this.matchPathService.fetch({ id: matchPath });
    if (matchPathInfo) data = { ...data, matchPath_name: get(matchPathInfo, 'name') };
    // 选手
    const signInfo = await this.signService.fetch({ id: sign });
    if (signInfo) data = { ...data, sign_name: get(signInfo, 'name') };

    return data;
  }
}
