package com.ruoyi.web.controller.manager;

import com.ruoyi.system.domain.userexam.ElUserExam;
import com.ruoyi.system.service.ElUserExamService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * packageName com.ruoyi.web.controller.manager
 *
 * @author wangxy
 * @version JDK 8
 * @className UserExamManager
 * @date 2024/7/1
 * @description 考试记录业务
 */
@Component
public class UserExamManager {

    @Resource
    private ElUserExamService elUserExamService;

    /**
     *
     *  考试记录保存
     * @param userId
     * @param examId
     * @param score
     * @param passed
     * @return void
     */

    public void joinResult(Long userId, String examId, Integer score, boolean passed) {
        //查询条件
         ElUserExam record = elUserExamService.lambdaQuery()
                 .eq(ElUserExam::getUserId, userId)
                .eq(ElUserExam::getExamId, examId).one();
        if(Objects.isNull(record)){
            record = new ElUserExam();
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            record.setUserId(userId);
            record.setExamId(examId);
            record.setMaxScore(score);
            record.setPassed(passed);
            elUserExamService.save(record);
            return;
        }
        // 修复低分数不加入统计问题
        record.setTryCount(record.getTryCount()+1);
        record.setUpdateTime(new Date());
        if(record.getMaxScore() < score){
            record.setMaxScore(score);
            record.setPassed(passed);
        }
        elUserExamService.updateById(record);
    }
}
