package com.ruoyi.rocketmq.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.rocketmq.domain.RmMonitorPolicy;
import com.ruoyi.rocketmq.service.IRmMonitorPolicyService;
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-09-10
 */
@RestController
@RequestMapping("/monitorPolicy")
public class RmMonitorPolicyController extends BaseController
{
    @Autowired
    private IRmMonitorPolicyService rmMonitorPolicyService;

    /**
     * 查询资源监控策略列表
     */
    @RequiresPermissions("rocketmq:policy:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmMonitorPolicy.getPageNum());
        pageDomain.setPageSize(rmMonitorPolicy.getPageSize());
        startPage(pageDomain);
        List<RmMonitorPolicy> list = rmMonitorPolicyService.selectRmMonitorPolicyList(rmMonitorPolicy);
        return getDataTable(list);
    }

    /**
     * 导出资源监控策略列表
     */
    @RequiresPermissions("rocketmq:policy:export")
    @Log(title = "资源监控策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        List<RmMonitorPolicy> list = rmMonitorPolicyService.selectRmMonitorPolicyList(rmMonitorPolicy);
        ExcelUtil<RmMonitorPolicy> util = new ExcelUtil<RmMonitorPolicy>(RmMonitorPolicy.class);
        util.showColumn(rmMonitorPolicy.getProperties());
        util.exportExcel(response, list, "资源监控策略数据");
    }

    /**
     * 获取资源监控策略详细信息
     */
    @RequiresPermissions("rocketmq:policy:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rmMonitorPolicyService.getRmMonitorPolicyMsgById(id));
    }

    /**
     * 新增资源监控策略
     */
    @RequiresPermissions("rocketmq:policy:add")
    @Log(title = "资源监控策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        int rows = rmMonitorPolicyService.addRmMonitorPolicy(rmMonitorPolicy);
        if(rows == -1){
            return AjaxResult.error("资源监控策略新增失败，该资源组已绑定其他策略");
        }
        return toAjax(rows);
    }

    /**
     * 修改资源监控策略
     */
    @RequiresPermissions("rocketmq:policy:edit")
    @Log(title = "资源监控策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        int rows = rmMonitorPolicyService.updateRmMonitorPolicy(rmMonitorPolicy);
        if(rows == -1){
            return AjaxResult.error("资源监控策略新增失败，该资源组已绑定其他策略");
        }
        return toAjax(rows);
    }
    /**
     * 资源监控策略下发
     */
    @RequiresPermissions("rocketmq:policy:edit")
    @Log(title = "资源监控策略下发", businessType = BusinessType.UPDATE)
    @GetMapping("/issuePolicy")
    public AjaxResult issuePolicy(Long id)
    {
        return toAjax(rmMonitorPolicyService.issuePolicy(id));
    }
    /**
     * 资源监控策略下发
     */
//    @RequiresPermissions("rocketmq:policy:edit")
    @Log(title = "issueSwitchPolicy", businessType = BusinessType.UPDATE)
    @GetMapping("/issueSwitchPolicy")
    public AjaxResult issueSwitchPolicy(Long id)
    {
        return toAjax(rmMonitorPolicyService.issueSwitchPolicy(id));
    }

    /**
     * 删除监控模板
     */
    @RequiresPermissions("rocketmq:policy:remove")
    @Log(title = "资源监控策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rmMonitorPolicyService.deleteRmMonitorPolicyByIds(ids));
    }

    /**
     * 新增交换机监控策略
     */
    @RequiresPermissions("rocketmq:policy:add")
    @Log(title = "资源监控策略", businessType = BusinessType.INSERT)
    @PostMapping("/addResourcePolicy")
    public AjaxResult addResourcePolicy(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        int rows = rmMonitorPolicyService.addResourcePolicy(rmMonitorPolicy);
        return toAjax(rows);
    }
     /**
     * 修改交换机监控策略
     */
    @RequiresPermissions("rocketmq:policy:update")
    @Log(title = "资源监控策略", businessType = BusinessType.INSERT)
    @PostMapping("/updateResourcePolicy")
    public AjaxResult updateResourcePolicy(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        int rows = rmMonitorPolicyService.updateResourcePolicy(rmMonitorPolicy);
        return toAjax(rows);
    }

    /**
     * 策略下发，内部调用
     * @param id
     * @return
     */
    @Log(title = "issueSwitchPolicy", businessType = BusinessType.UPDATE)
    @GetMapping("/innerIssueSwitchPolicy")
    @InnerAuth
    public R<String> innerIssueSwitchPolicy(Long id)
    {
        rmMonitorPolicyService.issueSwitchPolicy(id);
        return R.ok("优先级0策略下发成功");
    }
    /**
     * 查询资源监控策略列表
     */
    @PostMapping("/getPolicyMsgInner")
    @InnerAuth
    public R<List<RmMonitorPolicy>> getPolicyMsgInner(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        List<RmMonitorPolicy> list = rmMonitorPolicyService.selectRmMonitorPolicyList(rmMonitorPolicy);
        return R.ok(list);
    }
    /**
     * 修改资源监控策略
     */
    @PostMapping("/updatePolicyMsgInner")
    @InnerAuth
    public R<Integer> updatePolicyMsgInner(@RequestBody RmMonitorPolicy rmMonitorPolicy)
    {
        int rows = rmMonitorPolicyService.updatePolicyMsgInner(rmMonitorPolicy);
        return R.ok(rows);
    }
    /**
     * 修改资源监控策略
     */
    @GetMapping("/issueDefaultPolicyByClientId")
    @InnerAuth
    public R<Integer> issueDefaultPolicyByClientId(String clientId)
    {
        int rows = rmMonitorPolicyService.issueDefaultPolicyByClientId(clientId);
        return R.ok(rows);
    }

}
