package com.cm.common.plugin.controller.apis.systemlogger;

import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.*;
import com.cm.common.plugin.pojo.dtos.systemlogger.SystemLoggerDTO;
import com.cm.common.plugin.pojo.vos.systemlogger.SystemLoggerVO;
import com.cm.common.plugin.service.systemlogger.ISystemLoggerService;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.UUIDUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: SystemLoggerController
 * @Description: 系统日志
 * @Author: admin
 * @Date: 2019-05-28 17:42:45
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "系统日志管理接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/systemlogger")
public class SystemLoggerController extends AbstractController {

    @Autowired
    private ISystemLoggerService systemLoggerService;

    @ApiOperation(value = "系统日志新增", notes = "系统日志新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savesystemlogger")
    public SuccessResult saveSystemLogger(@RequestBody SystemLoggerVO systemLoggerVO) throws ParamsException, SaveException, SearchException {
        Map<String, Object> params = getParams();
        params.put("systemLoggerId", UUIDUtil.getUUID());
        checkParams(systemLoggerVO, params);
        return systemLoggerService.saveSystemLogger(params);
    }

    @ApiOperation(value = "系统日志删除", notes = "通过id列表批量删除系统日志接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "系统日志ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removesystemlogger/{ids}")
    public SuccessResult removeSystemLogger(@PathVariable("ids") String ids) throws RemoveException {
        Map<String, Object> params = getParams();
        params.put("systemLoggerIds", ids);
        return systemLoggerService.removeSystemLogger(params);
    }

    @ApiOperation(value = "系统日志修改", notes = "系统日志修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemLoggerId", value = "系统日志ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatesystemlogger/{systemLoggerId}")
    public SuccessResult updateSystemLogger(@PathVariable("systemLoggerId") String systemLoggerId, @RequestBody SystemLoggerVO systemLoggerVO) throws ParamsException, UpdateException {
        Map<String, Object> params = getParams();
        params.put("systemLoggerId", systemLoggerId);
        checkParams(systemLoggerVO, params);
        return systemLoggerService.updateSystemLogger(params);
    }

    /**
     * 校验参数
     *
     * @param systemLoggerVO
     * @param params
     * @throws ParamsException
     */
    private void checkParams(SystemLoggerVO systemLoggerVO, Map<String, Object> params) throws ParamsException {
        if (StringUtils.isBlank(systemLoggerVO.getHost())) {
            throw new ParamsException("主机不能为空");
        }
        params.put("host", systemLoggerVO.getHost());

        if (systemLoggerVO.getPort() == null) {
            throw new ParamsException("端口不能为空或者格式错误，格式为数字");
        }
        params.put("port", systemLoggerVO.getPort());

        if (StringUtils.isBlank(systemLoggerVO.getContextPath())) {
            throw new ParamsException("访问路径不能为空");
        }
        params.put("contextPath", systemLoggerVO.getContextPath());

        if (StringUtils.isBlank(systemLoggerVO.getUri())) {
            throw new ParamsException("接口不能为空");
        }
        params.put("uri", systemLoggerVO.getUri());

        if (StringUtils.isBlank(systemLoggerVO.getClassNameFull())) {
            throw new ParamsException("完整路径不能为空");
        }
        params.put("classNameFull", systemLoggerVO.getClassNameFull());

        if (StringUtils.isBlank(systemLoggerVO.getClassName())) {
            throw new ParamsException("类名不能为空");
        }
        params.put("className", systemLoggerVO.getClassName());

        if (StringUtils.isBlank(systemLoggerVO.getMethodName())) {
            throw new ParamsException("方法名不能为空");
        }
        params.put("methodName", systemLoggerVO.getMethodName());

        if (StringUtils.isBlank(systemLoggerVO.getParameterValues())) {
            throw new ParamsException("参数名称不能为空");
        }
        params.put("parameterValues", systemLoggerVO.getParameterValues());

        if (systemLoggerVO.getStartTime() == null) {
            throw new ParamsException("开始时间不能为空或者格式错误，格式为数字");
        }
        params.put("startTime", systemLoggerVO.getStartTime());

        if (systemLoggerVO.getEndTime() == null) {
            throw new ParamsException("截止时间不能为空或者格式错误，格式为数字");
        }
        params.put("endTime", systemLoggerVO.getEndTime());

        if (systemLoggerVO.getUsedTime() == null) {
            throw new ParamsException("耗时不能为空或者格式错误，格式为数字");
        }
        params.put("usedTime", systemLoggerVO.getUsedTime());

        if (StringUtils.isBlank(systemLoggerVO.getResponseResult())) {
            throw new ParamsException("响应内容不能为空");
        }
        params.put("responseResult", systemLoggerVO.getResponseResult());

    }

    @ApiOperation(value = "系统日志列表", notes = "系统日志列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listsystemlogger")
    public List<SystemLoggerDTO> listSystemLogger() throws SearchException {
        Map<String, Object> params = getParams();
        return systemLoggerService.listSystemLogger(params);
    }

    @ApiOperation(value = "系统日志详情", notes = "系统日志详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemLoggerId", value = "系统日志ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getsystemlogger/{systemLoggerId}")
    public SystemLoggerDTO getSystemLogger(@PathVariable("systemLoggerId") String systemLoggerId) throws SearchException {
        Map<String, Object> params = getParams();
        params.put("systemLoggerId", systemLoggerId);
        return systemLoggerService.getSystemLogger(params);
    }

    @ApiOperation(value = "分页系统日志列表", notes = "分页系统日志列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagesystemlogger")
    public SuccessResultList<List<SystemLoggerDTO>> listPageSystemLogger(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return systemLoggerService.listPageSystemLogger(page);
    }

}
