package com.tongran.rocketmq.controller;

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.RequiresPermissions;
import com.tongran.rocketmq.domain.InitialMemoryInfo;
import com.tongran.rocketmq.service.IInitialMemoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 内存监控信息Controller
 * 
 * @author tongran
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/memoryInfo")
@RequiresPermissions("rocketmq:traffic")
public class InitialMemoryInfoController extends BaseController
{
    @Autowired
    private IInitialMemoryInfoService initialMemoryInfoService;

    /**
     * 查询内存监控信息列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody InitialMemoryInfo initialMemoryInfo)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(initialMemoryInfo.getPageNum());
        pageDomain.setPageSize(initialMemoryInfo.getPageSize());
        startPage(pageDomain);
        List<InitialMemoryInfo> list = initialMemoryInfoService.selectInitialMemoryInfoList(initialMemoryInfo);
        return getDataTable(list);
    }

    /**
     * 导出内存监控信息列表
     */
    @Log(title = "内存监控信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InitialMemoryInfo initialMemoryInfo)
    {
        List<InitialMemoryInfo> list = initialMemoryInfoService.selectInitialMemoryInfoList(initialMemoryInfo);
        ExcelUtil<InitialMemoryInfo> util = new ExcelUtil<InitialMemoryInfo>(InitialMemoryInfo.class);
        util.exportExcel(response, list, "内存监控信息数据");
    }

    /**
     * 获取内存监控信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(initialMemoryInfoService.selectInitialMemoryInfoById(id));
    }

    /**
     * 新增内存监控信息
     */
    @Log(title = "内存监控信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InitialMemoryInfo initialMemoryInfo)
    {
        return toAjax(initialMemoryInfoService.insertInitialMemoryInfo(initialMemoryInfo));
    }

    /**
     * 修改内存监控信息
     */
    @Log(title = "内存监控信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InitialMemoryInfo initialMemoryInfo)
    {
        return toAjax(initialMemoryInfoService.updateInitialMemoryInfo(initialMemoryInfo));
    }

    /**
     * 删除内存监控信息
     */
    @Log(title = "内存监控信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(initialMemoryInfoService.deleteInitialMemoryInfoByIds(ids));
    }

    /**
     * 查询交换空间可用量监控数据
     */
    @PostMapping("/swapSizeFreeEcharts")
    public AjaxResult swapSizeFreeEcharts(@RequestBody InitialMemoryInfo initialMemoryInfo) {
        Map<String, Object> echartsData = initialMemoryInfoService.swapSizeFreeEcharts(initialMemoryInfo);
        return AjaxResult.success(echartsData);
    }

    /**
     * 查询内存利用率监控数据
     */
    @PostMapping("/untilzationEcharts")
    public AjaxResult untilzationEcharts(@RequestBody InitialMemoryInfo initialMemoryInfo) {
        Map<String, Object> echartsData = initialMemoryInfoService.untilzationEcharts(initialMemoryInfo);
        return AjaxResult.success(echartsData);
    }

    /**
     * 查询内存可用量监控数据（KB）
     */
    @PostMapping("/availableMemoryEcharts")
    public AjaxResult availableMemoryEcharts(@RequestBody InitialMemoryInfo initialMemoryInfo) {
        Map<String, Object> echartsData = initialMemoryInfoService.availableMemoryEcharts(initialMemoryInfo);
        return AjaxResult.success(echartsData);
    }

    /**
     * 查询交换空间百分比监控数据
     */
    @PostMapping("/swapSizePercentEcharts")
    public AjaxResult swapSizePercentEcharts(@RequestBody InitialMemoryInfo initialMemoryInfo) {
        Map<String, Object> echartsData = initialMemoryInfoService.swapSizePercentEcharts(initialMemoryInfo);
        return AjaxResult.success(echartsData);
    }

    /**
     * 查询可用内存百分比监控数据
     */
    @PostMapping("/percentEcharts")
    public AjaxResult percentEcharts(@RequestBody InitialMemoryInfo initialMemoryInfo) {
        Map<String, Object> echartsData = initialMemoryInfoService.percentEcharts(initialMemoryInfo);
        return AjaxResult.success(echartsData);
    }

}
