package com.clx.performance.controller.thirdparty;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nbopen.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value="/nbbank")
@Api(tags = "宁波银行回调")
public class NbBankController {

    @ApiOperation(value = "notify", notes = "<br>By:艾庆国")
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public SDKResponse bankNotify(String message) throws Exception {
        log.info("宁波银行回调, data:{}", message);

        RequestApiData requestApiData = new RequestApiData();
        requestApiData.setAppkey("77667c76_3503_4c04_95f7_fc10938c7942");
        requestApiData.setData(message);

        RequestHead requestHead = new RequestHead();
        requestHead.setRqsJrnlNo(NBOpenSDK.getRandom());

        SDKRequest request = new SDKRequest();
        request.setData(requestApiData);
        request.setHead(requestHead);

        System.out.println("SDK请求内容: "+ JSON.toJSONString(request));
        SDKResponse result = NBOpenSDK.decryptMessage(request);
        System.out.println("SDK响应结果: "+JSON.toJSONString(result));


        return null;

    }

    private SDKResponse getResult() throws Exception {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errorCode", "000000");
        jsonObject.put("errorMsg", "响应成功");

        RequestApiData requestApiData = new RequestApiData();
        requestApiData.setAppkey("77667c76_3503_4c04_95f7_fc10938c7942");
        requestApiData.setData(jsonObject);

        RequestHead requestHead = new RequestHead();
        requestHead.setRqsJrnlNo(NBOpenSDK.getRandom());

        SDKRequest request = new SDKRequest();
        request.setData(requestApiData);
        request.setHead(requestHead);

        log.info("加密前: "+ JSON.toJSONString(request));
        SDKResponse result = NBOpenSDK.encryptMessage(request);
        log.info("加密后: "+JSON.toJSONString(result));

        return result;
    }

}
