package com.ruoyi.web.controller.risk;

import java.util.List;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
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.RiskTemplate;
import com.ruoyi.system.service.IRiskTemplateService;

/**
 * 模板管理控制器
 */
@Controller
@RequestMapping("/risk/template")
public class RiskTemplateController extends BaseController
{
    private String prefix = "risk/template";

    @Autowired
    private IRiskTemplateService riskTemplateService;

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

    @RequiresPermissions("risk:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RiskTemplate riskTemplate)
    {
        startPage();
        List<RiskTemplate> list = riskTemplateService.selectRiskTemplateList(riskTemplate);
        return getDataTable(list);
    }

    @Log(title = "模板管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("risk:template:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RiskTemplate riskTemplate)
    {
        List<RiskTemplate> list = riskTemplateService.selectRiskTemplateList(riskTemplate);
        ExcelUtil<RiskTemplate> util = new ExcelUtil<RiskTemplate>(RiskTemplate.class);
        return util.exportExcel(list, "模板管理数据");
    }

    @RequiresPermissions("risk:template:list")
    @GetMapping("/{templateId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return AjaxResult.success().put("data", riskTemplateService.selectRiskTemplateById(templateId));
    }

    @RequiresPermissions("risk:template:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @RequiresPermissions("risk:template:add")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated RiskTemplate riskTemplate)
    {
        riskTemplate.setCreateBy(getLoginName());
        return toAjax(riskTemplateService.insertRiskTemplate(riskTemplate));
    }

    @RequiresPermissions("risk:template:edit")
    @GetMapping("/edit/{templateId}")
    public String edit(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        mmap.put("riskTemplate", riskTemplateService.selectRiskTemplateById(templateId));
        return prefix + "/edit";
    }

    @RequiresPermissions("risk:template:edit")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated RiskTemplate riskTemplate)
    {
        riskTemplate.setUpdateBy(getLoginName());
        return toAjax(riskTemplateService.updateRiskTemplate(riskTemplate));
    }

    @RequiresPermissions("risk:template:remove")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(riskTemplateService.deleteRiskTemplateByIds(ids));
    }
}
