package com.ynfy.buss.course.course.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.course.course.entity.Course;
import com.ynfy.buss.course.course.mapper.CourseMapper;
import com.ynfy.buss.course.course.service.ICourseService;
import com.ynfy.buss.course.coursecatalog.service.ICourseCatalogService;
import com.ynfy.buss.course.usercoursecatalog.entity.vo.UserCourseStudyVO;
import com.ynfy.buss.course.usercoursecatalog.service.IUserCourseCatalogService;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: course
 * @Author: jeecg-boot
 * @Date: 2023-10-24
 * @Version: V1.0
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseCatalogService courseCatalogService;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysCategoryService sysCategoryService;

    @Autowired
    private IUserCourseCatalogService userCourseCatalogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCourse(Course course) {
        saveOrUpdate(course);
        // 保存题目答案选项
        courseCatalogService.saveAll(course.getId(), course.getCatalogList());
    }

    /**
     * 分页查询
     */
    @Override
    public IPage<Course> selectCourseList(IPage<Course> page, Course course, LoginUser user) {
        return courseMapper.selectCourseList(page, course, user);
    }


    /**
     * 设置课程分类
     *
     * @param pageList
     */
    @Override
    public void setCategory(IPage<Course> pageList) {
        if (!Objects.isNull(pageList) && !CollectionUtils.isEmpty(pageList.getRecords())) {
            pageList.getRecords().forEach(record -> record.setCategoryName(String.join(",",
                    sysCategoryService.loadDictItem(record.getCategoryId()))));
        }
    }

    @Override
    public Course queryById(String id) {
        return courseMapper.queryById(id);
    }


    /**
     * 设置课程学习进度
     *
     * @param studyList
     * @param course
     */
    @Override
    public void setCourseStudyProcess(List<UserCourseStudyVO> studyList, Course course) {
        UserCourseStudyVO studyVO = studyList.stream().filter(o1 -> o1.getCourseId().equals(course.getId())).findFirst().get();
        course.setTaskNum(studyVO.getTaskNum());
        course.setFinishNum(studyVO.getFinishNum());
        course.setStudyNum(studyVO.getStudyNum());
        if (!Objects.isNull(studyVO.getTaskNum())) {
            //课程学习进度
            course.setCourseStudyProcess(NumberUtil.div(studyVO.getFinishNum(), studyVO.getTaskNum()).doubleValue());
        }
    }


    /**
     * 查询热门课程
     *
     * @param limit
     * @return
     */
    @Override
    public List<Course> listHotCourse(Integer limit) {
        return courseMapper.listHotCourse(limit);
    }

    /**
     * 设置用户学习情况
     *
     * @param courseList
     * @param userId
     */
    @Override
    public void setUserStudyInfo(List<Course> courseList, String userId) {
        List<String> courseIdList = courseList.stream().map(Course::getId).collect(Collectors.toList());
        List<UserCourseStudyVO> studyList = userCourseCatalogService.getUserCourseStudy(courseIdList, userId);
        if (!CollectionUtils.isEmpty(studyList)) {
            courseList.forEach((o) -> setCourseStudyProcess(studyList, o));
        }
    }

    /**
     * 统计课程学习人数
     *
     * @param courseId
     * @return
     */
    @Override
    public Integer countCourseLearner(String courseId) {
        return courseMapper.countCourseLearner(courseId);
    }

    /**
     * 删除课程及目录
     *
     * @param courseId
     */
    @Override
    @Transactional
    public void removeCourse(String courseId) {
        removeById(courseId);
        courseCatalogService.remove(courseId);
    }

    @Override
    @Transactional
    public void removeBatch(String ids) {
        removeByIds(Arrays.asList(ids.split(",")));
        courseCatalogService.removeByCourseIds(Arrays.asList(ids.split(",")));
    }
}
