package org.dromara.ygpassport.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.core.ExcelResult;
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.ygpassport.domain.bo.StudentListBo;
import org.dromara.ygpassport.domain.vo.StudentListVo;
import org.dromara.ygpassport.listener.StudentListImportListener;
import org.dromara.ygpassport.service.IStudentListService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/studentList")
public class StudentListController extends BaseController {

    private final IStudentListService studentListService;

    /**
     * 查询学生信息列表
     */
    @SaCheckPermission("student:studentList:list")
    @GetMapping("/list")
    public TableDataInfo<StudentListVo> list(StudentListBo bo, PageQuery pageQuery) {
        return studentListService.queryPageList(bo, pageQuery);
    }

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

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("student:studentList:import")
    @PostMapping(value = "/importStudentData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        StudentListImportListener studentListImportListener = new StudentListImportListener(updateSupport);

        ExcelResult<StudentListVo> result = ExcelUtil.importExcel(file.getInputStream(), StudentListVo.class, studentListImportListener);
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "学生信息", StudentListVo.class, response);
    }

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

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

    /**
     * 修改学生信息
     */
    @SaCheckPermission("student:studentList:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentListBo bo) {
        return toAjax(studentListService.updateByBo(bo));
    }

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