package com.fs.course.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.course.vo.FsUserCourseListPVO;
import com.fs.his.utils.RedisCacheUtil;
import com.fs.his.vo.OptionsVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.service.IFsUserCourseService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author fs
 * @date 2024-05-15
 */
@RestController
@RequestMapping("/course/userCourse")
public class FsUserCourseController extends BaseController
{
    @Autowired
    private IFsUserCourseService fsUserCourseService;

    @Autowired
    private RedisCacheUtil redisCacheUtil;
    /**
     * 查询课程列表
     */
//    @PreAuthorize("@ss.hasPermi('course:userCourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserCourse fsUserCourse)
    {
        startPage();
        List<FsUserCourseListPVO> list = fsUserCourseService.selectFsUserCourseListPVO(fsUserCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:userCourse:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserCourse fsUserCourse)
    {
        List<FsUserCourse> list = fsUserCourseService.selectFsUserCourseList(fsUserCourse);
        ExcelUtil<FsUserCourse> util = new ExcelUtil<FsUserCourse>(FsUserCourse.class);
        return util.exportExcel(list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:userCourse:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return AjaxResult.success(fsUserCourseService.selectFsUserCourseByCourseId(courseId));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('course:userCourse:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserCourse fsUserCourse)
    {
        fsUserCourseService.insertFsUserCourse(fsUserCourse);
        redisCacheUtil.delRedisKey("getCourseList");

        return toAjax(1);
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('course:userCourse:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserCourse fsUserCourse)
    {
        fsUserCourseService.updateFsUserCourse(fsUserCourse);
        redisCacheUtil.delRedisKey("getCourseList");
        return toAjax(1);
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('course:userCourse:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        fsUserCourseService.deleteFsUserCourseByCourseIds(courseIds);
        redisCacheUtil.delRedisKey("getCourseList");
        return toAjax(1);
    }


    @GetMapping("/getAllList")
    public R getAllList()
    {
        List<OptionsVO> list = fsUserCourseService.selectFsUserCourseAllList();
        return R.ok().put("data", list);
    }

    @Log(title = "课程上架", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsShow")
    public AjaxResult updateIsShow(@RequestBody FsUserCourse fsUserCourse)
    {
        fsUserCourseService.updateFsUserCourse(fsUserCourse);
        redisCacheUtil.delRedisKey("getCourseList");
       return toAjax(1);
    }

    @PreAuthorize("@ss.hasPermi('course:userCourse:putOn')")
    @Log(title = "课程批量上架", businessType = BusinessType.UPDATE)
    @PostMapping("/putOn/{courseIds}")
    public AjaxResult putOn(@PathVariable Long[] courseIds)
    {
        fsUserCourseService.updateFsUserCourseIsShow(courseIds,1);
        redisCacheUtil.delRedisKey("getCourseList");
        return toAjax(1);
    }

    @PreAuthorize("@ss.hasPermi('course:userCourse:putOn')")
    @Log(title = "课程批量下架", businessType = BusinessType.UPDATE)
    @PostMapping("/pullOff/{courseIds}")
    public AjaxResult pullOff(@PathVariable Long[] courseIds)
    {
        fsUserCourseService.updateFsUserCourseIsShow(courseIds,0);
        redisCacheUtil.delRedisKey("getCourseList");
        return toAjax(1);
    }
}
