package com.app.controller.platform;

import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.platform.FrontErrorDto;
import com.app.dto.platform.OperationLogDto;
import com.app.dto.platform.SearchLogDto;
import com.app.service.platform.monitor.OperationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 日志（包括操作、登录、异常日志）

 */
@Api(tags = "日志")
@Slf4j
@RestController("LogController")
@RequestMapping("/platform/log")
public class LogController {

    @Resource
    private OperationLogService operationLogService;

    /**
     * 查询【日志】列表
     *
     */
    @ApiOperation(value = "查询[日志]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<OperationLogDto>>> page(@Valid @RequestBody SearchLogDto dto) {
        return new ResponseEntity(new Result<>().ok(operationLogService.page(dto)), HttpStatus.OK);
    }

    /**
     * 删除所有日志
     */
    @ApiOperation(value = "删除所有日志")
    @DeleteMapping("/delAll/{type}")
    public ResponseEntity<Result> delAllLogs(@PathVariable("type") String type) {
        operationLogService.delAllLogs(type);
        return new ResponseEntity(new Result<>().ok(), HttpStatus.OK);
    }

    /**
     * 接收前端错误日志
     */
    @ApiOperation(value = "接收前端错误日志")
    @PostMapping("/reportForntError")
    public ResponseEntity<Result> delAllLogs(@Valid @RequestBody FrontErrorDto dto, HttpServletRequest request) {
        operationLogService.insertFrontErrorLog(request,dto);
        return new ResponseEntity(new Result<>().ok(), HttpStatus.OK);
    }
}
