package com.ruoyi.ie.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ie.domain.Papers;
import com.ruoyi.ie.domain.Questions;
import com.ruoyi.ie.mapper.PapersMapper;
import com.ruoyi.ie.mapper.QuestionsMapper;
import com.ruoyi.ie.service.IPapersService;
import com.ruoyi.ie.service.IQuestionsService;
import com.ruoyi.system.mapper.PersonResourcesMapper;
import com.ruoyi.system.service.IOssService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 试卷信息Service业务层处理
 *
 * @author mingxue
 * @date 2021-06-20
 */
@Service
public class PapersServiceImpl implements IPapersService {
    @Autowired
    private PapersMapper papersMapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private PersonResourcesMapper personResourcesMapper;
    @Autowired
    private IQuestionsService questionsService;
    @Autowired
    private IOssService ossService;

    /**
     * 查询试卷信息
     *
     * @param id 试卷信息ID
     * @return 试卷信息
     */
    @Override
    public Papers selectPapersById(Long id) {
        return papersMapper.selectPapersById(id);
    }

    /**
     * 查询试卷信息列表
     *
     * @param papers 试卷信息
     * @return 试卷信息
     */
    @Override
    public List<Papers> selectPapersList(Papers papers) {
        return papersMapper.selectPapersList(papers);
    }

    @Override
    public List<Papers> selectMyPapersList(Map cond) {
        return papersMapper.selectMyPapersList(cond);
    }

    /**
     * 新增试卷信息
     *
     * @param papers 试卷信息
     * @return 结果
     */
    @Override
    public int insertPapers(Papers papers) {
        return papersMapper.insertPapers(papers);
    }

    /**
     * 修改试卷信息
     *
     * @param papers 试卷信息
     * @return 结果
     */
    @Override
    public int updatePapers(Papers papers) {
        return papersMapper.updatePapers(papers);
    }

    /**
     * 批量删除试卷信息
     *
     * @param ids 需要删除的试卷信息ID
     * @return 结果
     */
    @Override
    public int deletePapersByIds(Long[] ids) {
        return papersMapper.deletePapersByIds(ids);
    }

    /**
     * 删除试卷信息信息
     *
     * @param id 试卷信息ID
     * @return 结果
     */
    @Override
    public int deletePapersById(Long id) {
        return papersMapper.deletePapersById(id);
    }


    @Override
    public List<Questions> listQuestionsByPaperId(Long paperId) {
        return questionsMapper.listQuestionsByPaperId(paperId);
    }

    @Override
    public List<Papers> selectPapersListFromFavorites(Papers papers) {
        return papersMapper.selectPapersListFromFavorites(papers);
    }

    @Override
    public void fillCollectInfo(String customerCode, List<Papers> papers) {
        if (CollectionUtils.isEmpty(papers)) {
            return;
        }
        List<Long> ids = papers.stream().map(Papers::getId).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerCode", customerCode);
        map.put("ids", ids);
        List<Papers> collectInfos = papersMapper.selectCollectInfo(map);
        Set<Long> collected = collectInfos.stream().map(Papers::getId).collect(Collectors.toSet());
        papers.forEach(e -> {
            e.setCollect(collected.contains(e.getId()));
        });
    }

    public Map getObjectivePaperStatistic(Integer subjectId) {
        Map data = new HashMap();
        Long userId = SecurityUtils.getUserId();
        Long total = questionsMapper.selectTotalBySubjectId(subjectId);
        data.put("total", total);
        Long completed = questionsMapper.selectTotalOfCompleted(subjectId, userId);
        data.put("completed", completed);
        data.put("accuracy", completed > 0L ? Math.round(questionsMapper.selectTotalOfLatestRight(subjectId, userId) * 100 / completed) : null);

        List<Map> recentList = new ArrayList<>();
        List<Questions> questionsList = questionsMapper.selectLatestRates(subjectId, userId);
        Collections.sort(questionsList, new Comparator<Questions>() {
            @Override
            public int compare(Questions o1, Questions o2) {
                int iRet = o1.getCreateTime().compareTo(o2.getCreateTime());
                return iRet == 0 ? o1.getId().compareTo(o2.getId()) : iRet;
            }
        });
        questionsList.stream().forEach(t -> {
            Map recent = new HashMap();
            recent.put("text", String.valueOf(recentList.size() + 1));
            recent.put("value", t.getScore());
            recentList.add(recent);
        });
        data.put("recentDetails", recentList);
        return data;
    }
}
