package com.tongran.rocketmq.controller;

import com.tongran.common.core.domain.R;
import com.tongran.common.core.utils.poi.ExcelUtil;
import com.tongran.common.core.web.controller.BaseController;
import com.tongran.common.core.web.domain.AjaxResult;
import com.tongran.common.core.web.page.PageDomain;
import com.tongran.common.core.web.page.TableDataInfo;
import com.tongran.common.log.annotation.Log;
import com.tongran.common.log.enums.BusinessType;
import com.tongran.common.security.annotation.InnerAuth;
import com.tongran.common.security.annotation.RequiresPermissions;
import com.tongran.rocketmq.domain.RmDeploymentPolicy;
import com.tongran.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 tongran
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/policy")
@RequiresPermissions("rocketmq:policy")
public class RmDeploymentPolicyController extends BaseController
{
    @Autowired
    private IRmDeploymentPolicyService rmDeploymentPolicyService;

    /**
     * 查询服务器脚本策略列表
     */
    @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);
    }

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

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

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

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

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

    /**
     * 下发服务器脚本策略
     */
    @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));
    }
}
