package org.dromara.work.controller;

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.work.domain.vo.TpCoursesVo;
import org.dromara.work.domain.bo.TpCoursesBo;
import org.dromara.work.service.ITpCoursesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 课程
 *
 * @author Maosw
 * @date 2025-04-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/courses")
public class TpCoursesController extends BaseController {

    private final ITpCoursesService tpCoursesService;

    /**
     * 查询课程列表
     */
    @SaCheckPermission("work:courses:list")
    @GetMapping("/list")
    public TableDataInfo<TpCoursesVo> list(TpCoursesBo bo, PageQuery pageQuery) {
        return tpCoursesService.queryPageList(bo, pageQuery);
    }

    /**
     * 统计课程列表
     */
    @SaCheckPermission("work:courses:list")
    @GetMapping("/count")
    public R<Map<String, Object>> count(TpCoursesBo bo) {
        return R.ok(tpCoursesService.queryCount(bo));
    }

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

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

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

    /**
     * 修改课程
     */
    @SaCheckPermission("work:courses:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpCoursesBo bo) {
        return toAjax(tpCoursesService.updateByBo(bo));
    }

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