package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainStudentBo;
import org.dromara.main.domain.vo.MainStudentVo;
import org.dromara.main.service.IMainStudentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/student")
public class MainStudentController extends BaseController {

    private final IMainStudentService mainStudentService;

    /**
     * 查询学员列表
     */
//    @SaCheckPermission("main:student:list")
    @GetMapping("/list")
    public TableDataInfo<MainStudentVo> list(MainStudentBo bo, PageQuery pageQuery) {
        return mainStudentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学员列表
     */
//    @SaCheckPermission("main:student:export")
    @Log(title = "学员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MainStudentBo bo, HttpServletResponse response) {
        List<MainStudentVo> list = mainStudentService.queryList(bo);
        ExcelUtil.exportExcel(list, "学员信息", MainStudentVo.class, response);
    }

    /**
     * 获取学员详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("main:student:query")
    @GetMapping("/{id}")
    public R<MainStudentVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(mainStudentService.queryById(id));
    }

    /**
     * 新增学员
     */
//    @SaCheckPermission("main:student:add")
    @Log(title = "学员管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MainStudentBo bo) {
        return toAjax(mainStudentService.insertByBo(bo));
    }

    /**
     * 修改学员
     */
//    @SaCheckPermission("main:student:edit")
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MainStudentBo bo) {
        return toAjax(mainStudentService.updateByBo(bo));
    }

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


    /**
     * 更新学员状态
     */
//    @SaCheckPermission("main:student:edit")
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody MainStudentBo bo) {
        return toAjax(mainStudentService.updateStatus(bo.getId(), bo.getStatus()));
    }

    /**
     * 更新学员用户类型（黑名单管理）
     */
//    @SaCheckPermission("main:student:edit")
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateUserType")
    public R<Void> updateUserType(@RequestBody MainStudentBo bo) {
        return toAjax(mainStudentService.updateUserType(bo.getId(), bo.getUserType()));
    }
}
