package com.xinyilu.web.controller.kindergartenCheckReport;

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.system.domain.dto.KindergartenCheckReportDTO;
import com.xinyilu.system.domain.dto.KindergartenExamineAndApproveDTO;
import com.xinyilu.system.domain.dto.UploadKindergartenCheckReportDTO;
import com.xinyilu.system.domain.kindergartenCheckReport.KindergartenCheckReport;
import com.xinyilu.system.service.kindergartenCheckReport.IKindergartenCheckReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 幼儿园检测报告Controller
 *
 * @author xinyilu
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/system/kindergartenCheckReport")
public class KindergartenCheckReportController extends BaseController {
    @Autowired
    private IKindergartenCheckReportService kindergartenCheckReportService;

    /**
     * 查询幼儿园检测报告列表
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(KindergartenCheckReportDTO kindergartenCheckReport) {
        startPage();
        List<KindergartenCheckReportDTO> list = kindergartenCheckReportService.selectKindergartenCheckReportList(kindergartenCheckReport);
        return getDataTable(list);
    }

    /**
     * 幼儿园检测报告,一键审核功能-列表(无分页)
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(@RequestParam(value = "batchCode") String batchCode) {
        if (StringUtils.isBlank(batchCode)) {
            return AjaxResult.error("请传入批次编码");
        }
        List<KindergartenExamineAndApproveDTO> list = kindergartenCheckReportService.selectKindergartenCheckReportListAll(batchCode);
        return AjaxResult.success(list);
    }

    /**
     * 获取幼儿园检测报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(kindergartenCheckReportService.selectKindergartenCheckReportById(id));
    }

    /**
     * 新增幼儿园检测报告
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:add')")
    @Log(title = "幼儿园检测报告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody KindergartenCheckReport kindergartenCheckReport) {
        return toAjax(kindergartenCheckReportService.insertKindergartenCheckReport(kindergartenCheckReport));
    }

    /**
     * 审核幼儿园检测报告
     *
     * @param kindergartenCheckReport 输入参数
     * @return 审核结果
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:edit')")
    @Log(title = "幼儿园检测报告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody KindergartenCheckReport kindergartenCheckReport) {
        return kindergartenCheckReportService.updateKindergartenCheckReport(kindergartenCheckReport);
    }

    /**
     * 删除幼儿园检测报告
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:remove')")
    @Log(title = "幼儿园检测报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(kindergartenCheckReportService.deleteKindergartenCheckReportByIds(ids));
    }

    /**
     * 下载幼儿园检测报告
     *
     * @param id  幼儿园id
     * @param res 响应信息
     */
    @PreAuthorize("@ss.hasPermi('system:kindergartenCheckReport:download')")
    @PostMapping("/download")
    public void downloadReport(@RequestParam(value = "id") Long id, HttpServletResponse res) {
        if (Objects.isNull(id)) {
            throw new ServiceException("请选择所要下载的报告记录信息！");
        }
        kindergartenCheckReportService.downloadReport(id, res);
    }

    /**
     * 判断幼儿园检测报告是否存在
     *
     * @param kindergartenId 幼儿园id
     * @param batchCode      批次编号
     * @return 结果
     */
    @GetMapping("/existKindergartenCheckReport")
    public AjaxResult existKindergartenCheckReport(Long kindergartenId, String batchCode) {
        if (Objects.isNull(kindergartenId)) {
            return AjaxResult.error("幼儿园信息不能为空！");
        }
        if (StringUtils.isBlank(batchCode)) {
            return AjaxResult.error("批次信息不能为空！");
        }
        return kindergartenCheckReportService.existKindergartenCheckReport(kindergartenId, batchCode);
    }

    /**
     * 幼儿园检测报告上传接口
     *
     * @param checkReportDTO 输入参数
     * @return AjaxResult 上传结果
     * @throws Exception 异常信息
     */
    @PostMapping("/uploadKindergartenCheckReport")
    public AjaxResult uploadKindergartenCheckReport(@Validated(Insert.class) UploadKindergartenCheckReportDTO checkReportDTO) throws Exception {
        return kindergartenCheckReportService.uploadKindergartenCheckReport(checkReportDTO);
    }
}
