package com.xinyilu.web.controller.personalcheckreport;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.exception.ServiceException;
import com.xinyilu.common.utils.StringUtils;
import com.xinyilu.common.utils.poi.ExcelUtil;
import com.xinyilu.system.domain.dto.PersonalExamineAndApproveDTO;
import com.xinyilu.system.domain.dto.UploadPersonalCheckReportDTO;
import com.xinyilu.system.domain.personalcheckreport.PersonalCheckReport;
import com.xinyilu.system.domain.vo.personalcheckreport.PersonalCheckReportVO;
import com.xinyilu.system.domain.vo.personalcheckreport.PersonalExamineAndApproveVO;
import com.xinyilu.system.service.personalcheckreport.IPersonalCheckReportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 个人体质检测报告Controller
 *
 * @author xinyilu
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/system/personalCheckReport")
public class PersonalCheckReportController extends BaseController {
    @Resource
    private IPersonalCheckReportService personalCheckReportService;


    /**
     * 查询个人体质检测报告列表
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(PersonalCheckReportVO personalCheckReport) {
        startPage();
        List<PersonalCheckReportVO> list = personalCheckReportService.selectPersonalCheckReportList(personalCheckReport);
        return getDataTable(list);
    }


    /**
     * 个人体质检测报告,一键审核功能-列表(无分页)
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(PersonalExamineAndApproveVO personalExamineAndApproveVO) {
        List<PersonalExamineAndApproveDTO> list = personalCheckReportService.selectPersonalCheckReportListAll(personalExamineAndApproveVO);
        return AjaxResult.success(list);
    }

    /**
     * 导出个人体质检测报告列表
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:export')")
    @Log(title = "个人体质检测报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PersonalCheckReportVO personalCheckReport) {
        List<PersonalCheckReportVO> list = personalCheckReportService.selectPersonalCheckReportList(personalCheckReport);
        ExcelUtil<PersonalCheckReportVO> util = new ExcelUtil<PersonalCheckReportVO>(PersonalCheckReportVO.class);
        util.exportExcel(response, list, "个人体质检测报告数据");
    }

    /**
     * 获取个人体质检测报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(personalCheckReportService.selectPersonalCheckReportById(id));
    }

    /**
     * 新增个人体质检测报告
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:add')")
    @Log(title = "个人体质检测报告", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody PersonalCheckReport personalCheckReport) {
        return toAjax(personalCheckReportService.insertPersonalCheckReport(personalCheckReport));
    }

    /**
     * 修改个人体质检测报告
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:edit')")
    @Log(title = "个人体质检测报告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated(Update.class) PersonalCheckReport personalCheckReport) {
        return personalCheckReportService.updatePersonalCheckReport(personalCheckReport);
    }

    /**
     * 删除个人体质检测报告
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:remove')")
    @Log(title = "个人体质检测报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(personalCheckReportService.deletePersonalCheckReportByIds(ids));
    }

    /**
     * 审核报告预览下载
     *
     * @param id       个人体质检测报告id
     * @param response 响应消息
     */
    @PreAuthorize("@ss.hasPermi('system:personalCheckReport:download')")
    @PostMapping("/download")
    public void downloadReport(@RequestParam(value = "id") Long id, HttpServletResponse response) {
        if (Objects.isNull(id)) {
            throw new ServiceException("请选择所要下载的报告记录信息！");
        }
        personalCheckReportService.downLoadAddress(id, response);
    }

    /**
     * 判断个人检测报告是否存在
     *
     * @param studentId 学生id
     * @param batchCode 批次编号
     * @return 结果
     */
    @GetMapping("/existCheckReport")
    public AjaxResult existCheckReport(Long studentId, String batchCode) {
        if (Objects.isNull(studentId)) {
            return AjaxResult.error("学生信息不能为空！");
        }
        if (StringUtils.isBlank(batchCode)) {
            return AjaxResult.error("批次信息不能为空！");
        }
        return personalCheckReportService.existCheckReport(studentId, batchCode);
    }

    /**
     * 上传个人学生报告信息
     *
     * @param checkReportDTO 输入参数信息
     * @return 上传结果
     * @throws Exception 异常信息
     */
    @PostMapping("/uploadPersonalCheckReport")
    public AjaxResult uploadPersonalCheckReport(@Validated(Insert.class) UploadPersonalCheckReportDTO checkReportDTO) throws Exception {
        return personalCheckReportService.uploadPersonalCheckReport(checkReportDTO);
    }
}
