package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbMemberMajor;
import com.ruoyi.hezhi.domain.dto.MemberMajorDTO;
import com.ruoyi.hezhi.mapper.TbMemberMajorMapper;
import com.ruoyi.hezhi.service.ITbMemberMajorService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户题库专业Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@Service
public class TbMemberMajorServiceImpl implements ITbMemberMajorService
{
    @Autowired
    private TbMemberMajorMapper tbMemberMajorMapper;

    /**
     * 查询用户题库专业
     *
     * @param memberMajorId 用户题库专业主键
     * @return 用户题库专业
     */
    @Override
    public TbMemberMajor selectTbMemberMajorByMemberMajorId(Long memberMajorId)
    {
        return tbMemberMajorMapper.selectTbMemberMajorByMemberMajorId(memberMajorId);
    }

    /**
     * 查询用户题库专业列表
     *
     * @param tbMemberMajor 用户题库专业
     * @return 用户题库专业
     */
    @Override
    public List<TbMemberMajor> selectTbMemberMajorList(TbMemberMajor tbMemberMajor)
    {
        return tbMemberMajorMapper.selectTbMemberMajorList(tbMemberMajor);
    }

    /**
     * 新增用户题库专业
     *
     * @param tbMemberMajor 用户题库专业
     * @return 结果
     */
    @Override
    public int insertTbMemberMajor(TbMemberMajor tbMemberMajor)
    {
        tbMemberMajor.setCreateTime(DateUtils.getNowDate());
        return tbMemberMajorMapper.insertTbMemberMajor(tbMemberMajor);
    }

    /**
     * 修改用户题库专业
     *
     * @param tbMemberMajor 用户题库专业
     * @return 结果
     */
    @Override
    public int updateTbMemberMajor(TbMemberMajor tbMemberMajor)
    {
        tbMemberMajor.setUpdateTime(DateUtils.getNowDate());
        return tbMemberMajorMapper.updateTbMemberMajor(tbMemberMajor);
    }

    /**
     * 批量删除用户题库专业
     *
     * @param memberMajorIds 需要删除的用户题库专业主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberMajorByMemberMajorIds(Long[] memberMajorIds)
    {
        return tbMemberMajorMapper.deleteTbMemberMajorByMemberMajorIds(memberMajorIds);
    }

    /**
     * 删除用户题库专业信息
     *
     * @param memberMajorId 用户题库专业主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberMajorByMemberMajorId(Long memberMajorId)
    {
        return tbMemberMajorMapper.deleteTbMemberMajorByMemberMajorId(memberMajorId);
    }

    /**
     * 用户新增题库专业
     *
     * @param memberMajorDTO
     * @return ok
     */
    @Override
    public AjaxResult insertMemberMajor(MemberMajorDTO memberMajorDTO) {
        TbMemberMajor tbMemberMajor = tbMemberMajorMapper.selectTbMemberMajorByMemberAndMajorId(memberMajorDTO.getMemberUserId(), memberMajorDTO.getMajorId(), memberMajorDTO.getCourseId());
        if (tbMemberMajor != null) {
            tbMemberMajor.setUpdateTime(DateUtils.getNowDate());
            tbMemberMajorMapper.updateTbMemberMajor(tbMemberMajor);
        } else {
            TbMemberMajor memberMajor = new TbMemberMajor();
            memberMajor.setMemberUserId(memberMajorDTO.getMemberUserId());
            memberMajor.setMajorId(memberMajorDTO.getMajorId());
            memberMajor.setResId(memberMajorDTO.getCourseId());
            memberMajor.setCreateTime(DateUtils.getNowDate());
            memberMajor.setType(2);
            tbMemberMajorMapper.insertTbMemberMajor(memberMajor);
        }
        return AjaxResult.success();
    }

    /**
     * 用户新增题库专业列表
     *
     * @param params 参数
     * @return 用户新增题库专业列表
     */
    @Override
    public List<Map<String, Object>> getMemberMajorList(Map<String, Object> params) {
        List<Map<String, Object>> memberMajorList = new ArrayList<>();
        List<Map<String, Object>> majorList = tbMemberMajorMapper.getMemberMajorList(params);
        if (majorList.isEmpty()) {
//            memberMajorList = tbMemberMajorMapper.getMemberMajorRecommendClassList();
        } else {
            memberMajorList = majorList;
        }
        Long courseId = MapUtils.getLong(params, "courseId");
        if (courseId != null){
            List<Map<String, Object>> memberMajorByCourseId = tbMemberMajorMapper.getMemberMajorByCourseId(params);
            if (!memberMajorByCourseId.isEmpty()){
                Map<String, Object> element = memberMajorByCourseId.get(0);
                long count = memberMajorList.stream().filter(item -> MapUtils.getLong(item, "courseId").equals(courseId)).count();
                if (count <= 0){
                    memberMajorList.add(0, element);
                }
            }
        }
        return memberMajorList;
    }
}
