package org.dromara.ygpassport.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
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.ResearchBo;
import org.dromara.ygpassport.domain.vo.ResearchVo;
import org.dromara.ygpassport.domain.vo.StudentImportVo;
import org.dromara.ygpassport.listener.ResearchImportListener;
import org.dromara.ygpassport.service.IResearchService;
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("/system/research")
public class ResearchController extends BaseController {

    private final IResearchService researchService;

    /**
     * 查询科研列表
     */
    @SaCheckPermission("system:research:list")
    @GetMapping("/list")
    public TableDataInfo<ResearchVo> list(ResearchBo bo, PageQuery pageQuery) {
        return researchService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出科研列表
     */
    @SaCheckPermission("system:research:export")
    @Log(title = "科研", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ResearchBo bo, HttpServletResponse response) {
        List<ResearchVo> list = researchService.queryList(bo);
        ExcelUtil.exportExcel(list, "科研", ResearchVo.class, response);
    }

    /**
     * 导入科研信息数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "科研信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:research:import")
    @PostMapping(value = "/importResearchData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ResearchImportListener listener = new ResearchImportListener(updateSupport);
        ExcelResult<ResearchVo> result = ExcelUtil.importExcel(file.getInputStream(), ResearchVo.class, listener);
        return R.ok(result.getAnalysis());
    }

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

    /**
     * 获取科研详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:research:query")
    @GetMapping("/{id}")
    public R<ResearchVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(researchService.queryById(id));
    }

    /**
     * 新增科研
     */
    @SaCheckPermission("system:research:add")
    @Log(title = "科研", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ResearchBo bo) {
        return toAjax(researchService.insertByBo(bo));
    }

    /**
     * 修改科研
     */
    @SaCheckPermission("system:research:edit")
    @Log(title = "科研", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ResearchBo bo) {
        return toAjax(researchService.updateByBo(bo));
    }

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