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.ArrayList;
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)  {
        //示例数据
        List<TbExamRegistrationVO> templateList = new ArrayList<>();
        TbExamRegistrationVO tbExamRegistrationVO = new TbExamRegistrationVO();
        tbExamRegistrationVO.setName("张三");
        tbExamRegistrationVO.setFormerName("曾用名");
        tbExamRegistrationVO.setSex(0);
        tbExamRegistrationVO.setNation("汉族");
        tbExamRegistrationVO.setIdentityCard("110101199001011234");
        tbExamRegistrationVO.setBirthday("1990-01-01");
        tbExamRegistrationVO.setNativePlace("河北省,唐山市,路北区");
        tbExamRegistrationVO.setEducation("本科");
        tbExamRegistrationVO.setMajor("计算机科学与技术");
        tbExamRegistrationVO.setGraduationSchool("河南大学");
        tbExamRegistrationVO.setEntranceYear("2018");
        tbExamRegistrationVO.setContactInformation("13677777777");
        tbExamRegistrationVO.setProvinceName("河南省");
        tbExamRegistrationVO.setCityName("郑州市");
        tbExamRegistrationVO.setAreaName("中原区");
        tbExamRegistrationVO.setAddress("中原区金水路");
        tbExamRegistrationVO.setContactPhone("13677777777");
        tbExamRegistrationVO.setAgencyName("机构名称");
        tbExamRegistrationVO.setCareerName("职业名称");
        tbExamRegistrationVO.setExamLevel("初级");
        tbExamRegistrationVO.setRegistrationBatch("报名批次");
        tbExamRegistrationVO.setStudyCenter("学习中心名称");
        tbExamRegistrationVO.setAgencyCode("机构代码");
        templateList.add(tbExamRegistrationVO);
        ImportTemplateUtil.downloadTemplate(response, TbExamRegistrationVO.class, "考试报名导入模板",templateList);
    }

    /**
     * 导入考试报名
     */
    @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));
    }
}
