import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { EvaluationIndicator } from '../entities/evaluation-indicator.entity';
import { Evaluation } from '../entities/evaluation.entity';

@Injectable()
export class EvaluationService {
  constructor(
    @InjectRepository(Evaluation)
    private evaluationRepository: Repository<Evaluation>,
  ) {}

  create(data) {
    return this.evaluationRepository.insert(data);
  }

  findAll() {
    return this.evaluationRepository
      .createQueryBuilder('e')
      .leftJoinAndMapMany(
        'e.indicators',
        EvaluationIndicator,
        'ei',
        ['ei.evaluation_id=e.id', 'e.del_flag=0', 'ei.del_flag=0'].join(
          ' and ',
        ),
      )
      .where({ delFlag: 0 })
      .select(['e.id', 'e.name', 'e.appModule', 'ei.id', 'ei.name'])
      .orderBy('e.id', 'DESC')
      .addOrderBy('ei.id', 'ASC')
      .getMany();
  }

  update(id: number, data) {
    return this.evaluationRepository.update(id, data);
  }

  remove(id: number) {
    return this.evaluationRepository.update(id, { delFlag: 1 });
  }
}
