package kv;

import common.Constant;
import common.Resp;
import config.Config;
import exception.ZXChainException;
import util.HttpUtil;
import util.Tools;
import util.ZxlSDKUtil;

public class KvSave {
    String publicKey;
    String kvKey;
    String kvValue;
    String sign;
    String operateId;

    public KvSave() {
    }

    public KvSave(String publicKey, String kvKey, String kvValue, String operateId) {
        this.publicKey = publicKey;
        this.kvKey = kvKey;
        this.kvValue = kvValue;
        this.operateId = operateId;
    }

    public void save(String priKey) throws Exception {
        String signData = Tools.getSignData(
                this.publicKey,
                this.kvKey,
                ZxlSDKUtil.sm3Hash(this.kvValue),
                this.operateId);
        this.setSign(ZxlSDKUtil.signByPriKey(priKey, signData));
        //调用接口
        Resp resp = HttpUtil.apiPostJsonX(Config.baseUrl + "/api/v1/kv/save", Tools.getJson(this), ZxlSDKUtil.generateApiSign());
        if (resp.getRetCode() != Constant.ZXL_OK) {
            throw new ZXChainException(resp.getRetMsg());
        }
    }

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getKvKey() {
        return kvKey;
    }

    public void setKvKey(String kvKey) {
        this.kvKey = kvKey;
    }

    public String getKvValue() {
        return kvValue;
    }

    public void setKvValue(String kvValue) {
        this.kvValue = kvValue;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getOperateId() {
        return operateId;
    }

    public void setOperateId(String operateId) {
        this.operateId = operateId;
    }
}
