package com.fs.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsCourseFinishTemp;
import com.fs.course.domain.FsCourseFinishTempParent;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.mapper.FsCourseFinishTempParentMapper;
import com.fs.course.service.IFsCourseFinishTempParentService;
import com.fs.course.service.IFsCourseFinishTempService;
import com.fs.course.service.IFsUserCourseService;
import com.fs.course.service.IFsUserCourseVideoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 完课模板Service业务层处理
 * 
 * @author 吴树波
 * @date 2025-05-22
 */
@Service
@AllArgsConstructor
public class FsCourseFinishTempParentServiceImpl extends ServiceImpl<FsCourseFinishTempParentMapper, FsCourseFinishTempParent> implements IFsCourseFinishTempParentService {

    private final IFsUserCourseService fsUserCourseService;
    private final IFsUserCourseVideoService fsUserCourseVideoService;
    private final IFsCourseFinishTempService fsCourseFinishTempService;

    /**
     * 查询完课模板
     * 
     * @param id 完课模板主键
     * @return 完课模板
     */
    @Override
    public FsCourseFinishTempParent selectFsCourseFinishTempParentById(Long id)
    {
        return baseMapper.selectFsCourseFinishTempParentById(id);
    }

    /**
     * 查询完课模板列表
     * 
     * @param fsCourseFinishTempParent 完课模板
     * @return 完课模板
     */
    @Override
    public List<FsCourseFinishTempParent> selectFsCourseFinishTempParentList(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        return baseMapper.selectFsCourseFinishTempParentList(fsCourseFinishTempParent);
    }

    /**
     * 新增完课模板
     * 
     * @param fsCourseFinishTempParent 完课模板
     * @return 结果
     */
    @Override
    public int insertFsCourseFinishTempParent(FsCourseFinishTempParent fsCourseFinishTempParent){
        Long courseId = fsCourseFinishTempParent.getCourseId();
        FsUserCourseVideo fsUserCourseVideo = new FsUserCourseVideo();
        fsUserCourseVideo.setCourseId(courseId);
        List<FsUserCourseVideo> videoList = fsUserCourseVideoService.selectFsUserCourseVideoListByCourseId(fsUserCourseVideo);
        int insert = baseMapper.insert(fsCourseFinishTempParent);
        fsCourseFinishTempParent.setCreateTime(DateUtils.getNowDate());
        videoList.forEach(e -> {
            FsCourseFinishTemp temp = new FsCourseFinishTemp();
            temp.setName(e.getFileName());
            temp.setStatus(fsCourseFinishTempParent.getStatus());
            temp.setSetting(fsCourseFinishTempParent.getSetting());
            temp.setChatSetting(fsCourseFinishTempParent.getChatSetting());
            temp.setCompanyId(fsCourseFinishTempParent.getCompanyId());
            temp.setCourseId(e.getCourseId());
            temp.setVideoId(e.getVideoId());
            temp.setCompanyUserIds(fsCourseFinishTempParent.getCompanyUserIds());
            temp.setIsAllCompanyUser(fsCourseFinishTempParent.getIsAllCompanyUser());
            temp.setParentId(fsCourseFinishTempParent.getId());
            fsCourseFinishTempService.insertFsCourseFinishTemp(temp);
        });
        return insert;
    }

    /**
     * 修改完课模板
     * 
     * @param fsCourseFinishTempParent 完课模板
     * @return 结果
     */
    @Override
    public int updateFsCourseFinishTempParent(FsCourseFinishTempParent fsCourseFinishTempParent)
    {
        fsCourseFinishTempParent.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(fsCourseFinishTempParent);
    }

    /**
     * 批量删除完课模板
     * 
     * @param ids 需要删除的完课模板主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseFinishTempParentByIds(Long[] ids){
        int i = baseMapper.deleteFsCourseFinishTempParentByIds(ids);
        fsCourseFinishTempService.deleteByParentIds(ids);
        return i;
    }

    /**
     * 删除完课模板信息
     * 
     * @param id 完课模板主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseFinishTempParentById(Long id){
        return deleteFsCourseFinishTempParentByIds(new Long[]{id});
    }
}
