package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbExamSubjectAnswer;
import com.ruoyi.hezhi.mapper.TbExamSubjectAnswerMapper;
import com.ruoyi.hezhi.service.ITbExamSubjectAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 考试题目答案选项Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-04
 */
@Service
public class TbExamSubjectAnswerServiceImpl implements ITbExamSubjectAnswerService
{
    @Autowired
    private TbExamSubjectAnswerMapper tbExamSubjectAnswerMapper;

    /**
     * 查询考试题目答案选项
     *
     * @param examSubjectAnswerId 考试题目答案选项主键
     * @return 考试题目答案选项
     */
    @Override
    public TbExamSubjectAnswer selectTbExamSubjectAnswerByExamSubjectAnswerId(Long examSubjectAnswerId)
    {
        return tbExamSubjectAnswerMapper.selectTbExamSubjectAnswerByExamSubjectAnswerId(examSubjectAnswerId);
    }

    /**
     * 查询考试题目答案选项列表
     *
     * @param tbExamSubjectAnswer 考试题目答案选项
     * @return 考试题目答案选项
     */
    @Override
    public List<TbExamSubjectAnswer> selectTbExamSubjectAnswerList(TbExamSubjectAnswer tbExamSubjectAnswer)
    {
        return tbExamSubjectAnswerMapper.selectTbExamSubjectAnswerList(tbExamSubjectAnswer);
    }

    /**
     * 新增考试题目答案选项
     *
     * @param tbExamSubjectAnswer 考试题目答案选项
     * @return 结果
     */
    @Override
    public int insertTbExamSubjectAnswer(TbExamSubjectAnswer tbExamSubjectAnswer)
    {
        tbExamSubjectAnswer.setCreateTime(DateUtils.getNowDate());
        return tbExamSubjectAnswerMapper.insertTbExamSubjectAnswer(tbExamSubjectAnswer);
    }

    /**
     * 修改考试题目答案选项
     *
     * @param tbExamSubjectAnswer 考试题目答案选项
     * @return 结果
     */
    @Override
    public int updateTbExamSubjectAnswer(TbExamSubjectAnswer tbExamSubjectAnswer)
    {
        tbExamSubjectAnswer.setUpdateTime(DateUtils.getNowDate());
        return tbExamSubjectAnswerMapper.updateTbExamSubjectAnswer(tbExamSubjectAnswer);
    }

    /**
     * 批量删除考试题目答案选项
     *
     * @param examSubjectAnswerIds 需要删除的考试题目答案选项主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectAnswerByExamSubjectAnswerIds(Long[] examSubjectAnswerIds)
    {
        return tbExamSubjectAnswerMapper.deleteTbExamSubjectAnswerByExamSubjectAnswerIds(examSubjectAnswerIds);
    }

    /**
     * 删除考试题目答案选项信息
     *
     * @param examSubjectAnswerId 考试题目答案选项主键
     * @return 结果
     */
    @Override
    public int deleteTbExamSubjectAnswerByExamSubjectAnswerId(Long examSubjectAnswerId)
    {
        return tbExamSubjectAnswerMapper.deleteTbExamSubjectAnswerByExamSubjectAnswerId(examSubjectAnswerId);
    }

    /**
     * 批量新增考试题目答案选项
     *
     * @param list 考试题目答案选项
     * @return 结果
     */
    @Override
    public int batchInsertTbExamSubjectAnswer(List<TbExamSubjectAnswer> list) {
        return tbExamSubjectAnswerMapper.batchInsertTbExamSubjectAnswer(list);
    }

    /**
     * 查询答案ID集合和答案集合
     *
     * @param examSubjectId 考试题目主键ID
     * @param examSubjectNo 考试题目编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectAnswerIdsAndAnswer(Long examSubjectId, String examSubjectNo) {
        return tbExamSubjectAnswerMapper.selectAnswerIdsAndAnswer(examSubjectId, examSubjectNo);
    }

    /**
     * 查询考试题目答案选项列表（考试题目编号）
     *
     * @param examSubjectNo 考试题目编号
     * @return 考试题目答案选项集合
     */
    @Override
    public List<TbExamSubjectAnswer> selectTbExamSubjectAnswerListBy(String examSubjectNo) {
        return tbExamSubjectAnswerMapper.selectTbExamSubjectAnswerListBy(examSubjectNo);
    }

    /**
     * 批量删除考试题目答案选项信息（考试题目编号）
     *
     * @param examSubjectNo 考试题目编号
     * @return 结果
     */
    @Override
    public int batchDeleteTbExamSubjectAnswerByExamSubjectNo(String examSubjectNo) {
        return tbExamSubjectAnswerMapper.batchDeleteTbExamSubjectAnswerByExamSubjectNo(examSubjectNo);
    }

    /**
     * 论述题分数
     *
     * @param examSubjectNo 考试题目编号
     * @param myAnswer 答案
     * @return 结果
     */
    @Override
    public BigDecimal getDiscussScore(String examSubjectNo, String myAnswer) {
        BigDecimal score = new BigDecimal(0);
        List<TbExamSubjectAnswer> examSubjectAnswerList = tbExamSubjectAnswerMapper.selectTbExamSubjectAnswerListBy(examSubjectNo);
        if (!"".equals(myAnswer) && !examSubjectAnswerList.isEmpty()) {
            for (TbExamSubjectAnswer subjectAnswer : examSubjectAnswerList) {
                String subjectName = subjectAnswer.getSubjectName();
                if (myAnswer.toLowerCase().contains(subjectName.toLowerCase())) {
                    score = score.add(subjectAnswer.getScore());
                }
            }
        }
        return score;
    }
}
