package com.ruoyi.mall.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.mapper.MallSchoolBatchLinkMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMajorRankingMapper;
import com.ruoyi.mall.domain.MallMajorRanking;
import com.ruoyi.mall.service.IMallMajorRankingService;

import javax.annotation.Resource;

/**
 * 专业榜单Service业务层处理
 *
 * @author LCL
 * @date 2023-10-24
 */
@Service
public class MallMajorRankingServiceImpl implements IMallMajorRankingService
{
    @Resource
    private MallMajorRankingMapper mallMajorRankingMapper;

    @Resource
    private MallSchoolBatchLinkMapper mallSchoolBatchLinkMapper;

    /**
     * 查询专业榜单
     *
     * @param rankingId 专业榜单主键
     * @return 专业榜单
     */
    @Override
    public MallMajorRanking selectMallMajorRankingByRankingId(Long rankingId)
    {
        return mallMajorRankingMapper.selectMallMajorRankingByRankingId(rankingId);
    }

    /**
     * 查询专业榜单列表
     *
     * @param mallMajorRanking 专业榜单
     * @return 专业榜单
     */
    @Override
    public List<MallMajorRanking> selectMallMajorRankingList(MallMajorRanking mallMajorRanking)
    {
        return mallMajorRankingMapper.selectMallMajorRankingList(mallMajorRanking);
    }

    /**
     * 新增专业榜单
     *
     * @param mallMajorRanking 专业榜单
     * @return 结果
     */
    @Override
    public int insertMallMajorRanking(MallMajorRanking mallMajorRanking)
    {
        mallMajorRanking.setCreateAt(DateUtils.getTime());
        return mallMajorRankingMapper.insertMallMajorRanking(mallMajorRanking);
    }

    /**
     * 修改专业榜单
     *
     * @param mallMajorRanking 专业榜单
     * @return 结果
     */
    @Override
    public int updateMallMajorRanking(MallMajorRanking mallMajorRanking)
    {
        mallMajorRanking.setUpdateAt(DateUtils.getTime());
        return mallMajorRankingMapper.updateMallMajorRanking(mallMajorRanking);
    }

    /**
     * 批量删除专业榜单
     *
     * @param rankingIds 需要删除的专业榜单主键
     * @return 结果
     */
    @Override
    public int deleteMallMajorRankingByRankingIds(Long[] rankingIds)
    {
        return mallMajorRankingMapper.deleteMallMajorRankingByRankingIds(rankingIds);
    }

    /**
     * 删除专业榜单信息
     *
     * @param rankingId 专业榜单主键
     * @return 结果
     */
    @Override
    public int deleteMallMajorRankingByRankingId(Long rankingId)
    {
        return mallMajorRankingMapper.deleteMallMajorRankingByRankingId(rankingId);
    }

    /**
     * 专业榜单信息列表
     *
     * @param type 类型：0-排行榜，1-专业强校，2-省份强校，3-非正常研究
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectMajorRankingList(String type) {
        return mallMajorRankingMapper.selectMajorRankingList(type);
    }

    /**
     * 专业榜单信息学校列表
     *
     * @param rankingId 专业榜单主键
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectMajorRankingSchoolList(Long rankingId) {
        List<Map<String, Object>> mapList = mallMajorRankingMapper.selectMajorRankingSchoolList(rankingId);
        if (!mapList.isEmpty()){
            for (Map<String, Object> map : mapList) {
                Long schoolId = MapUtils.getLong(map, "schoolId");
                List<Long> longs = mallSchoolBatchLinkMapper.selectMallSchoolBatchLinkBySchoolId(schoolId);
                if (!longs.isEmpty()){
                    map.put("batchId", longs.get(0));
                }
            }
        }
        return mapList;
    }

    /**
     * 专业榜单信息浏览量+1
     *
     * @param rankingId 专业榜单主键
     * @return 结果
     */
    @Override
    public int updateMallMajorRankingViewNumByRankingId(Long rankingId) {
        return mallMajorRankingMapper.updateMallMajorRankingViewNumByRankingId(rankingId);
    }
}
