package com.ff.weijinxiao.controller;


import com.ff.weijinxiao.domain.SystemLog;
import com.ff.weijinxiao.service.SystemLogService;
import com.ff.weijinxiao.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
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.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/systemlog")
@Api(value = "系统日志controller", tags = {"系统日志接口"})
public class SystemLogController {

    @Resource
    private SystemLogService systemLogService;

    @GetMapping("/list")
    @ApiOperation(value = "搜索日志", notes = "搜索日志")
    public Map<String, Object> searchLogs(@ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String beginTime,
                                          @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime,
                                          @ApiParam(required = false, value = "登录账号") @RequestParam(required = false) String account) {

        Map<String, Object> result = new HashMap<>();

        List<SystemLog> list = systemLogService.searchSystemLogsByTag(beginTime, endTime, account);
        if (null != list && list.size() > 0) {

            result.put("data", list);
            result.put("total", Integer.valueOf(list.size()));//总记录数
            result.put("code", 10000);
            result.put("message", "搜索日志完成");
        } else {
            result.put("code", 10001);
            result.put("message", "没有日志数据");
        }

        return result;
    }


    @GetMapping("/listpage")
    @ApiOperation(value = "搜索日志分页", notes = "搜索日志分页")
    public Map<String, Object> searchLogsPage(@ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String beginTime,
                                              @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime,
                                              @ApiParam(required = false, value = "登录账号") @RequestParam(required = false) String account,
                                              @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                              @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        if (null != beginTime && beginTime.length() > 0) {
            param.put("GT_create_date", beginTime);
        }
        if (null != endTime && endTime.length() > 0) {
            param.put("LT_create_date", endTime);
        }
        if (null != account && account.length() > 0) {
            param.put("LIKE_logAccount", account);
        }
        Page<SystemLog> list = systemLogService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            result.put("data", list);
            result.put("message", "分页查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }
        return result;
    }
}
