package com.mosty.operation.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.entity.log.SysOperLog;
import com.mosty.common.token.JwtSysUser;
import com.mosty.operation.log.BaseController;
import com.mosty.operation.log.entity.DeleteListVO;
import com.mosty.operation.log.entity.VO.OperlogPage;
import com.mosty.operation.log.service.SysOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@RestController
@AllArgsConstructor
@Api(tags = "操作日志")
@RequestMapping("/monitor/operlog")
public class SysOperlogController {


    private final SysOperLogService operLogService;


    @PostMapping("/list")
    @ApiOperation("查询操作日志列表")
    @Log(title = "查询操作日志详情", businessType = BusinessType.OTHER)
    public ResponseResult<IPage<SysOperLog>> list(@RequestBody OperlogPage operLog) {
        IPage<SysOperLog> sysOperLogPage = operLogService.selectByPage(operLog);
        return ResponseResult.success(sysOperLogPage);
    }


    @Log(title = "删除操作日志", businessType = BusinessType.DELETE)
    @ApiOperation("查询操作日志详情")
    @PostMapping("/remove")
    public ResponseResult<Boolean> remove(@RequestBody DeleteListVO deleteListVO) {
        operLogService.deleteOperLogByIds(deleteListVO.getIds());
        return ResponseResult.success(Boolean.TRUE);
    }

    @ApiOperation("查询操作日志详情")
    @Log(title = "查询操作日志详情", businessType = BusinessType.OTHER)
    @GetMapping("/detail/{id}")
    public ResponseResult<SysOperLog> detail(@PathVariable("id") Long id) {
        SysOperLog log = operLogService.getOne(new LambdaQueryWrapper<SysOperLog>()
                .eq(SysOperLog::getOperId, id));
        return ResponseResult.success(log);
    }

    @Log(title = "清空操作日志", businessType = BusinessType.CLEAN)
    @ApiOperation("清空操作日志")
    @PostMapping("/clean")
    public ResponseResult<Boolean> clean() {
        operLogService.cleanOperLog();
        return ResponseResult.success(Boolean.TRUE);
    }

    @Log(title = "系统使用情况统计数字", businessType = BusinessType.OTHER)
    @ApiOperation("系统使用情况统计数字")
    @JwtSysUser
    @PostMapping("/statistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1.今天 2.7天 3.30天 4.90天", required = true, dataType = "String"),
    })
    public ResponseResult<Map<String,Object>> statistics(String type) {
        return ResponseResult.success(operLogService.statistics(type));
    }

    @Log(title = "部门排名统计TOP10", businessType = BusinessType.OTHER)
    @ApiOperation("部门排名统计TOP10")
    @JwtSysUser
    @PostMapping("/bmStatistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1.今天 2.7天 3.30天 4.90天", required = true, dataType = "String"),
    })
    public ResponseResult<List<Map<String,Object>>> bmStatistics(String type) {
        return ResponseResult.success(operLogService.bmStatistics(type));
    }

    @Log(title = "人员排名统计TOP10", businessType = BusinessType.OTHER)
    @ApiOperation("人员排名统计TOP10")
    @JwtSysUser
    @PostMapping("/ryStatistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1.今天 2.7天 3.30天 4.90天", required = true, dataType = "String"),
    })
    public ResponseResult<List<Map<String,Object>>> ryStatistics(String type) {
        return ResponseResult.success(operLogService.ryStatistics(type));
    }

    @Log(title = "模块排名统计", businessType = BusinessType.OTHER)
    @ApiOperation("模块排名统计")
    @JwtSysUser
    @PostMapping("/mkStatistics")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1.今天 2.7天 3.30天 4.90天", required = true, dataType = "String"),
    })
    public ResponseResult<List<Map<String,Object>>> mkStatistics(String type) {
        return ResponseResult.success(operLogService.mkStatistics(type));
    }
}