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

import cn.hutool.json.JSONUtil;
import com.skr.mdm.api.equipment.service.ClientToFileAccessService;
import com.skr.mdm.bean.vo.content.ClientToFileAccessVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @ProjectName: mdm_new
 * @Package: com.skr.mdm.api.content.controller
 * @ClassName: FileAccessClientController
 * @Author: Tty
 * @Description: 客户端文件获取
 * @Date: 2021/2/24 9:55
 * @Version: 1.0
 */
@RestController
@RequestMapping("fileAccess")
@Api(tags = "客户端文件获取")
public class ClientToFileAccessController {

	@Autowired
	private ClientToFileAccessService clientToFileAccessService;

	@RequestMapping(value = "getFileAccess", method = RequestMethod.POST)
	@ApiResponses(
			@ApiResponse(code = 200, message = "ok", response = ClientToFileAccessVO.class)
	)
	@ApiOperation("文件层级指令-获取")
	public ResponseResult getFileAccess(@ApiParam(value = "设备id") Integer deviceId,
										@ApiParam(value = "路径信息") String filePath,
										@ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return clientToFileAccessService.getFileAccess(admin, deviceId, filePath);
	}

	@PostMapping(value = "cmdUploadFile")
	@ApiOperation("文件上传指令-获取")
	public ResponseResult uploadFile(@ApiParam(value = "设备id") Integer deviceId,
									 @ApiParam(value = "路径信息") @RequestParam String filePath,
									 @ApiIgnore Authentication authentication) throws Exception {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		List<String> filePathList = JSONUtil.toList(filePath, String.class);
		if (CollectionUtils.isNotEmpty(filePathList)) {
			return clientToFileAccessService.uploadFile(admin, deviceId, filePathList);
		} else {
			return ResponseResult.failure();
		}
	}
}