package com.diagbot.web;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.SysLog;
import com.diagbot.facade.SysLogFacade;
import com.diagbot.vo.SysLogVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description: 日志操作控制层
 * @author: gaodm
 * @time: 2018/8/30 10:12
 */
@RestController
@RequestMapping("/log")
public class SysLogController {

    @Autowired
    private SysLogFacade sysLogFacade;

    /**
     * 新增日志信息
     *
     * @param sysLogVo 新增日志输入参数
     * @return 新增日志是否成功
     */
    @ApiOperation(value = "添加日志", notes = "添加日志")
    @PostMapping("/add")
    @SysLogger("postLog")
    public RespDTO add(@RequestBody SysLogVo sysLogVo){
        //初始化新增日志信息
        SysLog sysLog = new SysLog();
        sysLog.setGmtCreate(new Date());
        sysLog.setIp(sysLogVo.getIp());
        sysLog.setMethod(sysLogVo.getMethod());
        sysLog.setOperation(sysLogVo.getOperation());
        sysLog.setParams(sysLogVo.getParams());
        return RespDTO.onSuc(sysLogFacade.save(sysLog)?"添加成功":"添加失败");
    }

    /**
     * 删除日志
     * @param id 日志信息ID
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除日志", notes = "删除日志")
    @DeleteMapping("/delete/{id}")
    @SysLogger("deleteLog")
    public RespDTO delete(@PathVariable(value = "id") Integer id){
        return RespDTO.onSuc(sysLogFacade.removeById(id)?"删除成功":"删除失败");
    }

    /**
     * 修改日志
     * @param sysLog 修改日志输入参数
     * @return 修改是否成功
     */
    @ApiOperation(value = "修改日志", notes = "修改日志")
    @PostMapping("/update")
    @SysLogger("updateLog")
    public RespDTO update(@RequestBody SysLog sysLog){
        return RespDTO.onSuc(sysLogFacade.updateById(sysLog)?"修改成功":"修改失败");
    }

    /**
     * 获取日志列表
     * @return 日志列信息
     */
    @ApiOperation(value = "获取日志列表", notes = "获取日志列表")
    @GetMapping("/list")
    @SysLogger("listLog")
    public RespDTO list(){
        Wrapper<SysLog> wrapper = new QueryWrapper<>();
        return RespDTO.onSuc(sysLogFacade.list(wrapper));
    }

    /**
     * 获取日志翻页信息
     * @return 日志翻页信息
     */
    @ApiOperation(value = "获取日志翻页信息", notes = "获取日志翻页信息")
    @GetMapping("/page")
    @SysLogger("pageLog")
    public RespDTO page() {
        //初始化日志翻页参数
        IPage<SysLog> wrapper = new Page<>();
        return RespDTO.onSuc(sysLogFacade.page(wrapper, null));
    }
}

