package com.njcn.gather.system.log.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
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.LogUtil;
import com.njcn.gather.system.log.pojo.param.SysLogParam;
import com.njcn.gather.system.log.pojo.po.SysLogAudit;
import com.njcn.gather.system.log.service.ISysLogAuditService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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;


/**
 * @author caozehui
 * @date 2024-11-29
 */
@Slf4j
@Api(tags = "日志管理")
@RestController
@RequestMapping("/sysLog")
@RequiredArgsConstructor
public class SysLogController extends BaseController {
    private final ISysLogAuditService sysLogAuditService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation(value = "获取日志列表")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Page<SysLogAudit>> list(@RequestBody @Validated SysLogParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        Page<SysLogAudit> result = sysLogAuditService.listSysLogAudit(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/exportCSV")
    @ApiOperation("日志导出为csv文件")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public void exportCSV(@RequestBody @Validated SysLogParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("export");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        sysLogAuditService.exportCSV(param);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/analyse")
    @ApiOperation("日志分析")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public void analyse(@RequestBody @Validated SysLogParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("analyze");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        sysLogAuditService.analyse(param);
    }
}

