package org.springblade.system.monitor;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springblade.common.annotation.Log;
import org.springblade.common.annotation.RequiresPermissions;
import org.springblade.common.core.controller.BaseController;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.common.core.domain.entity.SysOperLog;
import org.springblade.common.core.page.TableDataInfo;
import org.springblade.common.enums.BusinessType;
import org.springblade.common.utils.poi.ExcelUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.system.service.ISysOperLogService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author bonus
 */
@RestController
@RequestMapping("/monitor/operlog")
@NonDS
public class SysOperlogController extends BaseController {
    @Resource
    private ISysOperLogService operLogService;

    @RequiresPermissions("monitor:operlog:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysOperLog operLog) {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("monitor:operlog:remove")
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Long[] operIds) {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @RequiresPermissions("monitor:operlog:remove")
    @PostMapping("/clean")
    public AjaxResult clean() {
        operLogService.cleanOperLog();
        return success();
    }
}
