package org.dromara.system.controller.leave;

import java.util.List;

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.AskLeaveCourseVo;
import org.dromara.system.domain.bo.AskLeaveCourseBo;
import org.dromara.system.service.IAskLeaveCourseService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 请假课程信息
 *
 * @author boman
 * @date 2024-11-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/leaveCourse")
public class AskLeaveCourseController extends BaseController {

    private final IAskLeaveCourseService askLeaveCourseService;

    /**
     * 查询请假课程信息列表
     */
    @SaCheckPermission("system:leaveCourse:list")
    @GetMapping("/list")
    public TableDataInfo<AskLeaveCourseVo> list(AskLeaveCourseBo bo, PageQuery pageQuery) {
        return askLeaveCourseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出请假课程信息列表
     */
    @SaCheckPermission("system:leaveCourse:export")
    @Log(title = "请假课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AskLeaveCourseBo bo, HttpServletResponse response) {
        List<AskLeaveCourseVo> list = askLeaveCourseService.queryList(bo);
        ExcelUtil.exportExcel(list, "请假课程信息", AskLeaveCourseVo.class, response);
    }

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

    /**
     * 新增请假课程信息
     */
    @SaCheckPermission("system:leaveCourse:add")
    @Log(title = "请假课程信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AskLeaveCourseBo bo) {
        return toAjax(askLeaveCourseService.insertByBo(bo));
    }

    /**
     * 修改请假课程信息
     */
    @SaCheckPermission("system:leaveCourse:edit")
    @Log(title = "请假课程信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AskLeaveCourseBo bo) {
        return toAjax(askLeaveCourseService.updateByBo(bo));
    }

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