package com.fs.course.controller;


import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.course.param.FsUserCourseVideoParam;
import com.fs.course.service.IFsUserCourseVideoService;
import com.fs.course.vo.FsUserCourseVideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.core.web.service.TokenService;
import java.util.List;

/**
 * 课堂视频Controller
 *
 * @author fs
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/course/userCourseVideo")
public class FsUserCourseVideoController extends BaseController
{
    @Autowired
    private IFsUserCourseVideoService fsUserCourseVideoService;

    @Autowired
    private FsUserCourseVideoMapper fsUserCourseVideoMapper;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询课堂视频列表
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserCourseVideo fsUserCourseVideo)
    {
        startPage();
        List<FsUserCourseVideo> list = fsUserCourseVideoService.selectFsUserCourseVideoList(fsUserCourseVideo);
        return getDataTable(list);
    }

    /**
     * 导出课堂视频列表
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:export')")
    @Log(title = "课堂视频", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserCourseVideo fsUserCourseVideo)
    {
        List<FsUserCourseVideo> list = fsUserCourseVideoService.selectFsUserCourseVideoList(fsUserCourseVideo);
        ExcelUtil<FsUserCourseVideo> util = new ExcelUtil<FsUserCourseVideo>(FsUserCourseVideo.class);
        return util.exportExcel(list, "课堂视频数据");
    }

    /**
     * 获取课堂视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:query')")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable("videoId") Long videoId)
    {
        return AjaxResult.success(fsUserCourseVideoService.selectFsUserCourseVideoByVideoIdVO(videoId));
    }

    /**
     * 新增课堂视频
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:add')")
    @Log(title = "课堂视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserCourseVideo fsUserCourseVideo)
    {
        Long count = fsUserCourseVideoMapper.selectFsUserCourseVideoByCourseSort(fsUserCourseVideo.getCourseId(),fsUserCourseVideo.getCourseSort());
        if (count>0){
            return AjaxResult.error("课程排序重复");
        }
        return toAjax(fsUserCourseVideoService.insertFsUserCourseVideo(fsUserCourseVideo));
    }

    /**
     * 修改课堂视频
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:edit')")
    @Log(title = "更改公司红包金额", businessType = BusinessType.UPDATE)
    @PutMapping("/updateMoney")
    public R updateMoney(@RequestBody FsUserCourseVideoParam fsUserCourseVideo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserCourseVideo.setCompanyId(loginUser.getCompany().getCompanyId());
        return fsUserCourseVideoService.updateFsUserCourseVideoCompanyRedPacket(fsUserCourseVideo);
    }

    /**
     * 删除课堂视频
     */
    @PreAuthorize("@ss.hasPermi('course:userCourseVideo:remove')")
    @Log(title = "课堂视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{videoIds}")
    public AjaxResult remove(@PathVariable String[] videoIds)
    {
        return toAjax(fsUserCourseVideoService.deleteFsUserCourseVideoByVideoIds(videoIds));
    }


    @GetMapping("/getVideoListByCourseId")
    public TableDataInfo getVideoListByCourseId(FsUserCourseVideoParam fsUserCourseVideo)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserCourseVideo.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsUserCourseVideoVO> list = fsUserCourseVideoService.selectFsUserCourseVideoListByCourseIdAndCompany(fsUserCourseVideo);
        return getDataTable(list);
    }

    @GetMapping("/getSort/{courseId}")
    public R remove(@PathVariable("courseId") Long courseId)
    {

        return R.ok().put("data",fsUserCourseVideoService.selectCourseVideoSort(courseId));
    }
}
