package org.dromara.system.controller.course;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.CourseTableVo;
import org.dromara.system.domain.bo.CourseTableBo;
import org.dromara.system.service.ICourseTableService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 课程
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/course/table")
public class CourseTableController extends BaseController {

    private final ICourseTableService courseTableService;

    /**
     * 查询课程列表
     */
    @SaCheckPermission("course:table:list")
    @GetMapping("/list")
    public TableDataInfo<CourseTableVo> list(CourseTableBo bo, PageQuery pageQuery) {
        return courseTableService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询课程列表PC
     */
    @SaCheckPermission("course:table:list")
    @GetMapping("/pc/list")
    public TableDataInfo<CourseTableVo> pcList(CourseTableBo bo, PageQuery pageQuery) {
        return courseTableService.queryPagePcList(bo, pageQuery);
    }

    /**
     * 导出课程列表
     */
    @SaCheckPermission("course:table:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CourseTableBo bo, HttpServletResponse response) {
        List<CourseTableVo> list = courseTableService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程", CourseTableVo.class, response);
    }

    /**
     * 获取课程详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("course:table:query")
    @GetMapping("/{id}")
    public R<CourseTableVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(courseTableService.queryById(id));
    }

    /**
     * 新增课程
     */
    @SaCheckPermission("course:table:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CourseTableBo bo) {
        return courseTableService.insertByBo(bo);
    }

    /**
     * 修改课程
     */
    @SaCheckPermission("course:table:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CourseTableBo bo) {
        return toAjax(courseTableService.updateByBo(bo));
    }

    /**
     * 删除课程
     *
     * @param ids 主键串
     */
    @SaCheckPermission("course:table:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(courseTableService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * app查询班级授课老师和名称
     * @param schoolId
     * @param classId
     * @return
     */
    @GetMapping(value = "/now")
    public R<Map<String, Object>> now(Long schoolId, Long classId)
    {
        return courseTableService.now(schoolId,classId);
    }
}
