package com.ruoyi.web.controller.system.check;

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.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TdCheck;
import com.ruoyi.system.service.ITdCheckService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/system/checkresult")
public class TdCheckResultController extends BaseController {
    private String prefix = "system/checkresult";
    @Autowired
    private ITdCheckService tdCheckService;

    @RequiresPermissions("system:checkresult:view")
    @GetMapping()
    public String checkresult()
    {
        return prefix + "/checkresult";
    }

    /**
     * 查询检查结果管理列表
     */
    @RequiresPermissions("system:checkresult:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TdCheck tdCheck)
    {
        startPage();
        List<TdCheck> list = tdCheckService.selectTdCheckList(tdCheck);
        return getDataTable(list);
    }

    /**
     * 导出检查结果管理列表
     */
    @RequiresPermissions("system:checkresult:export")
    @Log(title = "检查结果管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TdCheck tdCheck)
    {
        List<TdCheck> list = tdCheckService.selectTdCheckList(tdCheck);
        ExcelUtil<TdCheck> util = new ExcelUtil<TdCheck>(TdCheck.class);
        return util.exportExcel(list, "检查结果管理数据");
    }

    /**
     * 修改检查结果管理
     */
    @RequiresPermissions("system:checkresult:edit")
    @GetMapping("/edit/{checkId}")
    public String edit(@PathVariable("checkId") Long checkId, ModelMap mmap)
    {
        TdCheck tdCheck = tdCheckService.selectTdCheckByCheckId(checkId);
        mmap.put("tdCheck", tdCheck);
        return prefix + "/edit";
    }

    /**
     * 修改保存检查结果管理
     */
    @RequiresPermissions("system:checkresult:edit")
    @Log(title = "检查结果管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdCheck tdCheck)
    {
        return toAjax(tdCheckService.updateTdCheck(tdCheck));
    }

    /**
     * 删除检查结果管理
     */
    @RequiresPermissions("system:checkresult:remove")
    @Log(title = "检查结果管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tdCheckService.deleteTdCheckByCheckIds(ids));
    }

    /**
     * 检查 报告管理
     */
    @RequiresPermissions("system:checkresult:check")
    @GetMapping("/checkresult/{checkId}")
    public String selfcheck(@PathVariable("checkId") Long checkId, ModelMap mmap)
    {
        TdCheck tdCheck = tdCheckService.selectTdCheckByCheckId(checkId);
        mmap.put("sysuser",getSysUser());
        mmap.put("tdCheck", tdCheck);
        return prefix + "/docheck";
    }

    /**
     * 保存检查检查结果
     */
    @RequiresPermissions("system:checkresult:check")
    @Log(title = "检查结果管理", businessType = BusinessType.CHECK)
    @PostMapping("/checkresult")
    @ResponseBody
    public AjaxResult selfcheckSave(TdCheck tdCheck)
    {
        if (Objects.equals(tdCheck.getCheckresult1(),"0")
                && Objects.equals(tdCheck.getCheckresult2(),"0")
                && Objects.equals(tdCheck.getCheckresult3(),"0")
                && Objects.equals(tdCheck.getCheckresult4(),"0")
                && Objects.equals(tdCheck.getCheckresult5(),"0")
                && Objects.equals(tdCheck.getCheckresult6(),"0")){
            tdCheck.setCheckState("0");
        }else {
            tdCheck.setCheckState("1");
        }
        return toAjax(tdCheckService.updateTdCheck(tdCheck));
    }

    /**
     * 打印操作
     */
    @RequiresPermissions("system:checkresult:print")
    @GetMapping("/checkprint/{checkId}")
    public String checkresultPrint(@PathVariable("checkId") Long checkId, ModelMap mmap)
    {
        TdCheck tdCheckResult = tdCheckService.selectTdCheckByCheckId(checkId);
        mmap.put("tdCheckResult", tdCheckResult);
        return prefix + "/checkresultprint";
    }

}
