package org.dromara.system.controller.course;

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.CourseChangeVo;
import org.dromara.system.domain.bo.CourseChangeBo;
import org.dromara.system.service.ICourseChangeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final ICourseChangeService courseChangeService;

    /**
     * 查询调课列表
     */
    @SaCheckPermission("course:change:list")
    @GetMapping("/list")
    public TableDataInfo<CourseChangeVo> list(CourseChangeBo bo, PageQuery pageQuery) {
        return courseChangeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询调课列表(与课表一起)
     */
    @GetMapping("/list/class")
    public TableDataInfo<CourseChangeVo> courseList(CourseChangeBo bo, PageQuery pageQuery)
    {
        return courseChangeService.courseList(bo,pageQuery);
    }

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

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

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

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

    /**
     * 回复调课
     */
    @SaCheckPermission("course:change:reply")
    @Log(title = "调课", businessType = BusinessType.UPDATE)
    @GetMapping("/reply")
    public R<Void> reply(CourseChangeBo bo)
    {
        return courseChangeService.updateCourseChangeReply(bo);
    }

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