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

import com.skr.mdm.api.log.service.LogAdminService;
import com.skr.mdm.bean.qo.log.AdminLogPageQO;
import com.skr.mdm.bean.qo.log.AdminLogQO;
import com.skr.mdm.bean.vo.system.LogAdminVO;
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 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:49
 */
@RestController
@RequestMapping("log/admin")
@Api(tags = "管理员日志")
public class LogAdminController {

    @Autowired
    private LogAdminService logAdminService;

    @PostMapping("list")
    @ApiOperation(value = "管理员日志列表", response = LogAdminVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType", value = "查询类型 1:管理员 2:内容 ", dataType = "Integer"),
    })
    @PreAuthorize("hasAnyAuthority('sys:audit:log:admin:list')")
    public ResponseResult queryAdminLogList(AdminLogPageQO adminLogPageQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return logAdminService.listAdminLog(adminLogPageQO, loginAdminDTO);
    }

    @PostMapping("export")
    @ApiOperation("管理员日志导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType", value = "查询类型 1:管理员 2:内容", dataType = "Integer"),
    })
    @PreAuthorize("hasAnyAuthority('sys:audit:log:admin:export')")
    public void exportAdminLog(AdminLogQO adminLogQO,
                               @ApiIgnore HttpServletResponse response,
                               @ApiIgnore Authentication authentication) throws IOException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        logAdminService.exportAdminLog(adminLogQO, response, loginAdminDTO);
    }
}
