package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.*;
import com.ruoyi.hezhi.domain.dto.MemberSubjectAllDTO;
import com.ruoyi.hezhi.mapper.TbExamCourseMapper;
import com.ruoyi.hezhi.mapper.TbMemberSubjectAllMapper;
import com.ruoyi.hezhi.mapper.TbMemberSubjectCollectMapper;
import com.ruoyi.hezhi.service.ITbExamSubjectService;
import com.ruoyi.hezhi.service.ITbMemberSubjectAllService;
import com.ruoyi.hezhi.service.ITbMemberSubjectWrongService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的题目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-06
 */
@Service
public class TbMemberSubjectAllServiceImpl implements ITbMemberSubjectAllService
{
    @Autowired
    private TbMemberSubjectAllMapper tbMemberSubjectAllMapper;
    @Autowired
    private ITbExamSubjectService tbExamSubjectService;
    @Autowired
    private ITbMemberSubjectWrongService tbMemberSubjectWrongService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private TbMemberSubjectCollectMapper tbMemberSubjectCollectMapper;
    @Autowired
    private TbExamCourseMapper tbExamCourseMapper;

    /**
     * 查询我的题目
     *
     * @param memberSubjectAllId 我的题目主键
     * @return 我的题目
     */
    @Override
    public TbMemberSubjectAll selectTbMemberSubjectAllByMemberSubjectAllId(Long memberSubjectAllId)
    {
        return tbMemberSubjectAllMapper.selectTbMemberSubjectAllByMemberSubjectAllId(memberSubjectAllId);
    }

    /**
     * 查询我的题目列表
     *
     * @param tbMemberSubjectAll 我的题目
     * @return 我的题目
     */
    @Override
    public List<TbMemberSubjectAll> selectTbMemberSubjectAllList(TbMemberSubjectAll tbMemberSubjectAll)
    {
        return tbMemberSubjectAllMapper.selectTbMemberSubjectAllList(tbMemberSubjectAll);
    }

    /**
     * 新增我的题目
     *
     * @param tbMemberSubjectAll 我的题目
     * @return 结果
     */
    @Override
    public int insertTbMemberSubjectAll(TbMemberSubjectAll tbMemberSubjectAll)
    {
        tbMemberSubjectAll.setCreateTime(DateUtils.getNowDate());
        return tbMemberSubjectAllMapper.insertTbMemberSubjectAll(tbMemberSubjectAll);
    }

    /**
     * 修改我的题目
     *
     * @param tbMemberSubjectAll 我的题目
     * @return 结果
     */
    @Override
    public int updateTbMemberSubjectAll(TbMemberSubjectAll tbMemberSubjectAll)
    {
        tbMemberSubjectAll.setUpdateTime(DateUtils.getNowDate());
        return tbMemberSubjectAllMapper.updateTbMemberSubjectAll(tbMemberSubjectAll);
    }

    /**
     * 批量删除我的题目
     *
     * @param memberSubjectAllIds 需要删除的我的题目主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSubjectAllByMemberSubjectAllIds(Long[] memberSubjectAllIds)
    {
        return tbMemberSubjectAllMapper.deleteTbMemberSubjectAllByMemberSubjectAllIds(memberSubjectAllIds);
    }

    /**
     * 删除我的题目信息
     *
     * @param memberSubjectAllId 我的题目主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSubjectAllByMemberSubjectAllId(Long memberSubjectAllId)
    {
        return tbMemberSubjectAllMapper.deleteTbMemberSubjectAllByMemberSubjectAllId(memberSubjectAllId);
    }

    /**
     * 题目数量
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public int memberSubjectAllCount(Map<String, Object> params) {
        return tbMemberSubjectAllMapper.memberSubjectAllCount(params);
    }

    /**
     * 查询我的题目
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public TbMemberSubjectAll queryMemberSubjectAllByParam(Map<String, Object> params) {
        return tbMemberSubjectAllMapper.queryMemberSubjectAllByParam(params);
    }

    /**
     * 查询我的题目列表
     *
     * @param memberSubjectAll 我的题目
     * @return 我的题目集合
     */
    @Override
    public List<TbMemberSubjectAll> selectMemberSubjectAllList(TbMemberSubjectAll memberSubjectAll) {
        return tbMemberSubjectAllMapper.selectMemberSubjectAllList(memberSubjectAll);
    }

