package com.elephant.framework.galaxy.aroad.module.system.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.module.system.entity.Log;
import com.elephant.framework.galaxy.aroad.module.system.service.LogService;
import com.elephant.framework.galaxy.aroad.module.system.vo.PreLogVO;
import com.elephant.framework.galaxy.aroad.response.R;
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>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/log")
public class LogController extends BaseController {
    private final LogService logService;

    /**
     * 简单分页查询
     *
     * @param page   分页对象
     * @param log 系统日志
     * @return
     */
    @GetMapping("/page")
    public R getLogPage(Page page, Log log) {
        return new R(logService.page(page, Wrappers.query(log)));
    }

    /**
     * 删除日志
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R removeById(@PathVariable Long id) {
        return new R(logService.removeById(id));
    }

    /**
     * 插入日志
     *
     * @param log 日志实体
     * @return success/false
     */
    @PostMapping("/save")
    public R save(@Valid @RequestBody Log log) {
        return new R(logService.save(log));
    }

    /**
     * 批量插入前端异常日志
     *
     * @param preLogVoList 日志实体
     * @return success/false
     */
    @PostMapping("/logs")
    public R saveBatchLogs(@RequestBody List<PreLogVO> preLogVoList) {
        return new R(logService.saveBatchLogs(preLogVoList));
    }
}

