package com.ruoyi.credit.controller;

import java.util.Date;
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.credit.domain.ValidationLog;
import com.ruoyi.credit.service.IValidationLogService;

/**
 * 验证日志Controller
 * 
 * @author ruoyi
 * @date 2025-12-31
 */
@RestController
@RequestMapping("/credit/validation-log")
public class ValidationLogController extends BaseController
{
    @Autowired
    private IValidationLogService validationLogService;

    /**
     * 查询验证日志列表
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:list')")
    @GetMapping("/list")
    public TableDataInfo list(ValidationLog validationLog)
    {
        startPage();
        List<ValidationLog> list = validationLogService.selectValidationLogList(validationLog);
        return getDataTable(list);
    }

    /**
     * 导出验证日志列表
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:export')")
    @Log(title = "验证日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ValidationLog validationLog)
    {
        List<ValidationLog> list = validationLogService.selectValidationLogList(validationLog);
        ExcelUtil<ValidationLog> util = new ExcelUtil<ValidationLog>(ValidationLog.class);
        util.exportExcel(response, list, "验证日志数据");
    }

    /**
     * 获取验证日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        return success(validationLogService.selectValidationLogByLogId(logId));
    }

    /**
     * 根据验证记录ID查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/validation/{validationId}")
    public AjaxResult getLogsByValidationId(@PathVariable("validationId") Long validationId)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByValidationId(validationId);
        return success(logs);
    }

    /**
     * 根据贷款ID查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/loan/{loanId}")
    public AjaxResult getLogsByLoanId(@PathVariable("loanId") Long loanId)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByLoanId(loanId);
        return success(logs);
    }

    /**
     * 根据时间范围查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/time-range")
    public AjaxResult getLogsByTimeRange(
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByTimeRange(startTime, endTime);
        return success(logs);
    }

    /**
     * 根据日志级别查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/level/{logLevel}")
    public AjaxResult getLogsByLevel(@PathVariable("logLevel") String logLevel)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByLevel(logLevel);
        return success(logs);
    }

    /**
     * 根据日志类型查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/type/{logType}")
    public AjaxResult getLogsByType(@PathVariable("logType") String logType)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByType(logType);
        return success(logs);
    }

    /**
     * 根据时间范围和日志级别查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/time-range-level")
    public AjaxResult getLogsByTimeRangeAndLevel(
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime,
            @RequestParam("logLevel") String logLevel)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByTimeRangeAndLevel(startTime, endTime, logLevel);
        return success(logs);
    }

    /**
     * 根据时间范围和日志类型查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/time-range-type")
    public AjaxResult getLogsByTimeRangeAndType(
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime,
            @RequestParam("logType") String logType)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByTimeRangeAndType(startTime, endTime, logType);
        return success(logs);
    }

    /**
     * 根据操作用户查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/operator/{operatorUser}")
    public AjaxResult getLogsByOperatorUser(@PathVariable("operatorUser") String operatorUser)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByOperatorUser(operatorUser);
        return success(logs);
    }

    /**
     * 根据执行步骤查询日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/step/{executionStep}")
    public AjaxResult getLogsByExecutionStep(@PathVariable("executionStep") String executionStep)
    {
        List<ValidationLog> logs = validationLogService.selectLogsByExecutionStep(executionStep);
        return success(logs);
    }

    /**
     * 查询异常日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/exceptions")
    public AjaxResult getExceptionLogs()
    {
        List<ValidationLog> logs = validationLogService.selectExceptionLogs();
        return success(logs);
    }

    /**
     * 查询性能警告日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/performance-warnings")
    public AjaxResult getPerformanceWarningLogs(@RequestParam("minDuration") Long minDuration)
    {
        List<ValidationLog> logs = validationLogService.selectPerformanceWarningLogs(minDuration);
        return success(logs);
    }

    /**
     * 统计日志数量
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/count")
    public AjaxResult countValidationLogs(ValidationLog validationLog)
    {
        int count = validationLogService.countValidationLogs(validationLog);
        return success(count);
    }

    /**
     * 统计各级别日志数量
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/count-by-level")
    public AjaxResult countLogsByLevel(
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime)
    {
        List<Map<String, Object>> stats = validationLogService.countLogsByLevel(startTime, endTime);
        return success(stats);
    }

    /**
     * 统计各类型日志数量
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:query')")
    @GetMapping("/count-by-type")
    public AjaxResult countLogsByType(
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime)
    {
        List<Map<String, Object>> stats = validationLogService.countLogsByType(startTime, endTime);
        return success(stats);
    }

    /**
     * 新增验证日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:add')")
    @Log(title = "验证日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ValidationLog validationLog)
    {
        return toAjax(validationLogService.insertValidationLog(validationLog));
    }

    /**
     * 批量新增验证日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:add')")
    @Log(title = "验证日志", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<ValidationLog> validationLogs)
    {
        return toAjax(validationLogService.batchInsertValidationLog(validationLogs));
    }

    /**
     * 修改验证日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:edit')")
    @Log(title = "验证日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ValidationLog validationLog)
    {
        return toAjax(validationLogService.updateValidationLog(validationLog));
    }

    /**
     * 删除验证日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:remove')")
    @Log(title = "验证日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(validationLogService.deleteValidationLogByLogIds(logIds));
    }

    /**
     * 根据验证记录ID删除日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:remove')")
    @Log(title = "验证日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/validation/{validationId}")
    public AjaxResult removeByValidationId(@PathVariable("validationId") Long validationId)
    {
        return toAjax(validationLogService.deleteLogsByValidationId(validationId));
    }

    /**
     * 根据贷款ID删除日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:remove')")
    @Log(title = "验证日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/loan/{loanId}")
    public AjaxResult removeByLoanId(@PathVariable("loanId") Long loanId)
    {
        return toAjax(validationLogService.deleteLogsByLoanId(loanId));
    }

    /**
     * 根据时间范围删除日志
     */
    @PreAuthorize("@ss.hasPermi('credit:validation-log:remove')")
    @Log(title = "验证日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/time-range")
    public AjaxResult removeByTimeRange(@RequestParam("endTime") Date endTime)
    {
        return toAjax(validationLogService.deleteLogsByTimeRange(endTime));
    }
}