package com.ruoyi.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallUserMajorIntentionMapper;
import com.ruoyi.mall.domain.MallUserMajorIntention;
import com.ruoyi.mall.service.IMallUserMajorIntentionService;

import javax.annotation.Resource;

/**
 * 用户意向专业Service业务层处理
 *
 * @author LCL
 * @date 2023-10-25
 */
@Service
public class MallUserMajorIntentionServiceImpl implements IMallUserMajorIntentionService
{
    @Resource
    private MallUserMajorIntentionMapper mallUserMajorIntentionMapper;
    @Resource
    private MallMemberUserMapper memberUserMapper;

    /**
     * 查询用户意向专业
     *
     * @param intentionId 用户意向专业主键
     * @return 用户意向专业
     */
    @Override
    public MallUserMajorIntention selectMallUserMajorIntentionByIntentionId(Long intentionId)
    {
        return mallUserMajorIntentionMapper.selectMallUserMajorIntentionByIntentionId(intentionId);
    }

    /**
     * 查询用户意向专业列表
     *
     * @param mallUserMajorIntention 用户意向专业
     * @return 用户意向专业
     */
    @Override
    public List<MallUserMajorIntention> selectMallUserMajorIntentionList(MallUserMajorIntention mallUserMajorIntention)
    {
        return mallUserMajorIntentionMapper.selectMallUserMajorIntentionList(mallUserMajorIntention);
    }

    /**
     * 新增用户意向专业
     *
     * @param mallUserMajorIntention 用户意向专业
     * @return 结果
     */
    @Override
    public int insertMallUserMajorIntention(MallUserMajorIntention mallUserMajorIntention)
    {
        mallUserMajorIntention.setCreateAt(DateUtils.getTime());
        return mallUserMajorIntentionMapper.insertMallUserMajorIntention(mallUserMajorIntention);
    }

    /**
     * 修改用户意向专业
     *
     * @param mallUserMajorIntention 用户意向专业
     * @return 结果
     */
    @Override
    public int updateMallUserMajorIntention(MallUserMajorIntention mallUserMajorIntention)
    {
        mallUserMajorIntention.setUpdateAt(DateUtils.getTime());
        return mallUserMajorIntentionMapper.updateMallUserMajorIntention(mallUserMajorIntention);
    }

    /**
     * 批量删除用户意向专业
     *
     * @param intentionIds 需要删除的用户意向专业主键
     * @return 结果
     */
    @Override
    public int deleteMallUserMajorIntentionByIntentionIds(Long[] intentionIds)
    {
        return mallUserMajorIntentionMapper.deleteMallUserMajorIntentionByIntentionIds(intentionIds);
    }

    /**
     * 删除用户意向专业信息
     *
     * @param intentionId 用户意向专业主键
     * @return 结果
     */
    @Override
    public int deleteMallUserMajorIntentionByIntentionId(Long intentionId)
    {
        return mallUserMajorIntentionMapper.deleteMallUserMajorIntentionByIntentionId(intentionId);
    }

    /**
     * 用户意向专业列表
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectMallUserMajorIntentionListByUserId(Long userId) {
        return mallUserMajorIntentionMapper.selectMallUserMajorIntentionListByUserId(userId);
    }

    /**
     * 根据用户ID删除用户意向专业信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteMallUserMajorIntentionByUserId(Long userId) {
        return mallUserMajorIntentionMapper.deleteMallUserMajorIntentionByUserId(userId);
    }

    /**
     * 根据用户ID查询志愿填报-首页信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public Map<String, Object> selectVolunteerFillingIndexByUserId(Long userId) {

        MallMemberUser memberUser = memberUserMapper.selectMallMemberUserById(userId);
        List<Map<String, Object>> mapList = mallUserMajorIntentionMapper.selectMallUserMajorIntentionListByUserId(userId);
        List<Map<String, Object>> batchSchoolNum = mallUserMajorIntentionMapper.selectBatchSchoolNum();

        Map<String, Object> result = new HashMap<>();
        // 发展方向：0-未设置，1-以就业为导向，2-以学科专业为导向，3-已进体制内为导向，4-以保研/考研为导向
        result.put("developDirection", memberUser.getDevelopDirection());

        // 意向专业
        if (mapList.size() > 0) {
            result.put("majorIntention", mapList.get(0).get("majorName"));
        } else {
            result.put("majorIntention", null);
        }
        // 各批次学校数量列表
        result.put("batchSchoolNum", batchSchoolNum);
        // 预计可报
        result.put("reportable", batchSchoolNum.stream().mapToInt(e -> Integer.parseInt(e.get("schoolNum").toString())).sum());
        return result;
    }
}
