package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.cultivate.mapper.FtbCultivateChapterTestMapper;
import jnpf.cultivate.mapper.FtbCultivateChapterTestResultMapper;
import jnpf.cultivate.service.FtbCultivateChapterTestService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.identify.FtbIdentityOrgWisdomStatisticDTO;
import jnpf.model.cultivate.po.course.FtbCultivateChapterTest;
import jnpf.model.cultivate.po.course.FtbCultivateChapterTestResult;
import jnpf.model.cultivate.vo.chapter.FtbCultivateChapterTestResultVO;
import jnpf.model.cultivate.vo.chapter.FtbCultivateChapterTestStatisticVO;
import jnpf.model.cultivate.vo.common.InnerPowerUserVO;
import jnpf.model.cultivate.vo.identify.FtbCultivateIdentityOrgWisdomStatisticVO;
import jnpf.model.cultivate.vo.identify.IdentifyApplyListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FtbCultivateChapterTestServiceImpl extends ServiceImpl<FtbCultivateChapterTestMapper, FtbCultivateChapterTest> implements FtbCultivateChapterTestService {

    @Resource
    private FtbCultivateChapterTestResultMapper resultMapper;

    @Autowired
    private UserApiV2Util userApiV2Util;
    /**
     * 返回一个空的分页结果列表
     *
     * @param statisticDTO 统计查询参数DTO
     * @return 空的分页结果列表
     */
    private  PageListVO<FtbCultivateChapterTestStatisticVO> returnEmptyList(CultivatePage statisticDTO){
        PageListVO<FtbCultivateChapterTestStatisticVO> pageInfo = new PageListVO<>();
        PaginationVO pagination = new PaginationVO();
        pagination.setCurrentPage(statisticDTO.getCurrentPage());
        pagination.setPageSize(statisticDTO.getPageSize());
        pagination.setTotal(0);
        pageInfo.setList(new ArrayList<>());
        pageInfo.setPagination(pagination);
        return pageInfo;
    }
    @Override
    public PageListVO<FtbCultivateChapterTestStatisticVO> getChapterTestStatistic(String  keyWords,
                                                                                  CultivatePage page) {
        List<String> innerPowerUserIds = new ArrayList<>();
        InnerPowerUserVO innerPowerUserVO = userApiV2Util.getLoginManagerUserIdsForEncode();
        if (innerPowerUserVO.getCode() == 1) {
            innerPowerUserIds = innerPowerUserVO.getUserIds();
        } else if (innerPowerUserVO.getCode() == 2) {
            return returnEmptyList(page);
        }
        Page<FtbCultivateChapterTestStatisticVO> pageInfo = page.coverCultivatePage();
        List<FtbCultivateChapterTestStatisticVO> statistic = baseMapper.getChapterTestStatistic(keyWords,innerPowerUserIds);
        // 处理题型
        if (CollUtil.isNotEmpty(statistic)){
            statistic.forEach(item->{
                List<FtbCultivateChapterTestResult> chapterTests  = resultMapper.selectChatperList(item.getCourseId(),item.getUserId());
                int numberOfTest = 0;
                long numberOfCorrect = 0;
                long numberOfWrong = 0;
                List<List<FtbCultivateChapterTestResultVO>> list = new ArrayList<>();
                for(FtbCultivateChapterTestResult chapterTest : chapterTests){
                    String chapterTestOptions = chapterTest.getChapterTestOptions();
                    List<FtbCultivateChapterTestResultVO> resultVOS = JSONObject.parseArray(chapterTestOptions, FtbCultivateChapterTestResultVO.class);
                    if(CollUtil.isEmpty(resultVOS)) continue;
                    list.add(resultVOS);
                    // 单章题目数量
                    numberOfTest += resultVOS.size();
                    // 单章正确题数量
                     numberOfCorrect += resultVOS.stream().filter(resultVO -> resultVO.getIsRight() == 1).count();
                    // 单章错误题数量
                     numberOfWrong += resultVOS.stream().filter(resultVO -> resultVO.getIsRight() == 0).count();
                }
                item.setNumberOfTest(numberOfTest);
                // 正确率
                BigDecimal correctRate = BigDecimal.ZERO;
                if (numberOfTest != 0 ) correctRate = BigDecimal.valueOf(numberOfCorrect).divide(BigDecimal.valueOf(numberOfTest), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
                item.setNumberOfCorrect((int) numberOfCorrect);
                item.setCorrectRate(correctRate);
                // 错误率
                BigDecimal errorRate = BigDecimal.ZERO;
                if (numberOfTest != 0 )  errorRate = BigDecimal.valueOf(numberOfWrong).divide(BigDecimal.valueOf(numberOfTest), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
                item.setNumberOfWrong((int) numberOfWrong);
                item.setErrorRate(errorRate);
                item.setInClassTestResults(list);
            });
        }
        return CultivatePage.paginate(statistic,pageInfo);
    }
}
