package com.qmth.boot.core.solar.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.qmth.boot.core.solar.enums.OrgType;
import com.qmth.boot.tools.codec.CodecUtils;
import com.qmth.boot.tools.models.ByteArray;

import java.io.Serializable;

/**
 * 机构信息
 */
public class OrgInfo implements Serializable {

    private static final long serialVersionUID = -3205639194613421765L;

    private Long id;

    private String code;

    private String name;

    private String logo;

    private OrgType type;

    private String accessKey;

    private String accessSecret;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public OrgType getType() {
        return type;
    }

    public void setType(OrgType type) {
        this.type = type;
    }

    /**
     * 尝试按照url格式获取logo链接
     *
     * @return
     */
    @JsonIgnore
    public String getLogoUrl() {
        if (logo != null && logo.startsWith("http")) {
            return logo;
        } else {
            return null;
        }
    }

    /**
     * 尝试按照base64格式获取logo二进制内容
     *
     * @return
     */
    @JsonIgnore
    public byte[] getLogoData() {
        if (logo != null && !logo.startsWith("http")) {
            return CodecUtils.fromBase64(logo);
        } else {
            return null;
        }
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    public void setAccessSecret(String accessSecret) {
        this.accessSecret = accessSecret;
    }

    /**
     * 返回第三方接口访问鉴权使用的key
     *
     * @return
     */
    @JsonIgnore
    public String getThirdPartyKey() {
        return code;
    }

    /**
     * 返回第三方接口访问鉴权使用的secret
     *
     * @return
     */
    @JsonIgnore
    public String getThirdPartySecret() {
        return ByteArray.hmacsha256(accessKey, accessSecret).toHexString().toLowerCase();
    }
}
