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.RiskOrg;
import com.ruoyi.system.service.IRiskOrgService;

/**
 * 组织机构管理控制器
 */
@Controller
@RequestMapping("/risk/org")
public class RiskOrgController extends BaseController
{
    private String prefix = "risk/org";

    @Autowired
    private IRiskOrgService riskOrgService;

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

    @RequiresPermissions("risk:org:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RiskOrg riskOrg)
    {
        startPage();
        List<RiskOrg> list = riskOrgService.selectRiskOrgList(riskOrg);
        return getDataTable(list);
    }

    @Log(title = "组织机构", businessType = BusinessType.EXPORT)
    @RequiresPermissions("risk:org:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RiskOrg riskOrg)
    {
        List<RiskOrg> list = riskOrgService.selectRiskOrgList(riskOrg);
        ExcelUtil<RiskOrg> util = new ExcelUtil<RiskOrg>(RiskOrg.class);
        return util.exportExcel(list, "组织机构数据");
    }

    @RequiresPermissions("risk:org:list")
    @GetMapping("/{orgId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("orgId") Long orgId)
    {
        return AjaxResult.success().put("data", riskOrgService.selectRiskOrgById(orgId));
    }

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

    @RequiresPermissions("risk:org:add")
    @Log(title = "组织机构", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated RiskOrg riskOrg)
    {
        riskOrg.setCreateBy(getLoginName());
        return toAjax(riskOrgService.insertRiskOrg(riskOrg));
    }

    @RequiresPermissions("risk:org:edit")
    @GetMapping("/edit/{orgId}")
    public String edit(@PathVariable("orgId") Long orgId, ModelMap mmap)
    {
        mmap.put("riskOrg", riskOrgService.selectRiskOrgById(orgId));
        return prefix + "/edit";
    }

    @RequiresPermissions("risk:org:edit")
    @Log(title = "组织机构", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated RiskOrg riskOrg)
    {
        riskOrg.setUpdateBy(getLoginName());
        return toAjax(riskOrgService.updateRiskOrg(riskOrg));
    }

    @RequiresPermissions("risk:org:remove")
    @Log(title = "组织机构", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(riskOrgService.deleteRiskOrgByIds(ids));
    }
}
