package com.ruoyi.hezhi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbCourse;
import com.ruoyi.hezhi.domain.TbTypeCourse;
import com.ruoyi.hezhi.domain.vo.CaseTypeVO;
import com.ruoyi.hezhi.service.ITbCaseTypeService;
import com.ruoyi.hezhi.service.ITbCourseService;
import com.ruoyi.hezhi.service.ITbTypeCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程Controller
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/hezhi/course")
public class TbCourseController extends BaseController
{
    @Autowired
    private ITbCourseService tbCourseService;
    @Autowired
    private ITbTypeCourseService tbTypeCourseService;
    @Autowired
    private ITbCaseTypeService tbCaseTypeService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbCourse tbCourse)
    {
        startPage();
        List<TbCourse> list = tbCourseService.selectTbCourseList(tbCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCourse tbCourse)
    {
        List<TbCourse> list = tbCourseService.selectTbCourseList(tbCourse);
        ExcelUtil<TbCourse> util = new ExcelUtil<TbCourse>(TbCourse.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        AjaxResult ajax = AjaxResult.success();
        // 全部分类list
        List<CaseTypeVO> allList = tbCaseTypeService.getCaseTypeList();
        TbCourse tbCourse = tbCourseService.selectTbCourseByCourseId(courseId);
        ajax.put("classList", allList);
        if (tbCourse != null) {
            ajax.put(AjaxResult.DATA_TAG, tbCourse);

//            TbTypeCourse tbTypeCourse = new TbTypeCourse();
//            tbTypeCourse.setCourseId(courseId);
//            List<TbTypeCourse> typeCourseList = tbTypeCourseService.selectTbTypeCourseList(tbTypeCourse);

//            ajax.put("classIds", typeCourseList.stream().map(TbTypeCourse::getCaseTypeId).collect(Collectors.toList()));
        }
        return ajax;

        // return success(tbCourseService.selectTbCourseByCourseId(courseId));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCourse tbCourse)
    {
        if (tbCourse.getMajorId() == null) {
            return AjaxResult.error("请选择课程分类");
        }
        tbCourse.setViewsNum(0);
        tbCourseService.insertTbCourse(tbCourse);
        Long[] longs = {tbCourse.getMajorId()};
        tbTypeCourseService.batchInsertTbTypeCourse(longs, tbCourse.getCourseId());

        return AjaxResult.success();
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCourse tbCourse)
    {
        if (tbCourse.getMajorId() == null) {
            return AjaxResult.error("请选择课程分类");
        }
        tbTypeCourseService.deleteTbTypeCourseByCourseId(tbCourse.getCourseId());
        Long[] longs = {tbCourse.getMajorId()};
        tbTypeCourseService.batchInsertTbTypeCourse(longs, tbCourse.getCourseId());
        return toAjax(tbCourseService.updateTbCourse(tbCourse));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('hezhi:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(tbCourseService.deleteTbCourseByCourseIds(courseIds));
    }

    /**
     * 所有课程列表
     * @return
     */
    @GetMapping("/getAllCourseList")
    public AjaxResult getAllCourseList() {
        return AjaxResult.success(tbCourseService.getAllCourseList());
    }
}
