package com.iformall.domain.po;

import com.iformall.utils.BaseConstant;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Table(name = "wx_merchant_b_user")
public class WxMerchantBUser implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    protected Long id;

    @Transient
    protected List<Long> ids;
    @Transient
    protected String sortColumns;

    public Long getId() {
        return id;
    }

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

    public String getSortColumns() {
        return sortColumns;
    }

    public List<Long> getIds() {
        return ids;
    }

    public void setIds(List<Long> ids) {
        this.ids = ids;
    }


    /*租户ID**/
    @io.swagger.annotations.ApiModelProperty(value = "租户ID", name = "tenantId")
    private String tenantId;
    /*B端用户手机号**/
    @io.swagger.annotations.ApiModelProperty(value = "B端用户手机号", name = "phone")
    private String phone;
    /***/
    @io.swagger.annotations.ApiModelProperty(value = "", name = "userPwd")
    private String userPwd;
    /*商户id**/
    @io.swagger.annotations.ApiModelProperty(value = "商户id", name = "merchantId")
    private Long merchantId;
    /*创建时间**/
    @io.swagger.annotations.ApiModelProperty(value = "创建时间", name = "createDate")
    private Date createDate;
    /*更新时间**/
    @io.swagger.annotations.ApiModelProperty(value = "更新时间", name = "updateDate")
    private Date updateDate;
    /*小程序的appId**/
    @io.swagger.annotations.ApiModelProperty(value = "小程序的appId", name = "appId")
    private String appId;
    /*用户登录用token**/
    @io.swagger.annotations.ApiModelProperty(value = "用户登录用token", name = "token")
    private String token;
    /*用户过期时间**/
    @io.swagger.annotations.ApiModelProperty(value = "用户过期时间", name = "expireTime")
    private Date expireTime;
    /*b端用户姓名**/
    @io.swagger.annotations.ApiModelProperty(value = "b端用户姓名", name = "name")
    private String name;
    /*绑定状态1未绑定商户0已绑定商户**/
    @io.swagger.annotations.ApiModelProperty(value = "绑定状态1未绑定商户0已绑定商户", name = "status")
    private Integer status;


    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String _tenantId) {
        tenantId = _tenantId;
    }


    public String getPhone() {
        return phone;
    }

    public void setPhone(String _phone) {
        phone = _phone;
    }

    public Long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(Long _merchantId) {
        merchantId = _merchantId;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date _createDate) {
        createDate = _createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date _updateDate) {
        updateDate = _updateDate;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String _appId) {
        appId = _appId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String _token) {
        token = _token;
    }

    public Date getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Date _expireTime) {
        expireTime = _expireTime;
    }

    public String getName() {
        return name;
    }

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

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public static enum Field {
        Id_ASC("`id` ASC"), Id_DESC("`id` DESC"),
        TenantId_ASC("`tenant_id` ASC"), TenantId_DESC("`tenant_id` DESC"),
        Phone_ASC("`phone` ASC"), Phone_DESC("`phone` DESC"),
        UserPwd_ASC("`user_pwd` ASC"), UserPwd_DESC("`user_pwd` DESC"),
        MerchantId_ASC("`merchant_id` ASC"), MerchantId_DESC("`merchant_id` DESC"),
        CreateDate_ASC("`create_date` ASC"), CreateDate_DESC("`create_date` DESC"),
        UpdateDate_ASC("`update_date` ASC"), UpdateDate_DESC("`update_date` DESC"),
        AppId_ASC("`app_id` ASC"), AppId_DESC("`app_id` DESC"),
        Token_ASC("`token` ASC"), Token_DESC("`token` DESC"),
        ExpireTime_ASC("`expire_time` ASC"), ExpireTime_DESC("`expire_time` DESC"),
        Name_ASC("`name` ASC"), Name_DESC("`name` DESC"),
        Status_ASC("`status` ASC"), Status_DESC("`status` DESC");

        private String value;

        Field(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setCol(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return this.getValue();
        }
    }

    public void setSortColumns(WxMerchantBUser.Field... fields) {
        if (fields == null || fields.length == 0) {
            return;
        }
        for (int k = 0; k < fields.length; k++) {
            if (fields[k] == null) {
                return;
            }
        }
        StringBuilder sb = new StringBuilder(fields[0].toString());
        for (int k = 1; k < fields.length; k++) {
            sb.append(",");
            sb.append(fields[k].toString());
        }

        this.sortColumns = sb.toString();
    }

    public void setSortColumns(String sortColumns) {
        if (sortColumns == null || "".equals(sortColumns.trim())) {
            return;
        }
        if (sortColumns.contains(",")) {
            String[] cols = sortColumns.split(",");
            java.util.List<Field> fList = new java.util.ArrayList();
            for (int k = 0; k < cols.length; k++) {
                fList.add(Field.valueOf(cols[k]));
            }
            this.setSortColumns(fList.toArray(new Field[fList.size()]));
        } else {
            this.setSortColumns(Field.valueOf(sortColumns));
        }
    }

    public void createToken(Date currentDate) {
        if (expireTime == null || expireTime.getTime() < currentDate.getTime()) {
            //生成一个token
            String token = UUID.randomUUID().toString();
            //过期时间
            Date expireTime = new Date(currentDate.getTime() + BaseConstant.EXPIRE * 1000);
            setToken(token);
            setExpireTime(expireTime);
        }
    }
}
