package com.chaozhanggui.admin.system.controller;

import com.chaozhanggui.admin.system.service.SystemApiService;
import com.chaozhanggui.admin.system.util.RSAUtil;
import com.chaozhanggui.common.system.config.RespBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统设置
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("systemApi")
@Slf4j
public class SystemApiController {


    @Autowired
    SystemApiService systemApiService;

    /**
     *  获取appid 信息
     * @param loginName
     * @param token
     * @param userId
     * @param appId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("querySystemApis")
    public RespBody querySystemApis(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
        ,@RequestParam("appId") String appId ,@RequestParam("pageNum") Integer pageNum
            ,@RequestParam("pageSize") Integer pageSize
    ){
        return systemApiService.querySystemApi(appId,pageNum,pageSize);
    }


    /**
     * 修改api 配置
     * @param loginName
     * @param token
     * @param userId
     * @param id
     * @param ip
     * @param publicKey
     * @param privateKey
     * @return
     */
    @GetMapping("modfityApi")
    public RespBody modfityApi(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
            ,@RequestParam("id") Integer id,@RequestParam("ip") String ip,@RequestParam("publicKey")  String publicKey,@RequestParam("privateKey") String privateKey ){
        return systemApiService.modifyApi(id,ip,publicKey,privateKey);
    }

    /**
     * 新增api
     * @param loginName
     * @param token
     * @param userId
     * @param appId
     * @param ip
     * @param publicKey
     * @param privateKey
     * @return
     */
    @GetMapping("initApi")
    public RespBody initApi(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
            ,@RequestParam("appId") String appId,@RequestParam("ip") String ip,@RequestParam("publicKey")  String publicKey,@RequestParam("privateKey") String privateKey ){
        return systemApiService.insertApi(appId,ip,publicKey,privateKey);
    }


    /**
     * 生成密钥对
     * @param loginName
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("createKey")
    public RespBody createKey(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId
    ){
        try {
          RSAUtil.KeyStore keys= RSAUtil.createKeys();
            Map<String,String> map=new HashMap<>();
            map.put("publicKey",keys.getPublicKey());
            map.put("privateKey",keys.getPrivateKey());
            return new RespBody("000000",map);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new RespBody("999944");
    }
}
