package com.rf.psychological.api.key.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.api.key.dao.model.KeyEntity;
import com.rf.psychological.api.key.service.KeyService;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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 java.util.List;

/**
 * @Author:zzf
 * @Date:2022/11/2:10:00
 * @Description:秘钥管理
 */
@Slf4j
@RestController
@RequestMapping("/v1/key")
@Api(tags = "秘钥")
public class KeyApi extends BaseController {

    @Autowired
    private KeyService keyService;

    @ApiOperation("保存秘钥")
    @PostMapping("/save")
    public Result save(@RequestBody String json) throws Exception {
        KeyEntity key = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"))).toJavaObject(KeyEntity.class);
        this.keyService.save(key);
        return success();
    }

    @ApiOperation(value = "查询",notes = "如果按照机构号查询，key为institutionNo")
    @PostMapping("/find")
    public Result find(@RequestBody String json) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data")));
        List<KeyEntity> keyEntityList = this.keyService.find(jsonObject);
        return success(keyEntityList);
    }


}
