package com.securitycontrol.system.controller;

import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.utils.WordUtils;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.system.SystemGlobal;
import com.securitycontrol.system.api.domain.SysLog;
import com.securitycontrol.system.service.ExportSqlService;
import com.securitycontrol.system.service.ISysLogService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author：cwchen
 * @date：2024-02-28-14:01
 * @version：1.0
 * @description：系统日志
 */
@RestController
@RequestMapping("/sys/sysLog/")
@Slf4j
public class SysLogController extends BaseController {

    @Resource(name = "ISysLogService")
    private ISysLogService service;
    /**
     * 存储的路径
     */
    @Value("${sql.filePath}")
    private String filePath;



    @ApiOperation(value = "保存系统日志")
    @PostMapping("saveLogs")
    public AjaxResult saveLogs(@RequestBody SysLog sysLog) {
        return service.saveLogs(sysLog);
    }

    @PostMapping("addLogs")
    public void addLogs(@RequestBody SysLog sysLog,HttpServletRequest request) {
          service.addLogs(sysLog,request);
    }


    @Autowired
    private ExportSqlService exportSqlService;


    @ApiOperation(value = "查询系统日志")
    @GetMapping("getSystemLogs")
    @Log(title = "审计日志", menu = "审计日志->系统日志", grade = OperationType.QUERY_BUSINESS, details = "查询系统日志列表", type = "系统日志")
    public TableDataInfo getSystemLogs(SysLog dto) {
        try{
            dto.setLogType(0);
            startPage();
            List<SysLog> list = service.getSystemLogs(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }


    @ApiOperation(value = "查询业务日志")
    @GetMapping("getYwLogs")
    @Log(title = "审计日志", menu = "审计日志->业务日志", grade = OperationType.QUERY_BUSINESS, details = "查询业务日志列表", type = "系统日志")
    public TableDataInfo getYwLogs(SysLog dto) {
        try{
            dto.setLogType(1);
            startPage();
            List<SysLog> list = service.getSystemLogs(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }


    @ApiOperation(value = "查询异常日志")
    @GetMapping("getErrLogs")
    @Log(title = "审计日志", menu = "审计日志->异常日志", grade = OperationType.QUERY_BUSINESS, details = "查询系统异常日志", type = "系统日志")
    public TableDataInfo getErrLogs(SysLog dto) {
        try{
            dto.setLogType(2);
            startPage();
            List<SysLog> list = service.getSystemLogs(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }
    @ApiOperation(value = "日志备份")
    @GetMapping("downloadErrLogs")
    @Log(title = "审计日志", menu = "审计日志->异常日志", grade = OperationType.COPY_LOG, details = "异常日志备份", type = "系统日志")
    public void downloadErrLogs(HttpServletRequest request, HttpServletResponse response) {
        try {
            String dateTimeNow=exportSqlService.export("2");
            String path=filePath + ExportSqlService.TABLE_NAME + dateTimeNow+ SystemGlobal.TEXT_FIX;
            WordUtils.fileDown(path, "sys_log"+ dateTimeNow+".sql",response);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

    @ApiOperation(value = "日志备份")
    @GetMapping("downloadYwLogs")
    @Log(title = "审计日志", menu = "审计日志-->业务日志", grade = OperationType.COPY_LOG, details = "业务日志备份", type = "系统日志")
    public void downloadYwLogs(HttpServletRequest request, HttpServletResponse response) {
        try {
            String dateTimeNow=exportSqlService.export("1");
            String path=filePath + ExportSqlService.TABLE_NAME + dateTimeNow+ SystemGlobal.TEXT_FIX;
            WordUtils.fileDown(path, "sys_log"+ dateTimeNow+".sql",response);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

    @ApiOperation(value = "日志备份")
    @GetMapping("downloadSysLogs")
    @Log(title = "审计日志", menu = "审计日志->系统日志", grade = OperationType.COPY_LOG, details = "系统日志备份", type = "系统日志")
    public void downloadSysLogs(HttpServletRequest request, HttpServletResponse response) {
        try {
            String dateTimeNow=exportSqlService.export("0");
            String path=filePath + ExportSqlService.TABLE_NAME + dateTimeNow+ SystemGlobal.TEXT_FIX;
            WordUtils.fileDown(path, "sys_log"+ dateTimeNow+".sql",response);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }


    @ApiOperation(value = "查询日志容量")
    @PostMapping("getLogsSet")
    @Log(title = "审计日志", menu = "审计日志->日志容量配置", grade = OperationType.QUERY_BUSINESS, details = "查询日志容量", type = "系统日志")
    public Result<String> getLogsSet() {
            return  service.getLogsSet();
    }


    @ApiOperation(value = "设置日志容量")
    @PostMapping("setLogsSet")
    @Log(title = "审计日志", menu = "审计日志->日志容量配置", grade = OperationType.QUERY_BUSINESS, details = "修改日志容量", type = "系统日志")
    public Result<String> setLogsSet(@RequestBody String capacity) {
        return  service.setLogsSet(capacity);
    }


    @ApiOperation(value = "查询日志统计分析")
    @PostMapping("getLogStatistics")
    @Log(title = "审计日志", menu = "审计日志->日志分析", grade = OperationType.QUERY_BUSINESS, details = "查询日志分析", type = "系统日志")
    public Result<Map<String,Object>> getLogStatistics(@RequestBody SysLog dto) {
        return  service.getLogStatistics(dto);
    }
    @ApiOperation(value = "查询日志告警")
    @PostMapping("logWarn")
    public Result<Map<String,Object>> logWarn() {
        return  service.logWarn();
    }


}
