import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, Equal } from 'typeorm';
import { Cirelation } from '../../entity/users/cirelation.entity';
import { BaseServiceV2 } from '../../frame/BaseServiceV2';
import { CompanyService } from './company.service';
import { IncubatorService } from './incubator.service';
import { ErrorCode, ServiceError } from '../../error/service.error';
import { get } from 'lodash';
@Provide()
export class CirelationService extends BaseServiceV2 {
  getQueryColumnsOpera(): object {
    return {};
  }
  @InjectEntityModel(Cirelation)
  model: Repository<Cirelation>;

  @Inject()
  companyService: CompanyService;

  @Inject()
  incubatorService: IncubatorService;

  // 添加企业检查
  async createExamine(data) {
    const { company, incubator } = data;
    const result = await this.model.findOne({ where: { company: Equal(company), incubator: Equal(incubator) } });
    if (result) throw new ServiceError(ErrorCode.SERVICE_COPY);
  }

  /**
   * 填充赛事信息
   * @param {object} data 数据
   */
  async fillName(data) {
    const { company, incubator } = data;
    // 企业名称
    const companyInfo = await this.companyService.fetch({ id: company });
    if (companyInfo) data = { ...data, company_name: get(companyInfo, 'name') };
    // 孵化器名称
    const incubatorInfo = await this.incubatorService.fetch({ id: incubator });
    if (incubatorInfo) data = { ...data, incubator_name: get(incubatorInfo, 'name') };

    return data;
  }
}
