import { Match } from '../../entity/platform/match.entity';
import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseServiceV2 } from '../../frame/BaseServiceV2';
import { ProjectService } from './project.service';
import dayjs = require('dayjs');
@Provide()
export class MatchService extends BaseServiceV2 {
  @InjectEntityModel(Match)
  model: Repository<Match>;

  @Inject()
  projectService: ProjectService;
  getQueryColumnsOpera() {
    const obj = {
      tags: this.Opera.Json,
    };
    return obj;
  }
  // 赛事完成后,导入项目库,贴 赛事 标签,支持筛选
  async updateProject({ id }, data) {
    const builder = this.model.createQueryBuilder();
    this.completeBuilderCondition({ id });
    const match = await builder.getOne();
    if (match && match.match_status === '2') {
      const result = await this.projectService.query({ user: match.user, name: match.name });
      if (result && result.total > 0) {
        await this.projectService.update({ id: result.data[0].id }, { user: match.user, name: match.name, industry: match.industry, tags: ['赛事'], time: dayjs().format('YYYY-MM-DD'), source: '赛事', status: '1' });
      } else {
        await this.projectService.create({ user: match.user, name: match.name, industry: match.industry, tags: ['赛事'], time: dayjs().format('YYYY-MM-DD'), source: '赛事', status: '1' });
      }
    }
  }
}
