package com.ruoyi.hezhi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hezhi.domain.TbExamRegistration;
import com.ruoyi.hezhi.domain.vo.TbExamRegistrationVO;
import com.ruoyi.hezhi.domain.vo.TopicImportTemplateVO;
import com.ruoyi.hezhi.service.ITbExamRegistrationService;
import com.ruoyi.hezhi.util.ImportTemplateUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.init.ResourceReader;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 考试报名Controller
 *
 * @author ruoyi
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/hezhi/examRegistration")
public class TbExamRegistrationController extends BaseController
{
    @Autowired
    private ITbExamRegistrationService tbExamRegistrationService;

    /**
     * 查询考试报名列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExamRegistration tbExamRegistration)
    {
        startPage();
        List<TbExamRegistration> list = tbExamRegistrationService.selectTbExamRegistrationList(tbExamRegistration);
        return getDataTable(list);
    }

    /**
     * 导出考试报名列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:export')")
    @Log(title = "考试报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbExamRegistration tbExamRegistration)
    {
        List<TbExamRegistration> list = tbExamRegistrationService.selectTbExamRegistrationList(tbExamRegistration);
        ExcelUtil<TbExamRegistration> util = new ExcelUtil<TbExamRegistration>(TbExamRegistration.class);
        util.exportExcel(response, list, "考试报名数据");
    }

    /**
     * 获取考试报名详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:query')")
    @GetMapping(value = "/{examRegistrationId}")
    public AjaxResult getInfo(@PathVariable("examRegistrationId") Long examRegistrationId)
    {
        return success(tbExamRegistrationService.selectTbExamRegistrationByExamRegistrationId(examRegistrationId));
    }

    /**
     * 获取证书发货详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:query')")
    @GetMapping(value = "/fahuo/{examRegistrationId}")
    public AjaxResult getFahuoInfo(@PathVariable("examRegistrationId") Long examRegistrationId)
    {
        return success(tbExamRegistrationService.selectTbExamRegistrationByExamRegistrationIdFahuo(examRegistrationId));
    }

    /**
     * 新增考试报名
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:add')")
    @Log(title = "考试报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExamRegistration tbExamRegistration)
    {
        try{
            return toAjax(tbExamRegistrationService.insertTbExamRegistration(tbExamRegistration));
        }catch (ServiceException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改考试报名
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:edit')")
    @Log(title = "考试报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExamRegistration tbExamRegistration)
    {
        return toAjax(tbExamRegistrationService.updateTbExamRegistration(tbExamRegistration));
    }

    /**
     * 删除考试报名
     */
    @PreAuthorize("@ss.hasPermi('hezhi:examRegistration:remove')")
    @Log(title = "考试报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examRegistrationIds}")
    public AjaxResult remove(@PathVariable Long[] examRegistrationIds)
    {
        return toAjax(tbExamRegistrationService.deleteTbExamRegistrationByExamRegistrationIds(examRegistrationIds));
    }

    /**
     * 证书发货
     * @return 结果
     */
    @Log(title = "证书发货", businessType = BusinessType.UPDATE)
    @PostMapping("/certificateSend")
    public AjaxResult certificateSend(@RequestBody TbExamRegistration tbExamRegistration){
        return toAjax(tbExamRegistrationService.certificateSend(tbExamRegistration));
    }

    /**
     * 下载导入模板
     *
     * @param response response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)  {
        ImportTemplateUtil.downloadTemplate(response, TbExamRegistrationVO.class, "考试报名导入模板");
    }

    /**
     * 导入考试报名
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        List<TbExamRegistrationVO> list = new ExcelUtil<TbExamRegistrationVO>(TbExamRegistrationVO.class)
                .importExcel(file.getInputStream());
        return AjaxResult.success(tbExamRegistrationService.importData(list));
    }
}
