package com.fs.course.service.impl;

import java.util.Collections;
import java.util.List;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsUserCourseStudyLog;
import com.fs.course.mapper.FsUserCourseStudyLogMapper;
import com.fs.course.param.FsUserCourseListUParam;
import com.fs.course.vo.FsUserCourseStudyListUVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCourseStudyMapper;
import com.fs.course.domain.FsUserCourseStudy;
import com.fs.course.service.IFsUserCourseStudyService;

/**
 * 课程用户关联Service业务层处理
 *
 * @author fs
 * @date 2024-05-15
 */
@Service
public class FsUserCourseStudyServiceImpl implements IFsUserCourseStudyService
{
    @Autowired
    private FsUserCourseStudyMapper fsUserCourseStudyMapper;

    @Autowired
    private FsUserCourseStudyLogMapper fsUserCourseStudyLogMapper;

    /**
     * 查询课程用户关联
     *
     * @param studyId 课程用户关联主键
     * @return 课程用户关联
     */
    @Override
    public FsUserCourseStudy selectFsUserCourseStudyByStudyId(String studyId)
    {
        return fsUserCourseStudyMapper.selectFsUserCourseStudyByStudyId(studyId);
    }

    /**
     * 查询课程用户关联列表
     *
     * @param fsUserCourseStudy 课程用户关联
     * @return 课程用户关联
     */
    @Override
    public List<FsUserCourseStudy> selectFsUserCourseStudyList(FsUserCourseStudy fsUserCourseStudy)
    {
        return fsUserCourseStudyMapper.selectFsUserCourseStudyList(fsUserCourseStudy);
    }

    /**
     * 新增课程用户关联
     *
     * @param fsUserCourseStudy 课程用户关联
     * @return 结果
     */
    @Override
    public int insertFsUserCourseStudy(FsUserCourseStudy fsUserCourseStudy)
    {
        fsUserCourseStudy.setCreateTime(DateUtils.getNowDate());
        return fsUserCourseStudyMapper.insertFsUserCourseStudy(fsUserCourseStudy);
    }

    /**
     * 修改课程用户关联
     *
     * @param fsUserCourseStudy 课程用户关联
     * @return 结果
     */
    @Override
    public int updateFsUserCourseStudy(FsUserCourseStudy fsUserCourseStudy)
    {
        fsUserCourseStudy.setUpdateTime(DateUtils.getNowDate());
        return fsUserCourseStudyMapper.updateFsUserCourseStudy(fsUserCourseStudy);
    }

    /**
     * 批量删除课程用户关联
     *
     * @param studyIds 需要删除的课程用户关联主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseStudyByStudyIds(String[] studyIds)
    {
        return fsUserCourseStudyMapper.deleteFsUserCourseStudyByStudyIds(studyIds);
    }

    /**
     * 删除课程用户关联信息
     *
     * @param studyId 课程用户关联主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseStudyByStudyId(String studyId)
    {
        return fsUserCourseStudyMapper.deleteFsUserCourseStudyByStudyId(studyId);
    }

    @Override
    public List<FsUserCourseStudyListUVO> selectFsUserCourseStudyListUVO(FsUserCourseListUParam param) {
        List<FsUserCourseStudyListUVO> list = fsUserCourseStudyMapper.selectFsUserCourseStudyListUVO(param);
        for (FsUserCourseStudyListUVO vo : list){
           FsUserCourseStudyLog log = fsUserCourseStudyLogMapper.selectFsUserCourseStudyLogByStudyId(vo.getStudyId());
           if (log != null){
               vo.setTimes(log.getTimes());
               vo.setCurrentTimes(log.getCurrentTimes());
               vo.setCourseName(log.getCourseName());
               vo.setVideoId(log.getVideoId());
               vo.setVideoName(log.getVideoName());
               vo.setProgress(log.getRate().intValue());
               vo.setStudyTime(log.getCreateTime());
           }
        }
        Collections.sort(list, (o1, o2) -> o2.getStudyTime().compareTo(o1.getStudyTime()));
        return list;
    }
}
