package org.dromara.system.controller.register;

import java.util.List;

import cn.dev33.satoken.annotation.SaCheckRole;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.constant.TenantConstants;
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.RegisterTeacherClassVo;
import org.dromara.system.domain.bo.RegisterTeacherClassBo;
import org.dromara.system.service.IRegisterTeacherClassService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 注册-老师-班级（学科）
 *
 * @author boman
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/register/class")
public class RegisterTeacherClassController extends BaseController {

    private final IRegisterTeacherClassService registerTeacherClassService;

    /**
     * 查询注册-老师-班级（学科）列表
     */
    @SaCheckPermission("register:class:list")
    @GetMapping("/list")
    public TableDataInfo<RegisterTeacherClassVo> list(RegisterTeacherClassBo bo, PageQuery pageQuery) {
        return registerTeacherClassService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出注册-老师-班级（学科）列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("register:class:export")
    @Log(title = "注册-老师-班级（学科）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RegisterTeacherClassBo bo, HttpServletResponse response) {
        List<RegisterTeacherClassVo> list = registerTeacherClassService.queryList(bo);
        ExcelUtil.exportExcel(list, "注册-老师-班级（学科）", RegisterTeacherClassVo.class, response);
    }

    /**
     * 获取注册-老师-班级（学科）详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("register:class:query")
    @GetMapping("/{id}")
    public R<RegisterTeacherClassVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(registerTeacherClassService.queryById(id));
    }

    /**
     * 新增注册-老师-班级（学科）
     */
    @SaCheckPermission("register:class:add")
    @Log(title = "注册-老师-班级（学科）", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RegisterTeacherClassBo bo) {
        return toAjax(registerTeacherClassService.insertByBo(bo));
    }

    /**
     * 修改注册-老师-班级（学科）
     */
    @SaCheckPermission("register:class:edit")
    @Log(title = "注册-老师-班级（学科）", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RegisterTeacherClassBo bo) {
        return toAjax(registerTeacherClassService.updateByBo(bo));
    }

    /**
     * 删除注册-老师-班级（学科）
     *
     * @param ids 主键串
     */
    @SaCheckPermission("register:class:remove")
    @Log(title = "注册-老师-班级（学科）", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(registerTeacherClassService.deleteWithValidByIds(List.of(ids), true));
    }
}
