package org.nl.system.controller.logging;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.common.utils.SecurityUtils;
import org.nl.system.service.logging.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author generator
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/api/logs")
@Api(tags = "系统：日志管理")
public class SysLogController {
    @Autowired
    private ISysLogService logService;
    @GetMapping
    @ApiOperation("日志查询")
    //@SaCheckPermission("@el.check()")
    public ResponseEntity<Object> query(@RequestParam Map criteria, PageQuery pageable){
        criteria.put("log_type","INFO");
        return new ResponseEntity<>(TableDataInfo.build(logService.queryAll(criteria,pageable)), HttpStatus.OK);
    }

    @GetMapping(value = "/user")
    // @ApiOperation("用户日志查询")
    public ResponseEntity<Object> queryUserLog(@RequestParam Map criteria, PageQuery pageable){
        criteria.put("log_type","INFO");
        criteria.put("username", SecurityUtils.getCurrentUsername());
        return new ResponseEntity<>(TableDataInfo.build(logService.queryAll(criteria,pageable)), HttpStatus.OK);
    }

    @GetMapping(value = "/error")
    @ApiOperation("错误日志查询")
    // @SaCheckPermission("@el.check()")
    public ResponseEntity<Object> queryErrorLog(@RequestParam Map criteria, PageQuery pageable){
        criteria.put("log_type","ERROR");
        return new ResponseEntity<>(TableDataInfo.build(logService.queryAll(criteria,pageable)), HttpStatus.OK);
    }

    @GetMapping(value = "/error/{id}")
    @ApiOperation("日志异常详情查询")
    // @SaCheckPermission("@el.check()")
    public ResponseEntity<Object> queryErrorLogs(@PathVariable String id){
        return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK);
    }

    @DeleteMapping(value = "/del/error")
    @Log("删除所有ERROR日志")
    @ApiOperation("删除所有ERROR日志")
    // @SaCheckPermission("@el.check()")
    public ResponseEntity<Object> delAllErrorLog(){
        logService.delAllByError();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping(value = "/del/info")
    @Log("删除所有INFO日志")
    @ApiOperation("删除所有INFO日志")
    //  @SaCheckPermission("@el.check()")
    public ResponseEntity<Object> delAllInfoLog(){
        logService.delAllByInfo();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

