package com.ruoyi.demo.controller;

import com.ruoyi.common.apiencrypt.annotation.ApiDecrypt;
import com.ruoyi.common.apiencrypt.annotation.ApiEncrypt;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * api 加密
 *
 * @Author 1024创新实验室-主任:卓大
 * @Date 2023/10/21 09:21:20
 */
@Validated
@Api(value = "api加密 控制器", tags = {"api加密 管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/apiEncrypt")
public class AdminApiEncryptController extends BaseController {

    @ApiDecrypt
    @PostMapping("/apiEncrypt/testRequestEncrypt")
    @Operation(summary = "测试 请求加密")
    public R<JweForm> testRequestEncrypt(@RequestBody @Valid JweForm form) {
        return R.ok(form);
    }

    @ApiEncrypt
    @PostMapping("/apiEncrypt/testResponseEncrypt")
    @Operation(summary = "测试 返回加密")
    public R<JweForm> testResponseEncrypt(@RequestBody @Valid JweForm form) {
        return R.ok(form);
    }

    @ApiDecrypt
    @ApiEncrypt
    @PostMapping("/apiEncrypt/testDecryptAndEncrypt")
    @Operation(summary = "测试 请求参数加密和解密、返回数据加密和解密")
    public R<JweForm> testDecryptAndEncrypt(@RequestBody @Valid JweForm form) {
        return R.ok(form);
    }

    @ApiDecrypt
    @ApiEncrypt
    @PostMapping("/apiEncrypt/testArray")
    @Operation(summary = "测试 数组加密和解密")
    public R<List<JweForm>> testArray(@RequestBody @Valid List<JweForm> list) {
        return R.ok(list);
    }

    @Data
    public static class JweForm {
        @NotBlank(message = "姓名 不能为空")
        String name;

        @NotNull
        @Min(value = 1)
        Integer age;
    }

}
