package com.ruoyi.model.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.*;
import com.ruoyi.model.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.service.IAppletUsersTeacherService;

/**
 * 伴宠师认证Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class AppletUsersTeacherServiceImpl extends ServiceImpl<AppletUsersTeacherMapper, AppletUsersTeacher> implements IAppletUsersTeacherService {
    @Autowired
    private AppletUsersTeacherMapper appletUsersTeacherMapper;
    @Autowired
    private AppletQuestionMapper appletQuestionMapper;
    @Autowired
    private AppletAnswerMapper appletAnswerMapper;

    @Autowired
    private AppletAnswerBaseMapper appletAnswerBaseMapper;
    @Autowired
    private AppletAnswerTrainMapper appletAnswerTrainMapper;
    /**
     * 查询伴宠师认证
     * 
     * @param id 伴宠师认证主键
     * @return 伴宠师认证
     */
    @Override
    public AppletUsersTeacher selectAppletUsersTeacherById(Long id)
    {
        AppletUsersTeacher appletUsersTeacher = appletUsersTeacherMapper.selectAppletUsersTeacherById(id);
        buildData(appletUsersTeacher);
        return appletUsersTeacher;
    }

    @Override
    public void buildData(AppletUsersTeacher appletUsersTeacher){
        if(appletUsersTeacher != null){
            List<AppletQuestion> appletQuestions = appletQuestionMapper.selectAppletQuestionList(null);
            //循环设置答案列表
            for (AppletQuestion appletQuestion : appletQuestions){
                //设置答案列表
                AppletAnswer appletAnswer = new AppletAnswer();
                appletAnswer.setQuestionId(appletQuestion.getId());
                List<AppletAnswer> appletAnswers = appletAnswerMapper.selectAppletAnswerList(appletAnswer);
                appletQuestion.setAnswerList(appletAnswers);

                if(appletQuestion.getTypeAnswer() == 0){
                    AppletAnswerBase appletAnswerBase = new AppletAnswerBase();
                    appletAnswerBase.setQuestionId(appletQuestion.getId());
                    appletAnswerBase.setUserId(appletUsersTeacher.getUserId());
                    List<AppletAnswerBase> appletAnswerBases = appletAnswerBaseMapper.selectAppletAnswerBaseList(appletAnswerBase);
                    appletQuestion.setUserAnswerBaseList(appletAnswerBases);
                }else{
                    AppletAnswerTrain appletAnswerTrain = new AppletAnswerTrain();
                    appletAnswerTrain.setQuestionId(appletQuestion.getId());
                    appletAnswerTrain.setUserId(appletUsersTeacher.getUserId());
                    List<AppletAnswerTrain> appletAnswerTrains = appletAnswerTrainMapper.selectAppletAnswerTrainList(appletAnswerTrain);
                    appletQuestion.setUserAppletAnswerTrain(appletAnswerTrains.size() > 0 ? appletAnswerTrains.get(0) : null);
                }
            }


            appletUsersTeacher.setAnswerList(appletQuestions);
        }
    }

    @Override
    public AppletUsersTeacher selectAppletUsersTeacherByUserId(Long userId)
    {
        return appletUsersTeacherMapper.selectAppletUsersTeacherByUserId(userId);
    }

    /**
     * 查询伴宠师认证列表
     * 
     * @param appletUsersTeacher 伴宠师认证
     * @return 伴宠师认证
     */
    @Override
    public List<AppletUsersTeacher> selectAppletUsersTeacherList(AppletUsersTeacher appletUsersTeacher)
    {
        return appletUsersTeacherMapper.selectAppletUsersTeacherList(appletUsersTeacher);
    }

    /**
     * 新增伴宠师认证
     * 
     * @param appletUsersTeacher 伴宠师认证
     * @return 结果
     */
    @Override
    public int insertAppletUsersTeacher(AppletUsersTeacher appletUsersTeacher)
    {
        appletUsersTeacher.setCreateTime(DateUtils.getNowDate());

        if(appletUsersTeacher.getExamNumber() == null){
            appletUsersTeacher.setExamNumber(0);
        }

        int i = 0;

        try {
            i = appletUsersTeacherMapper.insertAppletUsersTeacher(appletUsersTeacher);
        }catch (Exception e){
            i = appletUsersTeacherMapper.insert(appletUsersTeacher);
        }

        return i;
    }

    /**
     * 修改伴宠师认证
     * 
     * @param appletUsersTeacher 伴宠师认证
     * @return 结果
     */
    @Override
    public int updateAppletUsersTeacher(AppletUsersTeacher appletUsersTeacher)
    {
        appletUsersTeacher.setUpdateTime(DateUtils.getNowDate());
        return appletUsersTeacherMapper.updateAppletUsersTeacher(appletUsersTeacher);
    }

    /**
     * 批量删除伴宠师认证
     * 
     * @param ids 需要删除的伴宠师认证主键
     * @return 结果
     */
    @Override
    public int deleteAppletUsersTeacherByIds(Long[] ids)
    {
        List<AppletUsersTeacher> list = lambdaQuery()
                .in(AppletUsersTeacher::getId, ids).list();

        for (AppletUsersTeacher teacher : list) {
            deleteAnswerByUserId(teacher.getUserId());
        }

        return appletUsersTeacherMapper.deleteAppletUsersTeacherByIds(ids);
    }

    /**
     * 删除伴宠师认证信息
     * 
     * @param id 伴宠师认证主键
     * @return 结果
     */
    @Override
    public int deleteAppletUsersTeacherById(Long id)
    {
        AppletUsersTeacher byId = getById(id);
        if (byId != null){
            deleteAnswerByUserId(byId.getUserId());
        }
        return appletUsersTeacherMapper.deleteAppletUsersTeacherById(id);
    }

    public void deleteAnswerByUserId(Long userId){

        LambdaQueryWrapper<AppletAnswerBase> qw = Wrappers.<AppletAnswerBase>lambdaQuery()
                .eq(AppletAnswerBase::getUserId, userId);
        appletAnswerBaseMapper.delete(qw);


        LambdaQueryWrapper<AppletAnswerTrain> qw2 = Wrappers.<AppletAnswerTrain>lambdaQuery()
                .eq(AppletAnswerTrain::getUserId, userId);
        appletAnswerTrainMapper.delete(qw2);
    }
}
