package com.lutao.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.admin.api.dto.SysLogDTO;
import com.lutao.cloud.admin.api.dto.SysLogsDTO;
import com.lutao.cloud.admin.api.entity.SysLog;
import com.lutao.cloud.admin.api.vo.PreLogVO;
import com.lutao.cloud.admin.service.SysLogService;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *

 * @since 2017-11-20
 */
@Api(value = "/log", tags ={"日志管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/log")
public class SysLogController{

	private final SysLogService sysLogService;

	/**
	 * 简单分页查询
	 * @param page 分页对象
	 * @param sysLog 系统日志
	 * @return
	 */
	@ApiOperation(value = "简单分页查询", notes = "简单分页查询", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage<SysLog>> getLogPage(Page page, SysLogsDTO sysLog){
		return MessageInfo.ok(sysLogService.getLogByPage(page, sysLog));
	}

	/**
	 * 删除日志
	 * @param id ID
	 * @return success/false
	 */
	@ApiOperation(value = "删除日志", notes = "删除日志", httpMethod = "DELETE")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_log_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Long id){
		return MessageInfo.ok(sysLogService.removeById(id));
	}

	/**
	 * 插入日志
	 * @param sysLog 日志实体
	 * @return success/false
	 */
	@ApiOperation(value = "插入日志", notes = "插入日志", httpMethod = "POST")
	@Inner
	@PostMapping("/save")
	public MessageInfo<Boolean> save(@Valid @RequestBody SysLogDTO sysLog){
		return MessageInfo.ok(sysLogService.saveLog(sysLog));
	}

	/**
	 * 批量插入前端异常日志
	 * @param preLogVoList 日志实体
	 * @return success/false
	 */
	@ApiOperation(value = "批量插入前端异常日志", notes = "批量插入前端异常日志", httpMethod = "POST")
	@PostMapping("/logs")
	public MessageInfo<Boolean> saveBatchLogs(@RequestBody List<PreLogVO> preLogVoList){
		return MessageInfo.ok(sysLogService.saveBatchLogs(preLogVoList));
	}

	@ApiOperation(value = "日志详情", notes = "日志详情", httpMethod = "GET")
	@GetMapping("/info/{logId}")
	public MessageInfo<SysLog> getLogInfo(@PathVariable("logId") Integer logId){
		return MessageInfo.ok(sysLogService.getById(logId));
	}

}
