package com.yd.csf.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yd.common.result.Result;
import com.yd.csf.api.dto.AuditLogQueryRequest;
import com.yd.csf.service.model.SysOperDataChange;
import com.yd.csf.service.model.SysOperDataSnapshot;
import com.yd.csf.service.model.SysOperLog;
import com.yd.csf.service.service.AuditLogDataChangeService;
import com.yd.csf.service.service.AuditLogDataSnapshotService;
import com.yd.csf.service.service.AuditLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/auditlog")
@Tag(name = "审计日志")
@RequiredArgsConstructor
public class AuditLogController {

    @Resource
    private AuditLogService operLogService;
    @Resource
    private AuditLogDataSnapshotService snapshotService;
    @Resource
    private AuditLogDataChangeService changeService;

    @PostMapping("/log/page")
    @Operation(summary = "操作日志分页查询")
    public Result<Page<SysOperLog>> page(@RequestBody AuditLogQueryRequest request) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<SysOperLog>()
                .eq(StringUtils.isNotBlank(request.getModule()), SysOperLog::getModule, request.getModule())
                .eq(StringUtils.isNotBlank(request.getOperName()), SysOperLog::getOperName, request.getOperName())
                .eq(request.getStatus() != null, SysOperLog::getStatus, request.getStatus())
                .ge(StringUtils.isNotBlank(request.getStartTime()), SysOperLog::getOperTime, request.getStartTime())
                .le(StringUtils.isNotBlank(request.getEndTime()), SysOperLog::getOperTime, request.getEndTime())
                .orderByDesc(SysOperLog::getOperTime);

        Page<SysOperLog> page = operLogService.page(
                new Page<>(request.getPageNo(), request.getPageSize()), wrapper);
        return Result.success(page);
    }

    @GetMapping("/log/detail")
    @Operation(summary = "审计详情查询")
    public Result<Map<String, Object>> detail(@RequestParam String requestId) {
        if (StringUtils.isBlank(requestId)) {
            return Result.success(null);
        }

        SysOperLog operLog = operLogService.getOne(
                new LambdaQueryWrapper<SysOperLog>().eq(SysOperLog::getRequestId, requestId));

        List<SysOperDataSnapshot> snapshots = snapshotService.list(
                new LambdaQueryWrapper<SysOperDataSnapshot>().eq(SysOperDataSnapshot::getRequestId, requestId));

        List<SysOperDataChange> changes = changeService.list(
                new LambdaQueryWrapper<SysOperDataChange>().eq(SysOperDataChange::getRequestId, requestId));

        Map<String, Object> result = new HashMap<>();
        result.put("operLog", operLog);
        result.put("snapshots", snapshots);
        result.put("changes", changes);
        return Result.success(result);
    }
}
