package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.RmResourceGroup;
import com.ruoyi.system.service.IRmResourceGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 资源分组Controller
 * 
 * @author gyt
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/group")
public class RmResourceGroupController extends BaseController
{
    @Autowired
    private IRmResourceGroupService rmResourceGroupService;

    /**
     * 查询资源分组列表
     */
    @RequiresPermissions("system:group:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RmResourceGroup rmResourceGroup)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmResourceGroup.getPageNum());
        pageDomain.setPageSize(rmResourceGroup.getPageSize());
        startPage(pageDomain);
        List<RmResourceGroup> list = rmResourceGroupService.selectRmResourceGroupList(rmResourceGroup);
        return getDataTable(list);
    }

    /**
     * 导出资源分组列表
     */
    @RequiresPermissions("system:group:export")
    @Log(title = "资源分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody RmResourceGroup rmResourceGroup)
    {
        List<RmResourceGroup> list = rmResourceGroupService.selectRmResourceGroupList(rmResourceGroup);
        ExcelUtil<RmResourceGroup> util = new ExcelUtil<RmResourceGroup>(RmResourceGroup.class);
        util.showColumn(rmResourceGroup.getProperties());
        util.exportExcel(response, list, "资源分组数据");
    }

    /**
     * 获取资源分组详细信息
     */
    @RequiresPermissions("system:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rmResourceGroupService.selectRmResourceGroupById(id));
    }

    /**
     * 新增资源分组
     */
    @RequiresPermissions("system:group:add")
    @Log(title = "资源分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RmResourceGroup rmResourceGroup)
    {
        return toAjax(rmResourceGroupService.insertRmResourceGroup(rmResourceGroup));
    }

    /**
     * 修改资源分组
     */
    @RequiresPermissions("system:group:edit")
    @Log(title = "资源分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RmResourceGroup rmResourceGroup)
    {
        return toAjax(rmResourceGroupService.updateRmResourceGroup(rmResourceGroup));
    }

    /**
     * 删除资源分组
     */
    @RequiresPermissions("system:group:remove")
    @Log(title = "资源分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rmResourceGroupService.deleteRmResourceGroupByIds(ids));
    }

    /**
     * 获取资源分组详细信息
     */
    @InnerAuth
    @PostMapping(value = "/getResourceGroupMsgById")
    public R<RmResourceGroup> getResourceGroupMsgById(@RequestBody Long id)
    {
        return R.ok(rmResourceGroupService.selectRmResourceGroupById(id));
    }
    /**
     * 获取资源分组详细信息
     */
    @RequiresPermissions("system:group:query")
    @PostMapping(value = "/exitsResourceById")
    public AjaxResult exitsResourceById(@RequestBody RmResourceGroup rmResourceGroup)
    {
        // 根据资源id查询该资源是否已经在资源组中
        String exits = rmResourceGroupService.exitsResourceById(rmResourceGroup);
        return success(exits);
    }
}
