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

import com.skr.mdm.api.equipment.service.DebugLogService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.qo.controller.ClientDebugLogQO;
import com.skr.mdm.bean.vo.equipment.ClientDebugLogVO;
import io.swagger.annotations.*;
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.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;


/**
 * @Author: Tty
 * @Description: 调试日志
 * @Date: 2022/2/17 18:59
 * @Version: 1.0
 */
@RestController
@RequestMapping("debugLog")
@Api(tags = "设备调试日志")
@Slf4j
public class DebugLogController {

	@Autowired
	private DebugLogService debugLogService;

	@PostMapping("list")
	@ApiOperation(value = "查询")
	@ApiResponses({
			@ApiResponse(code = 200, message = "成功", response = ClientDebugLogVO.class)
	})
	@PreAuthorize("hasAnyAuthority('sys:equipment:debug:log')")
	public ResponseResult list(@RequestBody ClientDebugLogQO clientDebugLogQO,
											 @ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return debugLogService.queryDebugLogList(admin,clientDebugLogQO);
	}


	@PostMapping(value = "download")
	@ApiOperation(value = "调试日志下载")
	@PreAuthorize("hasAnyAuthority('sys:equipment:debug:log')")
	public void download(HttpServletResponse response,
							 @RequestParam @ApiParam(value = "设备id", required = true) Integer equipmentId,
							 @RequestParam @ApiParam(value = "source == address", required = true) String source,
							 @ApiIgnore Authentication authentication) throws Exception {
		LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
		debugLogService.debugLogDownload(loginAdminDTO, response, equipmentId, source);
	}
}