package com.ruoyi.monitor.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.monitor.domain.AuditLog;
import com.ruoyi.monitor.service.IAuditLogService;

/**
 * 审计日志Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/monitor/auditlog")
public class AuditLogController extends BaseController
{
    @Autowired
    private IAuditLogService auditLogService;

    /**
     * 查询审计日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(AuditLog auditLog)
    {
        startPage();
        List<AuditLog> list = auditLogService.selectAuditLogList(auditLog);
        return getDataTable(list);
    }

    /**
     * 导出审计日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:export')")
    @Log(title = "审计日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AuditLog auditLog)
    {
        List<AuditLog> list = auditLogService.selectAuditLogList(auditLog);
        ExcelUtil<AuditLog> util = new ExcelUtil<AuditLog>(AuditLog.class);
        util.exportExcel(response, list, "审计日志数据");
    }

    /**
     * 获取审计日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        return success(auditLogService.selectAuditLogByLogId(logId));
    }

    /**
     * 新增审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:add')")
    @Log(title = "审计日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuditLog auditLog)
    {
        return toAjax(auditLogService.insertAuditLog(auditLog));
    }

    /**
     * 修改审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:edit')")
    @Log(title = "审计日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuditLog auditLog)
    {
        return toAjax(auditLogService.updateAuditLog(auditLog));
    }

    /**
     * 删除审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:remove')")
    @Log(title = "审计日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(auditLogService.deleteAuditLogByLogIds(logIds));
    }

    /**
     * 根据用户ID查询审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/user/{userId}")
    public AjaxResult getByUserId(@PathVariable("userId") Long userId)
    {
        List<AuditLog> list = auditLogService.selectAuditLogByUserId(userId);
        return success(list);
    }

    /**
     * 根据业务ID和业务类型查询审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/business")
    public AjaxResult getByBusiness(@RequestParam("businessId") String businessId, 
                                   @RequestParam("businessType") String businessType)
    {
        List<AuditLog> list = auditLogService.selectAuditLogByBusiness(businessId, businessType);
        return success(list);
    }

    /**
     * 根据操作类型查询审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/operation/{operationType}")
    public AjaxResult getByOperationType(@PathVariable("operationType") String operationType)
    {
        List<AuditLog> list = auditLogService.selectAuditLogByOperationType(operationType);
        return success(list);
    }

    /**
     * 查询审计日志统计信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics(@RequestParam Map<String, Object> params)
    {
        List<Map<String, Object>> statistics = auditLogService.selectAuditLogStatistics(params);
        return success(statistics);
    }

    /**
     * 根据风险等级查询审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/risk/{riskLevel}")
    public AjaxResult getByRiskLevel(@PathVariable("riskLevel") String riskLevel)
    {
        List<AuditLog> list = auditLogService.selectAuditLogByRiskLevel(riskLevel);
        return success(list);
    }

    /**
     * 查询指定时间范围内的审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/timerange")
    public AjaxResult getByTimeRange(@RequestParam Map<String, Object> params)
    {
        List<AuditLog> list = auditLogService.selectAuditLogByTimeRange(params);
        return success(list);
    }

    /**
     * 清理过期的审计日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:remove')")
    @Log(title = "审计日志清理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean/{retentionDays}")
    public AjaxResult cleanExpiredLogs(@PathVariable("retentionDays") int retentionDays)
    {
        int deletedCount = auditLogService.cleanExpiredAuditLogs(retentionDays);
        return success("成功清理 " + deletedCount + " 条过期审计日志");
    }

    /**
     * 分析异常操作
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/analyze/anomalous")
    public AjaxResult analyzeAnomalousOperations(@RequestParam Map<String, Object> params)
    {
        List<AuditLog> anomalousOps = auditLogService.analyzeAnomalousOperations(params);
        return success(anomalousOps);
    }

    /**
     * 生成审计报告
     */
    @PreAuthorize("@ss.hasPermi('monitor:auditlog:query')")
    @GetMapping("/report")
    public AjaxResult generateAuditReport(@RequestParam Map<String, Object> params)
    {
        Map<String, Object> report = auditLogService.generateAuditReport(params);
        return success(report);
    }
}