    /**
     * 练习答题
     *
     * @param memberSubjectAllDTO
     * @return ok
     */
    @Override
    public AjaxResult subjectPracticeAnswer(MemberSubjectAllDTO memberSubjectAllDTO) {
        Long memberUserId = memberSubjectAllDTO.getMemberUserId();
        Long examSubjectId = memberSubjectAllDTO.getExamSubjectId();
        Long courseId = memberSubjectAllDTO.getCourseId();
        Integer practiceType = memberSubjectAllDTO.getPracticeType();
        Integer subjectType = memberSubjectAllDTO.getSubjectType();
        String myAnswer = memberSubjectAllDTO.getMyAnswer();
        Long location = memberSubjectAllDTO.getLocation();

        // 查询当前题目
        TbExamSubject tbExamSubject = tbExamSubjectService.selectTbExamSubjectByExamSubjectId(memberSubjectAllDTO.getExamSubjectId());
        if (tbExamSubject == null) {
            return AjaxResult.error("题目信息异常");
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("memberUserId", memberUserId);
        params.put("examSubjectId", examSubjectId);

        // 正确答案
        String answer = tbExamSubject.getAnswer();
        if (tbExamSubject.getSubjectType() == 3) {
            if ("正确".equals(answer)) {
                answer = "A";
            }
            if ("错误".equals(answer)) {
                answer = "B";
            }
        }

        TbMemberSubjectAll memberSubjectAll = new TbMemberSubjectAll();
        memberSubjectAll.setExamSubjectId(examSubjectId);
        memberSubjectAll.setMemberUserId(memberUserId);
        memberSubjectAll.setLocation(location);
        memberSubjectAll.setPracticeType(practiceType);
        memberSubjectAll.setSubjectType(subjectType);//todo 插入做题记录
        memberSubjectAll.setMyAnswer(myAnswer);
        memberSubjectAll.setAnswer(answer);

        if (tbExamSubject.getSubjectType() != 4) {
            if (myAnswer.equals(answer)) {
                memberSubjectAll.setStatus(1);
            } else {
                memberSubjectAll.setStatus(2);
                //添加错题[收藏题目]
//                String finalAnswer = answer;
                threadPoolTaskExecutor.execute(()->{
                    //添加错题
                    TbMemberSubjectCollect memberSubjectCollect = tbMemberSubjectCollectMapper.selectTbMemberSubjectCollectByMemberUserIdSubjectId(
                            memberUserId,
                            examSubjectId,
                            courseId);
                    if (ObjectUtils.isEmpty(memberSubjectCollect)){
                        TbMemberSubjectCollect subjectCollect = new TbMemberSubjectCollect();
                        subjectCollect.setMemberUserId(memberUserId);
                        subjectCollect.setExamSubjectId(examSubjectId);
                        subjectCollect.setCourseId(courseId);
                        subjectCollect.setCreateTime(DateUtils.getNowDate());
                        tbMemberSubjectCollectMapper.insertTbMemberSubjectCollect(subjectCollect);
                    }
                });
            }
        } else {
            memberSubjectAll.setStatus(3);
        }


        params.put("practiceType", practiceType);
        TbMemberSubjectAll subjectAllByParam = tbMemberSubjectAllMapper.queryMemberSubjectAllByParam(params);
        if (subjectAllByParam == null) {
            tbMemberSubjectAllMapper.insertTbMemberSubjectAll(memberSubjectAll);
        } else {
            memberSubjectAll.setMemberSubjectAllId(subjectAllByParam.getMemberSubjectAllId());
            tbMemberSubjectAllMapper.updateTbMemberSubjectAll(memberSubjectAll);
        }
        return AjaxResult.success().put("data", memberSubjectAll);
    }

    /**
     * 练习答题清空
     *
     * @param memberSubjectAllDTO
     * @return ok
     */
    @Override
    public AjaxResult subjectPracticeAnswerClear(MemberSubjectAllDTO memberSubjectAllDTO) {
        Long memberUserId = memberSubjectAllDTO.getMemberUserId();
        Long courseId = memberSubjectAllDTO.getCourseId();
        Integer practiceType = memberSubjectAllDTO.getPracticeType();
        //参数校验
        if (ObjectUtils.isEmpty(memberUserId)) {
            throw new ServiceException("缺少参数memberUserId");
        }
        if (ObjectUtils.isEmpty(courseId)) {
            throw new ServiceException("缺少参数courseId");
        }
        if (ObjectUtils.isEmpty(practiceType)) {
            throw new ServiceException("缺少参数practiceType");
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("memberUserId", memberUserId);
//        params.put("practiceType", practiceType); //全清，不判断联系类型，不然统计会乱
        TbExamCourse examCourseDetail = tbExamCourseMapper.getSubjectWarehouseClassIdByCourseId(courseId);
        if (ObjectUtils.isEmpty(examCourseDetail)) {
            throw new ServiceException("此课程未添加题库");
        }
        params.put("subjectWarehouseClassId", examCourseDetail.getSubjectWarehouseClassId());
        return AjaxResult.success(tbMemberSubjectAllMapper.deleteTbMemberSubjectAllByMsp(params));
    }
}
