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 org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
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.List;
import java.util.Objects;

/**
 * @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;

    @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 = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (StringUtils.isNotBlank(course.getName())) {
            course.setName(course.getName().replace("*", ""));
        }
        if (StringUtils.isNotBlank(user.getOrgCode())) {
            SysDepart depart = sysDepartService.queryByOrgCode(user.getOrgCode());
            if (!Objects.isNull(depart)) {
                user.setOrgCode(depart.getId());
            }
        }
        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());
        if (!Objects.isNull(studyVO.getTaskNum())) {
            //课程学习进度
            course.setCourseStudyProcess(NumberUtil.div(studyVO.getFinishNum(), studyVO.getTaskNum()).doubleValue());
        }
    }
}
