package com.ruoyi.model.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.AppletAnswer;
import com.ruoyi.model.mapper.AppletAnswerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.AppletQuestionMapper;
import com.ruoyi.model.domain.AppletQuestion;
import com.ruoyi.model.service.IAppletQuestionService;

/**
 * 考核题库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-28
 */
@Service
public class AppletQuestionServiceImpl extends ServiceImpl<AppletQuestionMapper, AppletQuestion> implements IAppletQuestionService
{
    @Autowired
    private AppletQuestionMapper appletQuestionMapper;
    @Autowired
    private AppletAnswerMapper appletAnswerMapper;

    /**
     * 查询考核题库
     * 
     * @param id 考核题库主键
     * @return 考核题库
     */
    @Override
    public AppletQuestion selectAppletQuestionById(Long id)
    {
        AppletQuestion appletQuestion = appletQuestionMapper.selectAppletQuestionById(id);
        AppletAnswer appletAnswer = new AppletAnswer();
        appletAnswer.setQuestionId(id);
        appletQuestion.setAnswerList(appletAnswerMapper.selectAppletAnswerList(appletAnswer));
        return appletQuestion;
    }

    /**
     * 查询考核题库列表
     * 
     * @param appletQuestion 考核题库
     * @return 考核题库
     */
    @Override
    public List<AppletQuestion> selectAppletQuestionList(AppletQuestion appletQuestion)
    {
        return appletQuestionMapper.selectAppletQuestionList(appletQuestion);
    }

    /**
     * 新增考核题库
     * 
     * @param appletQuestion 考核题库
     * @return 结果
     */
    @Override
    public int insertAppletQuestion(AppletQuestion appletQuestion)
    {
        appletQuestion.setCreateTime(DateUtils.getNowDate());
        int i = appletQuestionMapper.insertAppletQuestion(appletQuestion);
        for (AppletAnswer appletAnswer : appletQuestion.getAnswerList()) {
            appletAnswer.setQuestionId(appletQuestion.getId());
            appletAnswerMapper.insertAppletAnswer(appletAnswer);
        }
        return i;
    }

    /**
     * 修改考核题库
     * 
     * @param appletQuestion 考核题库
     * @return 结果
     */
    @Override
    public int updateAppletQuestion(AppletQuestion appletQuestion)
    {
        //查询答案
        AppletAnswer appletAnswer = new AppletAnswer();
        appletAnswer.setQuestionId(appletQuestion.getId());
        List<AppletAnswer> list = appletAnswerMapper.selectAppletAnswerList(appletAnswer);
        for (AppletAnswer appletAnswer1 : list) {
            //删除答案
            appletAnswerMapper.deleteAppletAnswerById(appletAnswer1.getId());
        }
        appletQuestion.setUpdateTime(DateUtils.getNowDate());
        int i = appletQuestionMapper.updateAppletQuestion(appletQuestion);
        //添加答案
        for (AppletAnswer appletAnswer1 : appletQuestion.getAnswerList()) {
            appletAnswer1.setQuestionId(appletQuestion.getId());
            appletAnswerMapper.insertAppletAnswer(appletAnswer1);
        }
        return i;
    }

    /**
     * 批量删除考核题库
     * 
     * @param ids 需要删除的考核题库主键
     * @return 结果
     */
    @Override
    public int deleteAppletQuestionByIds(Long[] ids)
    {
        return appletQuestionMapper.deleteAppletQuestionByIds(ids);
    }

    /**
     * 删除考核题库信息
     * 
     * @param id 考核题库主键
     * @return 结果
     */
    @Override
    public int deleteAppletQuestionById(Long id)
    {
        return appletQuestionMapper.deleteAppletQuestionById(id);
    }
}
