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.RiskMaterial;
import com.ruoyi.system.service.IRiskMaterialService;

/**
 * 资料管理控制器
 */
@Controller
@RequestMapping("/risk/material")
public class RiskMaterialController extends BaseController
{
    private String prefix = "risk/material";

    @Autowired
    private IRiskMaterialService riskMaterialService;

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

    @RequiresPermissions("risk:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RiskMaterial riskMaterial)
    {
        startPage();
        List<RiskMaterial> list = riskMaterialService.selectRiskMaterialList(riskMaterial);
        return getDataTable(list);
    }

    @Log(title = "资料管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("risk:material:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RiskMaterial riskMaterial)
    {
        List<RiskMaterial> list = riskMaterialService.selectRiskMaterialList(riskMaterial);
        ExcelUtil<RiskMaterial> util = new ExcelUtil<RiskMaterial>(RiskMaterial.class);
        return util.exportExcel(list, "资料管理数据");
    }

    @RequiresPermissions("risk:material:list")
    @GetMapping("/{materialId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return AjaxResult.success().put("data", riskMaterialService.selectRiskMaterialById(materialId));
    }

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

    @RequiresPermissions("risk:material:add")
    @Log(title = "资料管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated RiskMaterial riskMaterial)
    {
        riskMaterial.setCreateBy(getLoginName());
        return toAjax(riskMaterialService.insertRiskMaterial(riskMaterial));
    }

    @RequiresPermissions("risk:material:edit")
    @GetMapping("/edit/{materialId}")
    public String edit(@PathVariable("materialId") Long materialId, ModelMap mmap)
    {
        mmap.put("riskMaterial", riskMaterialService.selectRiskMaterialById(materialId));
        return prefix + "/edit";
    }

    @RequiresPermissions("risk:material:edit")
    @Log(title = "资料管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated RiskMaterial riskMaterial)
    {
        riskMaterial.setUpdateBy(getLoginName());
        return toAjax(riskMaterialService.updateRiskMaterial(riskMaterial));
    }

    @RequiresPermissions("risk:material:remove")
    @Log(title = "资料管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(riskMaterialService.deleteRiskMaterialByIds(ids));
    }
}
