package com.skr.mdm.api.log.controller;

import com.skr.mdm.api.log.service.LogUserService;
import com.skr.mdm.bean.qo.log.UserLogPageQO;
import com.skr.mdm.bean.qo.log.UserLogQO;
import com.skr.mdm.bean.vo.system.UserLogVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author nfq
 * @date 2023-03-09 09:47
 */
@RestController
@RequestMapping("log/user")
@Api(tags = "用户日志")
@Slf4j
public class LogUserController {

	@Autowired
	private LogUserService logUserService;

	@PostMapping("list")
	@ApiOperation(value = "用户日志列表", response = UserLogVO.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "departmentId", value = "部门id", dataType = "Integer"),
			@ApiImplicitParam(name = "searchType", value = "1:用户名 2:设备 3:内容", dataType = "String"),
	})
	@PreAuthorize("hasAnyAuthority('sys:audit:log:user:list')")
	public ResponseResult listUserLog(UserLogPageQO userLogPageQO, @ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return logUserService.listUserLog(userLogPageQO, admin);
	}

	@PostMapping("export")
	@ApiOperation("用户日志导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "departmentId", value = "部门id", dataType = "Integer"),
			@ApiImplicitParam(name = "searchType", value = "1:用户名 2:设备 3:内容", dataType = "String"),
	})
	@PreAuthorize("hasAnyAuthority('sys:audit:log:user:export')")
	public void exportUserLog(UserLogQO userLogQO, HttpServletResponse response,
							  @ApiIgnore Authentication authentication) throws IOException {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		logUserService.exportUserLog(userLogQO, response, admin);
	}
}
