package com.taais.system.controller.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.core.BaseController;
import com.taais.system.domain.bo.SysOperLogBo;
import com.taais.system.domain.vo.SysOperLogVo;
import com.taais.system.service.ISysOperLogService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

    /**
     * 获取操作日志记录列表
     */
    @SaCheckPermission("monitor:operlog:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysOperLogVo>> list(SysOperLogBo operLogBo) {
        return CommonResult.success(operLogService.selectPage(operLogBo));
    }

    /**
     * 导出操作日志记录列表
     */
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogBo operLog) {
        List<SysOperLogVo> list = operLogService.selectOperLogList(operLog);
        ExcelUtil.exportExcel(list, "操作日志", SysOperLogVo.class, response);
    }

    /**
     * 根据编号获取详细信息
     *
     * @param operId 日志id
     */
    @SaCheckPermission("monitor:operlog:list")
    @GetMapping("/{operId}")
    public CommonResult<SysOperLogVo> getInfo(@PathVariable Long operId) {
        return CommonResult.success(operLogService.selectOperLogById(operId));
    }

    /**
     * 批量删除操作日志记录
     *
     * @param operIds 日志ids
     */
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/{operIds}")
    public CommonResult<Void> remove(@PathVariable Long[] operIds) {
        boolean deleted = operLogService.deleteOperLogByIds(operIds);
        if (!deleted) {
            CommonResult.fail("删除操作日志记录失败!");
        }
        return CommonResult.success();
    }


    /**
     * 清理操作日志记录
     */
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @SaCheckPermission("monitor:operlog:remove")
    @DeleteMapping("/clean")
    public CommonResult<Void> clean() {
        boolean cleaned = operLogService.cleanOperLog();
        if (!cleaned) {
            CommonResult.fail("清除操作日志记录失败!");
        }
        return CommonResult.success();
    }
}
