package org.king.modules.system.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.king.common.api.vo.Result;
import org.king.common.system.base.controller.KingController;
import org.king.common.system.query.QueryGenerator;
import org.king.modules.system.entity.SysLog;
import org.king.modules.system.entity.SysRole;
import org.king.modules.system.service.ISysLogService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: 系统日志表
 * @Author: king-boot
 * @Date: 2018-12-26
 * @Version: V1.0
 */
@RestController
@RequestMapping("/sys/log")
@Slf4j
@Api(tags = "系统日志管理")
public class SysLogController extends KingController<SysLog, ISysLogService> {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * @param syslog
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     * @功能：查询日志记录
     */
    @GetMapping(value = "/list")
    @ApiOperation("系统日志--分页列表查询")
    public Result<?> queryPageList(SysLog syslog, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<SysLog> queryWrapper = QueryGenerator.initQueryWrapper(syslog, req.getParameterMap());
        Page<SysLog> page = new Page<>(pageNo, pageSize);
        //日志关键词
        String keyWord = req.getParameter("keyWord");
        if (StrUtil.isNotBlank(keyWord)) {
            queryWrapper.like("log_content", keyWord);
        }
        //TODO 过滤逻辑处理
        //TODO begin、end逻辑处理
        //TODO 一个强大的功能，前端传一个字段字符串，后台只返回这些字符串对应的字段
        //创建时间/创建人的赋值
        IPage<SysLog> pageList = sysLogService.page(page, queryWrapper);
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        return Result.ok(pageList);
    }

    /**
     * @param id
     * @return
     * @功能：删除单个日志记录
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("系统日志--删除单个日志记录")
    public Result<SysLog> delete(@RequestParam(name = "id") String id) {
        Result<SysLog> result = new Result<>();
        SysLog sysLog = sysLogService.getById(id);
        if (sysLog == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = sysLogService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * @param ids
     * @return
     * @功能：批量，全部清空日志记录
     */
    @DeleteMapping(value = "/deleteBatch")
    @ApiOperation("系统日志--批量，全部清空日志记录")
    public Result<SysRole> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<SysRole> result = new Result<>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            if ("allclear".equals(ids)) {
                this.sysLogService.removeAll();
                result.success("清除成功!");
            }
            this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }


}
