package com.ruoyi.mall.service.impl;

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

import cn.hutool.db.PageResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallExamDataMapper;
import com.ruoyi.mall.domain.MallExamData;
import com.ruoyi.mall.service.IMallExamDataService;

import javax.annotation.Resource;

/**
 * 报考数据Service业务层处理
 *
 * @author LCL
 * @date 2023-10-24
 */
@Service
public class MallExamDataServiceImpl implements IMallExamDataService
{
    @Resource
    private MallExamDataMapper mallExamDataMapper;

    /**
     * 查询报考数据
     *
     * @param examDataId 报考数据主键
     * @return 报考数据
     */
    @Override
    public MallExamData selectMallExamDataByExamDataId(Long examDataId)
    {
        return mallExamDataMapper.selectMallExamDataByExamDataId(examDataId);
    }

    /**
     * 查询报考数据列表
     *
     * @param mallExamData 报考数据
     * @return 报考数据
     */
    @Override
    public List<MallExamData> selectMallExamDataList(MallExamData mallExamData)
    {
        return mallExamDataMapper.selectMallExamDataList(mallExamData);
    }

    /**
     * 新增报考数据
     *
     * @param mallExamData 报考数据
     * @return 结果
     */
    @Override
    public int insertMallExamData(MallExamData mallExamData)
    {
        mallExamData.setCreateAt(DateUtils.getTime());
        return mallExamDataMapper.insertMallExamData(mallExamData);
    }

    /**
     * 修改报考数据
     *
     * @param mallExamData 报考数据
     * @return 结果
     */
    @Override
    public int updateMallExamData(MallExamData mallExamData)
    {
        mallExamData.setUpdateAt(DateUtils.getTime());
        return mallExamDataMapper.updateMallExamData(mallExamData);
    }

    /**
     * 批量删除报考数据
     *
     * @param examDataIds 需要删除的报考数据主键
     * @return 结果
     */
    @Override
    public int deleteMallExamDataByExamDataIds(Long[] examDataIds)
    {
        return mallExamDataMapper.deleteMallExamDataByExamDataIds(examDataIds);
    }

    /**
     * 删除报考数据信息
     *
     * @param examDataId 报考数据主键
     * @return 结果
     */
    @Override
    public int deleteMallExamDataByExamDataId(Long examDataId)
    {
        return mallExamDataMapper.deleteMallExamDataByExamDataId(examDataId);
    }

    /**
     * 查询报考数据-报考人数列表
     *
     * @return 报考数据集合
     */
    @Override
    public List<Map<String, Object>> selectNumberOfApplicantsList() {
        return mallExamDataMapper.selectNumberOfApplicantsList();
    }

    /**
     * 查询报考数据-招生计划人数
     *
     * @param years 年份
     * @return 数据
     */
    @Override
    public List<Map<String, Object>> selectEnrollmentNumber(String years) {
        List<Map<String, Object>> mapList = mallExamDataMapper.selectEnrollmentNumber(years);
        if (mapList.size() > 0) {
            for (Map<String, Object> map : mapList) {
                BigDecimal peopleNum = new BigDecimal(map.get("peopleNum").toString());// 计划招生人数
                BigDecimal num = new BigDecimal(map.get("num").toString());// 总人数

                map.put("proportion", peopleNum.multiply(new BigDecimal("100")).divide(num,  2, BigDecimal.ROUND_HALF_UP));
            }
        }
        return mapList;
    }

    /**
     * 查询报考数据-数据控线
     *
     * @return 报考数据集合
     */
    @Override
    public Map<Object, List<Map<String, Object>>> selectDataControlCableList() {
        List<Map<String, Object>> mapList = mallExamDataMapper.selectDataControlCableList();

        return mapList.stream().collect(
                Collectors.groupingBy(item -> item.get("years").toString())
        );
    }
}
