package com.njcn.system.controller;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.constant.ServerInfo;
import com.njcn.common.pojo.dto.LogInfoDTO;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.system.pojo.po.UserLog;
import com.njcn.system.service.IUserLogService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.param.BaseParam;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
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.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 前端控制器（行政区域）
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/userLog")
@Api(tags = "用户日志管理")
@AllArgsConstructor
public class UserLogController extends BaseController {

    private final IUserLogService userLogService;

    /**
     * 插入审计日志
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("插入审计日志")
    @ApiImplicitParam(name = "logInfoDTO", value = "插入日志参数", required = true)
    public HttpResult<Object> addUserLog(@RequestBody LogInfoDTO logInfoDTO) {
        String methodDescribe = getMethodDescribe("addUserLog");
        userLogService.addUserLog(logInfoDTO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY)
    @PostMapping("/query")
    @ApiOperation("查询审计日志(治理模块)")
    @ApiImplicitParam(name = "baseParam", value = "查询日志参数", required = true)
    public HttpResult<List<UserLog>> queryUserLog(@RequestBody BaseParam baseParam) {
        String methodDescribe = getMethodDescribe("queryUserLog");
        List<UserLog> list = userLogService.lambdaQuery().eq(UserLog::getLoginName, RequestUtil.getUsername()).
                in(UserLog::getServiceName, Stream.of(ServerInfo.CS_DEVICE_BOOT,
                        ServerInfo.CS_HARMONIC_BOOT,
                        ServerInfo.CS_REPORT_BOOT,
                        ServerInfo.CS_EVENT_BOOT,
                        ServerInfo.CS_WARN_BOOT,
                        ServerInfo.CS_SYSTEM_BOOT).collect(Collectors.toList())).between(UserLog::getCreateTime,baseParam.getSearchBeginTime(),baseParam.getSearchEndTime())
        .orderByDesc(UserLog::getCreateTime).last("limit 100").list();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }
}

