package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hezhi.domain.TbCourse;
import com.ruoyi.hezhi.domain.TbCourseChapter;
import com.ruoyi.hezhi.domain.TbExamRegistration;
import com.ruoyi.hezhi.domain.TbMajorClass;
import com.ruoyi.hezhi.mapper.*;
import com.ruoyi.hezhi.service.ITbCourseChapterService;
import com.ruoyi.hezhi.service.ITbMajorClassService;
import com.ruoyi.hezhi.service.ITbTeacherCourseService;
import com.ruoyi.hezhi.service.ITbTypeCourseService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 专业分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-16
 */
@Service
public class TbMajorClassServiceImpl implements ITbMajorClassService
{
    @Autowired
    private TbMajorClassMapper tbMajorClassMapper;
    @Autowired
    private ITbCourseChapterService courseChapterService;
    @Autowired
    private ITbTeacherCourseService tbTeacherCourseService;
    @Autowired
    private TbTypeCourseMapper tbTypeCourseMapper;
    @Resource
    private TbCourseMapper tbCourseMapper;

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private TbExamRegistrationMapper tbExamRegistrationMapper;

    /**
     * 查询专业分类
     *
     * @param majorId 专业分类主键
     * @return 专业分类
     */
    @Override
    public TbMajorClass selectTbMajorClassByMajorId(Long majorId)
    {
        return tbMajorClassMapper.selectTbMajorClassByMajorId(majorId);
    }

    /**
     * 查询专业分类列表
     *
     * @param tbMajorClass 专业分类
     * @return 专业分类
     */
    @Override
    public List<TbMajorClass> selectTbMajorClassList(TbMajorClass tbMajorClass)
    {
        return tbMajorClassMapper.selectTbMajorClassList(tbMajorClass);
    }

    /**
     * 查询专业分类列表
     *
     * @return 专业分类
     */
    @Override
    public List<TbMajorClass> getAllMajorClassList()
    {
        return tbMajorClassMapper.getAllMajorClassList();
    }

    /**
     * 新增专业分类
     *
     * @param tbMajorClass 专业分类
     * @return 结果
     */
    @Override
    public int insertTbMajorClass(TbMajorClass tbMajorClass)
    {
        tbMajorClass.setCreateTime(DateUtils.getNowDate());
        tbMajorClass.setDelFlag(0);
        return tbMajorClassMapper.insertTbMajorClass(tbMajorClass);
    }

    /**
     * 修改专业分类
     *
     * @param tbMajorClass 专业分类
     * @return 结果
     */
    @Override
    public int updateTbMajorClass(TbMajorClass tbMajorClass)
    {
        tbMajorClass.setUpdateTime(DateUtils.getNowDate());
        return tbMajorClassMapper.updateTbMajorClass(tbMajorClass);
    }

    /**
     * 批量删除专业分类
     *
     * @param majorIds 需要删除的专业分类主键
     * @return 结果
     */
    @Override
    public int deleteTbMajorClassByMajorIds(Long[] majorIds)
    {
        return tbMajorClassMapper.deleteTbMajorClassByMajorIds(majorIds);
    }

    /**
     * 删除专业分类信息
     *
     * @param majorId 专业分类主键
     * @return 结果
     */
    @Override
    public int deleteTbMajorClassByMajorId(Long majorId)
    {
        return tbMajorClassMapper.deleteTbMajorClassByMajorId(majorId);
    }

    /**
     * 专业分类列表
     *
     * @param params 参数
     * @return 专业分类列表
     */
    @Override
    public List<Map<String, Object>> selectTbMajorClassMapList(Map<String, Object> params) {
        return tbMajorClassMapper.selectTbMajorClassMapList(params);
    }

    /**
     * 专业分类树列表
     *
     * @return 专业分类树列表
     */
    @Override
    public List<TbMajorClass> selectTbMajorClassTreeList() {
        TbMajorClass tbMajorClass = new TbMajorClass();
        tbMajorClass.setStatus(0);
        tbMajorClass.setDelFlag(0);
        List<TbMajorClass> list = tbMajorClassMapper.selectTbMajorClassList(tbMajorClass);
        return this.toTree(list, 0L);
    }

