package user.person;

import com.alibaba.fastjson.JSONObject;
import common.Constant;
import config.Config;
import common.Resp;
import exception.ZXChainException;
import org.apache.commons.lang3.StringUtils;
import util.HttpUtil;
import util.Tools;
import util.ZxlSDKUtil;

public class RegisterPerson {
    String personName;
    String email;
    String mobile;
    String idCard;
    int cardType;
    String platformPubKey = Config.platformPubKey;
    String platformSignData;

    public RegisterPerson() {
    }

    public RegisterPerson(String personName, String email, String mobile, String idCard, int cardType) {
        this.personName = personName;
        this.email = email;
        this.mobile = mobile;
        this.idCard = idCard;
        this.cardType = cardType;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public int getCardType() {
        return cardType;
    }

    public void setCardType(int cardType) {
        this.cardType = cardType;
    }

    public String getPlatformPubKey() {
        return platformPubKey;
    }

    public void setPlatformPubKey(String platformPubKey) {
        this.platformPubKey = platformPubKey;
    }

    public String getPlatformSignData() {
        return platformSignData;
    }

    public void setPlatformSignData(String platformSignData) {
        this.platformSignData = platformSignData;
    }

    public String register() throws Exception {
        if (this.cardType < 1 || this.cardType > 7) {
            throw new RuntimeException("cardType参数错误: "+this.cardType);
        }

        if (this.cardType == 1 && !Tools.checkIdCard(this.idCard)) {
            throw new RuntimeException("身份证格式错误: "+this.idCard);
        }

        if (StringUtils.isBlank(this.personName)) {
            throw new RuntimeException("personName参数错误: "+this.personName);
        }

        JSONObject headers = ZxlSDKUtil.generateApiSign();

        //构造参数
        String sign = ZxlSDKUtil.signByPriKey(Config.platformPrivateKey,  Tools.getSignData(this.getPersonName(), this.getMobile(), this.getIdCard()));
        this.setPlatformSignData(sign);

        JSONObject param = Tools.getJson(this);

        //调用接口
        Resp resp = HttpUtil.apiPostJsonX("https://open.zxchain.qq.com/api/v1/user/register/person_platform/down_grade", param, headers);
        if (resp.getRetCode() != Constant.ZXL_OK) {
            throw new ZXChainException(resp.getRetMsg());
        }

        return resp.getData(RegisterPersonResp.class).userIdentification;
    }
}
