import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateQuestionDifficultyLevelDto } from 'src/assessment-evaluation/dto/dtos-question-manager/question-difficulty-level/create-question-difficulty-level.dto';
import { UpdateQuestionDifficultyLevelDto } from 'src/assessment-evaluation/dto/dtos-question-manager/question-difficulty-level/update-question-difficulty-level.dto';
import { QuestionDifficultyLevel } from 'src/assessment-evaluation/entities/entities-question-manager/question-difficulty-level.entity';

@Injectable()
export class QuestionDifficultyLevelService {
  constructor(
    @InjectRepository(QuestionDifficultyLevel)
    private questionDifficultyLevelRepository: Repository<QuestionDifficultyLevel>,
  ) {}
  /* 
      创建难易程度
    */
  async create(
    createQuestionDifficultyLevelDto: CreateQuestionDifficultyLevelDto,
  ) {
    if (
      await this.questionDifficultyLevelRepository.findOne({
        where: { delFlag: 0, name: createQuestionDifficultyLevelDto.name },
      })
    ) {
      throw '难易程度已存在';
    }
    return await this.questionDifficultyLevelRepository.save(
      createQuestionDifficultyLevelDto,
    );
  }
  /* 
    获取所有试题难度
  */
  async findAll() {
    return this.questionDifficultyLevelRepository.find({
      select: ['id', 'name'],
      where: { delFlag: 0 },
    });
  }
  /* 
    更新试题难度
  */
  async update(
    id: number,
    updateQuestionDifficultyLevelDto: UpdateQuestionDifficultyLevelDto,
  ) {
    return this.questionDifficultyLevelRepository.update(
      id,
      updateQuestionDifficultyLevelDto,
    );
  }
  /* 
    删除试题难度
  */
  async remove(id: number) {
    await this.questionDifficultyLevelRepository.update(id, { delFlag: 1 });
    return true;
  }
}
