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

import com.skr.mdm.api.system.service.SysChangeCertificateService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.qo.controller.QueryObjectQO;
import com.skr.mdm.bean.vo.system.SysChangeCertificateUserVO;
import com.skr.mdm.bean.vo.system.SysChangeCertificateVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @Author: Tty
 * @Description:
 * @Date: 2020/11/30 17:25
 * @Version: 1.0
 */

@Slf4j
@RestController
@RequestMapping("sysChange")
@Api(tags = "服务器变更")
public class SysChangeController {

	@Autowired
	private SysChangeCertificateService changeCertificateService;

	/**
	 * 查询服务器证书记录
	 *
	 * @param authentication
	 * @return
	 */
	@PostMapping("getCertificate")
	@ApiResponses(
			@ApiResponse(code = 200, message = "ok", response = SysChangeCertificateVO.class)
	)
	@ApiOperation("查询服务器证书")
	public ResponseResult getCertificate(@ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return changeCertificateService.getCertificate(admin);
	}

	/**
	 * 查询作用人信息
	 *
	 * @param authentication
	 * @return
	 */
	@PostMapping("userList")
	@ApiResponses(
			@ApiResponse(code = 200, message = "ok", response = SysChangeCertificateUserVO.class)
	)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "searchType", value = "1:用户名 2:设备名  3:部门 4:状态")
	})
	@ApiOperation("作用人信息")
	public ResponseResult userList(@RequestBody QueryObjectQO queryObjectQO, @ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return changeCertificateService.queryList(queryObjectQO, admin);
	}

	/**
	 * 读取cer文件信息
	 *
	 * @param file
	 * @return
	 */
	@PostMapping("certificateInfo")
	@ApiResponses(
			@ApiResponse(code = 200, message = "ok", response = SysChangeCertificateVO.class)
	)
	@ApiOperation("读取文件信息")
	public ResponseResult certificateInfo(@RequestParam(value = "file") MultipartFile file) throws Exception {
		return changeCertificateService.readCerFile(file);
	}

	/**
	 * 仅添加
	 *
	 * @return
	 */
	@PostMapping("addCertificate")
	@ApiOperation("添加证书信息")
	public ResponseResult addCertificate(@RequestParam(value = "file",required = false) MultipartFile file,
                                         @ApiParam("证书信息 json") SysChangeCertificateVO changeCertificateVO,
										 @ApiIgnore Authentication authentication) throws Exception {
        LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return changeCertificateService.addCertificate(admin, file, changeCertificateVO);
    }

	/**
	 * 下发
	 *
	 * @param changeCertificateId
	 * @return
	 */
	@PostMapping("push")
	@ApiOperation("下发")
	public ResponseResult sendCertificate(@ApiParam("证书id") Integer changeCertificateId, @ApiIgnore Authentication authentication) {
		LoginAdminDTO admin = (LoginAdminDTO) authentication.getPrincipal();
		return changeCertificateService.sendCertificate(changeCertificateId, admin);
	}



}