package com.bonus.system.controller;

import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.core.utils.global.SystemGlobal;
import com.bonus.system.api.domain.SysLogsVo;
import com.bonus.system.service.ExportSqlService;
import com.bonus.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;


    @Autowired
    private ExportSqlService exportSqlService;
//    /**
//     * 存储的路径
//     */
//    @Value("${sql.filePath}")
//    private String filePath;

    /**
     * 保存系统日志，业务日志和其他日常日志
     * @param sysLog 日志对象
     */
    @ApiOperation(value = "保存系统日志")
    @PostMapping("saveLogs")
    public AjaxResult saveLogs(@RequestBody SysLogsVo sysLog) {
        return service.saveLogs(sysLog);
    }

    /**
     * 保存越权日志
     * @param sysLog
     * @param request
     */
    @PostMapping("addLogs")
    public void addLogs(@RequestBody SysLogsVo sysLog,HttpServletRequest request) {
          service.saveLogs(sysLog,request);
    }

//    @Autowired
//    private ExportSqlService exportSqlService;

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

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

    @ApiOperation(value = "查询异常日志")
    @GetMapping("getErrLogs")
    @SysLog(title = "审计日志", module = "审计日志->异常日志", businessType = OperaType.QUERY ,logType = 0,details = "查询系统异常日志", type = "系统日志")
    public TableDataInfo getErrLogs(SysLogsVo dto) {
        try{
            dto.setLogType(2);
            startPage();
            List<SysLogsVo> list = service.getSystemLogs(dto);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBad(new ArrayList<>(),"请求出错了");
        }
    }
    @ApiOperation(value = "日志备份")
    @GetMapping("downloadErrLogs")
    @SysLog(title = "审计日志", module = "审计日志->异常日志", businessType = OperaType.COPY_LOG, logType = 0, 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;
            FileUtils.fileDownload(path, "sys_log"+ dateTimeNow+".sql",response);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

    @ApiOperation(value = "日志备份")
    @GetMapping("downloadYwLogs")
    @SysLog(title = "审计日志", module = "审计日志-->业务日志", businessType = OperaType.COPY_LOG,logType = 0, 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;
            FileUtils.fileDownload(path, "sys_log"+ dateTimeNow+".sql",response);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

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


    @ApiOperation(value = "查询日志容量")
    @PostMapping("getLogsSet")
    @SysLog(title = "审计日志", module = "审计日志->日志容量配置", businessType = OperaType.QUERY, details = "查询日志容量", type = "系统日志",logType = 0)
    public R<String> getLogsSet() {
            return  service.getLogsSet();
    }


    @ApiOperation(value = "设置日志容量")
    @PostMapping("setLogsSet")
    @SysLog(title = "审计日志", module = "审计日志->日志容量配置", businessType = OperaType.QUERY, details = "修改日志容量", type = "系统日志",logType = 0)
    public R<String> setLogsSet(@RequestBody SysLogsVo dto) {

        return  service.setLogsSet(dto.getCapacity());
    }


    @ApiOperation(value = "查询日志统计分析")
    @PostMapping("getLogStatistics")
    @SysLog(title = "审计日志", module = "审计日志->日志分析", businessType = OperaType.QUERY, details = "查询日志分析", type = "系统日志",logType = 0)
    public R<Map<String,Object>> getLogStatistics(@RequestBody SysLogsVo dto) {
        return  service.getLogStatistics(dto);
    }

    @ApiOperation(value = "查询日志告警")
    @GetMapping("logWarn")
    public AjaxResult logWarn() {
        service.handleWarningLog();
        return success();
    }


}
