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.RmDeploymentPolicy;
import com.ruoyi.rocketmq.service.IRmDeploymentPolicyService;
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-15
 */
@RestController
@RequestMapping("/policy")
public class RmDeploymentPolicyController extends BaseController
{
    @Autowired
    private IRmDeploymentPolicyService rmDeploymentPolicyService;

    /**
     * 查询服务器脚本策略列表
     */
    @RequiresPermissions("rocketmq:policy:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RmDeploymentPolicy rmDeploymentPolicy)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmDeploymentPolicy.getPageNum());
        pageDomain.setPageSize(rmDeploymentPolicy.getPageSize());
        startPage(pageDomain);
        List<RmDeploymentPolicy> list = rmDeploymentPolicyService.selectRmDeploymentPolicyList(rmDeploymentPolicy);
        return getDataTable(list);
    }

    /**
     * 导出服务器脚本策略列表
     */
    @RequiresPermissions("rocketmq:policy:export")
    @Log(title = "服务器脚本策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RmDeploymentPolicy rmDeploymentPolicy)
    {
        List<RmDeploymentPolicy> list = rmDeploymentPolicyService.selectRmDeploymentPolicyList(rmDeploymentPolicy);
        ExcelUtil<RmDeploymentPolicy> util = new ExcelUtil<RmDeploymentPolicy>(RmDeploymentPolicy.class);
        util.exportExcel(response, list, "服务器脚本策略数据");
    }

    /**
     * 获取服务器脚本策略详细信息
     */
    @RequiresPermissions("rocketmq:policy:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rmDeploymentPolicyService.selectRmDeploymentPolicyById(id));
    }

    /**
     * 新增服务器脚本策略
     */
    @RequiresPermissions("rocketmq:policy:add")
    @Log(title = "服务器脚本策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RmDeploymentPolicy rmDeploymentPolicy)
    {
        return toAjax(rmDeploymentPolicyService.insertRmDeploymentPolicy(rmDeploymentPolicy));
    }

    /**
     * 修改服务器脚本策略
     */
    @RequiresPermissions("rocketmq:policy:edit")
    @Log(title = "服务器脚本策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RmDeploymentPolicy rmDeploymentPolicy)
    {
        return toAjax(rmDeploymentPolicyService.updateRmDeploymentPolicy(rmDeploymentPolicy));
    }

    /**
     * 删除服务器脚本策略
     */
    @RequiresPermissions("rocketmq:policy:remove")
    @Log(title = "服务器脚本策略", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rmDeploymentPolicyService.deleteRmDeploymentPolicyByIds(ids));
    }

    /**
     * 下发服务器脚本策略
     */
    @RequiresPermissions("rocketmq:policy:list")
    @GetMapping(value = "/issueDeploymentPolicy")
    public AjaxResult issueDeploymentPolicy(Long id)
    {
        return success(rmDeploymentPolicyService.issueDeploymentPolicy(id));
    }

    /**
     * 新增服务器脚本策略
     */
    @InnerAuth
    @PostMapping("/addDeployScript")
    public R<Integer> addDeployScript(@RequestBody RmDeploymentPolicy rmDeploymentPolicy)
    {
        return R.ok(rmDeploymentPolicyService.insertRmDeploymentPolicy(rmDeploymentPolicy));
    }
}
