package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallExamSectionMapper;
import com.ruoyi.mall.domain.MallExamSection;
import com.ruoyi.mall.service.IMallExamSectionService;

import javax.annotation.Resource;

/**
 * 联考一分一段Service业务层处理
 *
 * @author LCL
 * @date 2023-10-23
 */
@Service
public class MallExamSectionServiceImpl implements IMallExamSectionService
{
    @Resource
    private MallExamSectionMapper mallExamSectionMapper;

    /**
     * 查询联考一分一段
     *
     * @param sectionId 联考一分一段主键
     * @return 联考一分一段
     */
    @Override
    public MallExamSection selectMallExamSectionBySectionId(Long sectionId)
    {
        return mallExamSectionMapper.selectMallExamSectionBySectionId(sectionId);
    }

    /**
     * 查询联考一分一段列表
     *
     * @param mallExamSection 联考一分一段
     * @return 联考一分一段
     */
    @Override
    public List<MallExamSection> selectMallExamSectionList(MallExamSection mallExamSection)
    {
        return mallExamSectionMapper.selectMallExamSectionList(mallExamSection);
    }

    /**
     * 新增联考一分一段
     *
     * @param mallExamSection 联考一分一段
     * @return 结果
     */
    @Override
    public int insertMallExamSection(MallExamSection mallExamSection)
    {
        mallExamSection.setCreateAt(DateUtils.getTime());
        return mallExamSectionMapper.insertMallExamSection(mallExamSection);
    }

    /**
     * 修改联考一分一段
     *
     * @param mallExamSection 联考一分一段
     * @return 结果
     */
    @Override
    public int updateMallExamSection(MallExamSection mallExamSection)
    {
        mallExamSection.setUpdateAt(DateUtils.getTime());
        return mallExamSectionMapper.updateMallExamSection(mallExamSection);
    }

    /**
     * 批量删除联考一分一段
     *
     * @param sectionIds 需要删除的联考一分一段主键
     * @return 结果
     */
    @Override
    public int deleteMallExamSectionBySectionIds(Long[] sectionIds)
    {
        return mallExamSectionMapper.deleteMallExamSectionBySectionIds(sectionIds);
    }

    /**
     * 删除联考一分一段信息
     *
     * @param sectionId 联考一分一段主键
     * @return 结果
     */
    @Override
    public int deleteMallExamSectionBySectionId(Long sectionId)
    {
        return mallExamSectionMapper.deleteMallExamSectionBySectionId(sectionId);
    }

    /**
     * 成绩分析-联考成绩分析-分数排名
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public Map<String, Object> selectUserScoreRanking(Long userId) {
        Map<String, Object> userMap = mallExamSectionMapper.selectUserScoreRanking(userId);
        Integer score = MapUtil.getInt(userMap, "majorScore");
        MallExamSection examSection = mallExamSectionMapper.selectMallExamSectionClosestBy("2023", score);
        Long sameNum = 0L;// 同位分人数
        Long ranking = 0L;// 本省排名
        BigDecimal proportion = new BigDecimal("0");// 占比
        if (examSection != null) {
            sameNum = examSection.getNum();
            ranking = Long.valueOf(examSection.getIntervalTwo());
        }

        userMap.put("sameNum", sameNum);
        userMap.put("ranking", ranking);

        if (ranking != 0L) {
            BigDecimal decimal1 = new BigDecimal(sameNum);
            BigDecimal decimal2 = new BigDecimal(ranking);
            proportion = decimal1.divide(decimal2,  2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
        }
        userMap.put("proportion", proportion);

        return userMap;
    }

    /**
     * 查询联考一分一段中历年同位分
     *
     * @param years 年份
     * @param score 分数
     * @return 联考一分一段
     */
    @Override
    public List<MallExamSection> selectMallExamSectionClosestListBy(String years, Integer score) {
        return mallExamSectionMapper.selectMallExamSectionClosestListBy(years, score);
    }
}
