package org.dromara.system.controller.info;

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.system.domain.vo.StudentInfoVo;
import org.dromara.system.domain.bo.StudentInfoBo;
import org.dromara.system.service.IStudentInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 学生档案信息
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/info")
public class StudentInfoController extends BaseController {

    private final IStudentInfoService studentInfoService;

    /**
     * 查询学生档案信息列表
     */
    @SaCheckPermission("student:info:list")
    @GetMapping("/list")
    public TableDataInfo<StudentInfoVo> list(StudentInfoBo bo, PageQuery pageQuery) {
        return studentInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学生档案信息列表
     */
    @SaCheckPermission("student:info:export")
    @Log(title = "学生档案信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StudentInfoBo bo, HttpServletResponse response) {
        List<StudentInfoVo> list = studentInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "学生档案信息", StudentInfoVo.class, response);
    }

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

    /**
     * 新增学生档案信息
     */
    @SaCheckPermission("student:info:add")
    @Log(title = "学生档案信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StudentInfoBo bo) {
        return studentInfoService.insertByBo(bo);
    }

    /**
     * 修改学生档案信息
     */
    @SaCheckPermission("student:info:edit")
    @Log(title = "学生档案信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentInfoBo bo) {
        return toAjax(studentInfoService.updateByBo(bo));
    }

    /**
     * 删除学生档案信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("student:info:remove")
    @Log(title = "学生档案信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(studentInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 统计学生档案信息
     */
    @GetMapping("/statistics")
    public R<Map<String,Object>> statistics(StudentInfoBo bo)
    {
        return studentInfoService.statistics(bo);
    }
}
