package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbMemberSubjectCollect;
import com.ruoyi.hezhi.domain.dto.MemberSubjectCollectDTO;
import com.ruoyi.hezhi.mapper.TbExamSubjectAnswerMapper;
import com.ruoyi.hezhi.mapper.TbMemberSubjectCollectMapper;
import com.ruoyi.hezhi.mapper.TbMemberSubjectWrongMapper;
import com.ruoyi.hezhi.service.ITbMemberSubjectCollectService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的收藏题目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-07
 */
@Service
public class TbMemberSubjectCollectServiceImpl implements ITbMemberSubjectCollectService
{
    @Autowired
    private TbMemberSubjectCollectMapper tbMemberSubjectCollectMapper;
    @Autowired
    private TbExamSubjectAnswerMapper examSubjectAnswerMapper;
    @Autowired
    private TbMemberSubjectWrongMapper memberSubjectWrongMapper;

    /**
     * 查询我的收藏题目
     *
     * @param memberSubjectCollectId 我的收藏题目主键
     * @return 我的收藏题目
     */
    @Override
    public TbMemberSubjectCollect selectTbMemberSubjectCollectByMemberSubjectCollectId(Long memberSubjectCollectId)
    {
        return tbMemberSubjectCollectMapper.selectTbMemberSubjectCollectByMemberSubjectCollectId(memberSubjectCollectId);
    }

    /**
     * 查询我的收藏题目列表
     *
     * @param tbMemberSubjectCollect 我的收藏题目
     * @return 我的收藏题目
     */
    @Override
    public List<TbMemberSubjectCollect> selectTbMemberSubjectCollectList(TbMemberSubjectCollect tbMemberSubjectCollect)
    {
        return tbMemberSubjectCollectMapper.selectTbMemberSubjectCollectList(tbMemberSubjectCollect);
    }

    /**
     * 新增我的收藏题目
     *
     * @param tbMemberSubjectCollect 我的收藏题目
     * @return 结果
     */
    @Override
    public int insertTbMemberSubjectCollect(TbMemberSubjectCollect tbMemberSubjectCollect)
    {
        tbMemberSubjectCollect.setCreateTime(DateUtils.getNowDate());
        return tbMemberSubjectCollectMapper.insertTbMemberSubjectCollect(tbMemberSubjectCollect);
    }

    /**
     * 修改我的收藏题目
     *
     * @param tbMemberSubjectCollect 我的收藏题目
     * @return 结果
     */
    @Override
    public int updateTbMemberSubjectCollect(TbMemberSubjectCollect tbMemberSubjectCollect)
    {
        tbMemberSubjectCollect.setUpdateTime(DateUtils.getNowDate());
        return tbMemberSubjectCollectMapper.updateTbMemberSubjectCollect(tbMemberSubjectCollect);
    }

    /**
     * 批量删除我的收藏题目
     *
     * @param memberSubjectCollectIds 需要删除的我的收藏题目主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSubjectCollectByMemberSubjectCollectIds(Long[] memberSubjectCollectIds)
    {
        return tbMemberSubjectCollectMapper.deleteTbMemberSubjectCollectByMemberSubjectCollectIds(memberSubjectCollectIds);
    }

    /**
     * 删除我的收藏题目信息
     *
     * @param memberSubjectCollectId 我的收藏题目主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSubjectCollectByMemberSubjectCollectId(Long memberSubjectCollectId)
    {
        return tbMemberSubjectCollectMapper.deleteTbMemberSubjectCollectByMemberSubjectCollectId(memberSubjectCollectId);
    }

    /**
     * 收藏或取消收藏题目
     *
     * @param memberSubjectCollectDTO
     * @return ok
     */
    @Override
    public AjaxResult subjectCollectOrCancelCollect(MemberSubjectCollectDTO memberSubjectCollectDTO) {
        TbMemberSubjectCollect memberSubjectCollect = tbMemberSubjectCollectMapper.selectTbMemberSubjectCollectByMemberUserIdSubjectId(
                memberSubjectCollectDTO.getMemberUserId(),
                memberSubjectCollectDTO.getExamSubjectId(),
                memberSubjectCollectDTO.getCourseId());
        if (ObjectUtils.isNotEmpty(memberSubjectCollect)) {
            tbMemberSubjectCollectMapper.deleteTbMemberSubjectCollectByMemberSubjectCollectId(memberSubjectCollect.getMemberSubjectCollectId());
        } else {
            TbMemberSubjectCollect subjectCollect = new TbMemberSubjectCollect();
            subjectCollect.setMemberUserId(memberSubjectCollectDTO.getMemberUserId());
            subjectCollect.setExamSubjectId(memberSubjectCollectDTO.getExamSubjectId());
            subjectCollect.setCourseId(memberSubjectCollectDTO.getCourseId());
            subjectCollect.setCreateTime(DateUtils.getNowDate());
            tbMemberSubjectCollectMapper.insertTbMemberSubjectCollect(subjectCollect);
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 题目收藏列表
     *
     * @param params 参数
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> getMemberSubjectCollectList(Map<String, Object> params) {
        List<Map<String, Object>> memberSubjectCollectList = tbMemberSubjectCollectMapper.getMemberSubjectCollectList(params);
        if (!memberSubjectCollectList.isEmpty()) {
            for (Map<String, Object> examSubjectInfo : memberSubjectCollectList) {
                // 题目类型（1-单选，2-多选，3-判断，4-论述）
                int subjectType = Integer.parseInt(examSubjectInfo.get("subjectType").toString());
                String examSubjectNo = examSubjectInfo.get("examSubjectNo").toString();
                if (subjectType != 4) {
                    // 选项列表
                    List<Map<String, Object>> subjectSelectList = examSubjectAnswerMapper.selectSelectList(null, examSubjectNo);
                    examSubjectInfo.put("subjectSelectList", subjectSelectList);
                    // 我的答案
//                    memberSubjectWrongMapper.selectTbMemberSubjectWrongByMemberSubjectWrongId();
                    // 处理answer字段
                    String answer = examSubjectInfo.get("answer").toString();
                    String[] answerOptions = answer.split(",");
                    List<Map<String, Object>> correctOptions = new ArrayList<>();
                    for (String option : answerOptions) {
                        for (Map<String, Object> selectOption : subjectSelectList) {
                            if (option.equals(selectOption.get("subjectOption"))) {
                                correctOptions.add(selectOption);
                                break;
                            }
                        }
                    }
                    examSubjectInfo.put("correctOptions", correctOptions);
                } else {
                    examSubjectInfo.put("subjectSelectList", null);
                    examSubjectInfo.put("correctOptions", null);
                }
            }
        }
        return memberSubjectCollectList;
    }
}
