package com.njcn.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.system.pojo.param.AuditParam;
import com.njcn.system.pojo.vo.AuditLogCusVO;
import com.njcn.system.pojo.vo.AuditLogVO;
import com.njcn.system.pojo.vo.LogParamVO;
import com.njcn.system.pojo.vo.OnlineUsersVO;
import com.njcn.system.service.AuditService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date 2022/07/11 19:00
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/audit")
@Api(tags = "审计管理")
@AllArgsConstructor
public class AuditController extends BaseController {

    private final AuditService auditService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getAuditLog")
    @ApiOperation("审计日志列表")
    @ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
    public HttpResult<Page<AuditLogVO>> getAuditLog(@RequestBody @Validated AuditParam auditParam, HttpServletRequest request){
        String methodDescribe = getMethodDescribe("getAuditLog");
        Page<AuditLogVO> result = auditService.getAuditLog(auditParam);
        auditService.saveAuditLog(request,auditParam,methodDescribe);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/logFileWriter")
    @ApiOperation("日志文件备份")
    public HttpResult<Object> logFileWriter(){
        String methodDescribe = getMethodDescribe("logFileWriter");
        auditService.logFileWriter();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/recoverLogFile")
    @ApiOperation("日志文件恢复")
    public HttpResult<Object> recoverLogFile() {
        String methodDescribe = getMethodDescribe("recoverLogFile");
        auditService.recoverLogFile();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/censusAuditLog")
    @ApiOperation("审计日志统计")
    @ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
    public HttpResult<Page<AuditLogCusVO>> censusAuditLog(@RequestBody @Validated AuditParam auditParam){
        String methodDescribe = getMethodDescribe("censusAuditLog");
        Page<AuditLogCusVO> result = auditService.censusAuditLog(auditParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/clearHistoryLog")
    @ApiOperation("清空历史日志")
    public HttpResult<Object> clearHistoryLog() {
        String methodDescribe = getMethodDescribe("clearHistoryLog");
        auditService.clearHistoryLog();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 这个接口有毒，千万被调用
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/saveLogParam")
    @ApiOperation("筛选下拉列表参数")
    @Deprecated
    public HttpResult<LogParamVO> saveLogParam() {
        String methodDescribe = getMethodDescribe("saveLogParam");
        LogParamVO result = auditService.saveLogParam();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getMemoInfo")
    @ApiOperation("获取表空间大小")
    public HttpResult<Float> getMemoInfo() {
        String methodDescribe = getMethodDescribe("getMemoInfo");
        Float memoInfo = auditService.getMemoInfo();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, memoInfo, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getOnlineUsers")
    @ApiOperation("获取在线用户")
    public HttpResult<List<OnlineUsersVO>> getOnlineUsers() throws ParseException {
        String methodDescribe = getMethodDescribe("getOnlineUsers");
        List<OnlineUsersVO> onlineUsers = auditService.getOnlineUsers();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, onlineUsers, methodDescribe);
    }
}