    /**
     * 方式一：使用两层循环实现
     * @param treeList
     * @return
     */
    @Override
    public List<TbMajorClass> toTree(List<TbMajorClass> treeList, Long pid) {
        List<TbMajorClass> retList = new ArrayList<TbMajorClass>();
        for (TbMajorClass parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(parent);
            }
            for (TbMajorClass childOne : treeList) {
                if (childOne.getPid().equals(parent.getMajorId())) {
                    if (parent.getChildrenList() == null) {
                        parent.setChildrenList(new ArrayList<TbMajorClass>());
                    }
                    parent.getChildrenList().add(childOne);
                }
            }
        }
        return retList;
    }

    /**
     * 专业课程列表
     *
     * @param params 参数
     * @return 专业课程列表
     */
    @Override
    public List<Map<String, Object>> getMajorCourseList(Map<String, Object> params) {
        List<Map<String, Object>> majorCourseList = tbMajorClassMapper.getMajorCourseList(params);
        String memberUserId = MapUtils.getString(params, "memberUserId");
        if (StringUtils.isNotEmpty(memberUserId)&&CollUtil.isNotEmpty(majorCourseList)){
            //stream获取courseId的List
            List<Long> courseIdList = majorCourseList.stream().map(item -> MapUtils.getLong(item, "courseId")).collect(Collectors.toList());
            List<TbExamRegistration> list = tbExamRegistrationMapper.selectCourseIsPay(memberUserId, courseIdList);
            majorCourseList.forEach(course -> {
                if (course != null){
                    if (CollUtil.isNotEmpty(list)){
                        list.forEach(item -> {
                            if (Objects.equals(MapUtils.getLong(course, "courseId"), item.getCourseId())){
                                course.put("isEnroll", item.getIsEnroll());
                                course.put("examRegistrationId", item.getExamRegistrationId());
                            }
                        });
                    }
                }
            });
        }
        return majorCourseList;
    }

    /**
     * 获取机构专业分类
     * @param evaluateAgencyId 机构ID
     * @return 专业课程列表
     */
    @Override
    public List<Map<String, Object>> getEvaluateAgencyMajorClass(Long evaluateAgencyId){
        return tbTypeCourseMapper.getTypeCourseByEvaluateAgencyId(evaluateAgencyId);
    }

    /**
     * 专业课程详情
     *
     * @param params 参数
     * @return 专业课程详情
     */
    @Override
    public Map<String, Object> getMajorCourseInfo(Map<String, Object> params) {
        Map<String, Object> majorCourseInfo = tbMajorClassMapper.getMajorCourseInfo(params);
        if (!majorCourseInfo.isEmpty()) {
            long courseId = MapUtils.getLong(majorCourseInfo, "courseId");
            if (majorCourseInfo.get("courseSyllabus") == null) {
                majorCourseInfo.put("courseSyllabus", courseChapterService.getCourseChapterTreeList(courseId));
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("courseId", courseId);
            majorCourseInfo.put("teacherList", tbTeacherCourseService.getTeacherList(map));

            majorCourseInfo.put("collegeImage", "https://image.xnszz.com/IMG_1417_1417@FpRfsd1u_dOMfKttUGaU7VeO7r1Z.jpg");

            //处理浏览量
            threadPoolTaskExecutor.execute(() -> {
                TbCourse tbCourse = tbCourseMapper.selectTbCourseByCourseId(courseId);
                if (StringUtils.isNotNull(tbCourse)){
                    tbCourse.setViewsNum(tbCourse.getViewsNum() + 1);
                    tbCourseMapper.updateTbCourse(tbCourse);
                }
            });
        }

        return majorCourseInfo;
    }

    /**
     * 课程师资列表
     *
     * @param params 参数
     * @return 课程师资列表
     */
    @Override
    public List<Map<String, Object>> getCourseTeacherList(Map<String, Object> params) {
        return tbMajorClassMapper.getCourseTeacherList(params);
    }

    /**
     * 查询专业分类列表-下拉框树列表
     *
     * @return 专业分类集合
     */
    @Override
    public List<Map<String,Object>> getTbMajorClassTreeList() {
        //返回数据map集合
        List<Map<String, Object>> resultMap = new ArrayList<>();

        //一级分类列表
        TbMajorClass majorClassOne = new TbMajorClass();
        majorClassOne.setStatus(0);
        majorClassOne.setDelFlag(0);
        majorClassOne.setPid(0L);
        List<TbMajorClass> majorClassOneList = tbMajorClassMapper.selectTbMajorClassList(majorClassOne);

        if (majorClassOneList.size() > 0){
            for (TbMajorClass majorOne : majorClassOneList) {
                Map<String, Object> majorParentMap = new HashMap<>();
                majorParentMap.put("value",majorOne.getMajorId());
                majorParentMap.put("label",majorOne.getMajorName());

                //查询二级分类列表
                TbMajorClass majorClassTwo = new TbMajorClass();
                majorClassTwo.setStatus(0);
                majorClassTwo.setDelFlag(0);
                majorClassTwo.setPid(majorOne.getMajorId());
                List<TbMajorClass> majorClassTwoList = tbMajorClassMapper.selectTbMajorClassList(majorClassTwo);

                List<Map<String, Object>> childMapList = new ArrayList<>();

                if (majorClassTwoList.size() > 0){
                    for (TbMajorClass majorTwo : majorClassTwoList) {
                        Map<String, Object> childMap = new HashMap<>();
                        childMap.put("value",majorTwo.getMajorId());
                        childMap.put("label",majorTwo.getMajorName());
                        childMapList.add(childMap);
                    }
                }
                majorParentMap.put("children", childMapList);

                resultMap.add(majorParentMap);
            }
        }
        return resultMap;
    }

    /**
     * 专业课程视频列表
     *
     * @param params 参数
     * @return 专业课程视频列表
     */
    @Override
    public List<Map<String, Object>> getMajorCourseVideoList(Map<String, Object> params) {
        return tbMajorClassMapper.getMajorCourseVideoList(params);
    }
}
