package com.srs.comprehensive.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.comprehensive.domain.QuestionnaireAnswer;
import com.srs.comprehensive.domain.CphTeacher;
import com.srs.comprehensive.domain.QuestionnaireResult;
import com.srs.comprehensive.mapper.QuestionnaireAnswerMapper;
import com.srs.comprehensive.mapper.CphTeacherMapper;
import com.srs.comprehensive.service.IQuestionnaireAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author srs
 * @date 2024-05-20
 */
@Service
public class QuestionnaireAnswerServiceImpl extends ServiceImpl<QuestionnaireAnswerMapper, QuestionnaireAnswer> implements IQuestionnaireAnswerService {
    @Autowired
    private QuestionnaireAnswerMapper answerMapper;

    @Autowired
    private CphTeacherMapper teacherMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public QuestionnaireAnswer selectAnswerById(Long id) {

        return answerMapper.selectAnswerById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tAnswer 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<QuestionnaireAnswer> selectAnswerList(QuestionnaireAnswer tAnswer) {
        return answerMapper.selectAnswerList(tAnswer);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tAnswer 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAnswer(QuestionnaireAnswer tAnswer) {
            return answerMapper.insertAnswer(tAnswer);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tAnswer 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAnswer(QuestionnaireAnswer tAnswer) {

        //先查询是不是填过了。如果填了就修改否则新增

        return answerMapper.updateAnswer(tAnswer);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAnswerByIds(Long[] ids) {
        return answerMapper.deleteAnswerByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAnswerById(Long id) {
        return answerMapper.deleteAnswerById(id);
    }

    @Override
    public QuestionnaireAnswer saveAnswer(QuestionnaireAnswer answer) {

        QueryWrapper<QuestionnaireAnswer> answerQueryWrapper = new QueryWrapper<QuestionnaireAnswer>();
        answerQueryWrapper.eq("user_id",answer.getUserId())
                .eq("questionnaire_id",answer.getQuestionnaireId())
                .eq("target_id",answer.getTargetId());
        QuestionnaireAnswer queryResult = answerMapper.selectOne(answerQueryWrapper);
        if(queryResult == null){
            CphTeacher teacher = teacherMapper.selectCphTeacherByTeacherId(answer.getTargetId());
            answer.setTargetName(teacher.getName());
            //如果还没填写就先新增
            answer.setStatus(0L);//状态0，未填写
            insertAnswer(answer);
            return  answer;
        }else{

            return queryResult;
        }
    }

    @Override
    public List<QuestionnaireResult> ratioScore(QuestionnaireResult result) {
        List<QuestionnaireResult> list = answerMapper.ratioScore(result);
        for (QuestionnaireResult questionnaireResult : list) {
            // 获取已评学生数量和带班人数
            int studentFillNum = questionnaireResult.getStudentFillNum();
            int studentNum = questionnaireResult.getStudentNum();

            // 检查带班人数是否为零，避免除零异常
            if (studentNum == 0) {
                questionnaireResult.setXsRatio("0%");
            } else {
                // 使用 BigDecimal 进行精确计算
                BigDecimal fillNum = BigDecimal.valueOf(studentFillNum);
                BigDecimal totalNum = BigDecimal.valueOf(studentNum);
                BigDecimal ratio = fillNum.divide(totalNum, 2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

                // 将结果格式化为保留两位小数的字符串并添加百分号
                String formattedRatio = ratio.setScale(2, RoundingMode.DOWN).toString();
                questionnaireResult.setXsRatio(formattedRatio + "%");
            }
        }
        return list;
    }


}
