import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/system/entities/user.entity';
import { Repository } from 'typeorm';
import { EvaluationIndicatorDto } from '../dto/evaluation-indicator.dto';
import { StudentEvaluationDto } from '../dto/student-evaluation.dto';
import { EvaluationIndicator } from '../entities/evaluation-indicator.entity';
import { StudentEvaluation } from '../entities/student-evaluation.entity';

@Injectable()
export class EvaluationIndicatorService {
  constructor(
    @InjectRepository(EvaluationIndicator)
    private eiRepository: Repository<EvaluationIndicator>,
    @InjectRepository(StudentEvaluation)
    private seRepository: Repository<StudentEvaluation>,
  ) {}
  create(data) {
    return this.eiRepository.insert(data);
  }

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

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

  findAllStudentEvaluation(orgId: number) {
    return this.seRepository
      .createQueryBuilder('se')
      .innerJoin(
        User,
        'u',
        ['se.studentId=u.id', 'se.del_flag=0', 'u.del_flag=0'].join(' and '),
      )
      .where('u.org_id=:orgId', { orgId })
      .select(['se.id', 'se.studentId', 'se.indicatorId', 'se.score'])
      .getMany();
  }

  async createStudentEvaluationScore(data: StudentEvaluationDto) {
    await this.seRepository.insert(data);
    return true;
  }

  async updateStudentEvaluationScore(id: number, data: StudentEvaluationDto) {
    await this.seRepository.update(id, data);
    return true;
  }
}
