package idgenerator import ( "errors" "github.com/bwmarrin/snowflake" ) // With default settings, this snowflake generator should be sufficiently fast enough // on most systems to generate 4096 unique ID's per millisecond. This is the maximum // that the snowflake ID format supports. That is, around 243-244 nanoseconds per operation. // 业务node, node ∈ [0, 1023] const ( NodeMin = 0 // 最小node 包括 NodeMax = 1023 // 最大node 包括 ) const ( _ = iota NodeMaterial // node 素材(商品) NodeCatalog // node 分类 NodeOrder // node 订单 NodeBanner // node banner NodeLogin // node login NodeToken // node token NodeAccount // node account NodeVeriCode // node vericode NodeMember // node 用户id NodeProduct // node 商品 NodeMoment // node 动态 NodeFootPrint // node 足迹 NodeThumbsUp // node 点赞 NodeAccountRelation // node 用户关系 NodeStreamer // node 主播 NodeFeedback // node 意见反馈 NodeCallHistory // node 通话记录 NodeStreamerLink // node 主播链接 NodeUserWxAddCheck // node 用户微信添加审核 NodeRealNameAuthentication // node 实名认证 NodeContactCustomerService // node 联系客服 NodeImageAudit // node 图像审核 NodeCoinOrder // node 金币订单 NodeImageAuditTask // node 图像审核任务 NodeTextAudit // node 文字审核 NodeTextAuditTask // node 文字审核任务 NodeDailyStatement // node 每日报表表 NodeWithdrawOrderId // node 提现订单 ) func GenIdInt64(node int64) (int64, error) { if node < NodeMin || node > NodeMax { return -1, errors.New("node number must include [0, 1023]") } n, err := snowflake.NewNode(node) if err != nil { return -1, err } return n.Generate().Int64(), nil } func GenIdString(node int64) (string, error) { if node < NodeMin || node > NodeMax { return "", errors.New("node number must include [0, 1023]") } n, err := snowflake.NewNode(node) if err != nil { return "", err } return n.Generate().String(), nil } // 商品 func GenProductId() string { id, _ := GenIdString(NodeProduct) return id } // 订单 func GenOrderId() string { id, _ := GenIdString(NodeOrder) return id } // 金币订单 func GenCoinOrderId() string { id, _ := GenIdString(NodeCoinOrder) return id } // banner func GenBannerId() int64 { id, _ := GenIdInt64(NodeBanner) return id } // login func GenLoginId() int64 { id, _ := GenIdInt64(NodeLogin) return id } // token func GenTokenId() int64 { id, _ := GenIdInt64(NodeToken) return id } // account func GenAccountId() int64 { id, _ := GenIdInt64(NodeAccount) return id } // vericode func GenVeriCodeId() int64 { id, _ := GenIdInt64(NodeVeriCode) return id } // catalog func GenCatalogId() int64 { id, _ := GenIdInt64(NodeCatalog) return id } // moment func GenMomentId() int64 { id, _ := GenIdInt64(NodeMoment) return id } // footprint func GenFootPrintId() int64 { id, _ := GenIdInt64(NodeFootPrint) return id } // thumbs_up func GenThumbsUpId() int64 { id, _ := GenIdInt64(NodeThumbsUp) return id } // account_relation func GenAccountRelationId() int64 { id, _ := GenIdInt64(NodeAccountRelation) return id } // streamer func GenStreamerId() int64 { id, _ := GenIdInt64(NodeStreamer) return id } // feedback func GenFeedbackId() int64 { id, _ := GenIdInt64(NodeFeedback) return id } // callhistory func GenCallHistoryId() int64 { id, _ := GenIdInt64(NodeCallHistory) return id } // streamerlink func GenStreamerLinkId() int64 { id, _ := GenIdInt64(NodeStreamerLink) return id } // userwxaddcheck func GenUserWxAddCheckId() int64 { id, _ := GenIdInt64(NodeUserWxAddCheck) return id } // realname_authentication func GenRealNameAuthenticationId() int64 { id, _ := GenIdInt64(NodeRealNameAuthentication) return id } // contact_customer_service func GenContactCustomerServiceId() int64 { id, _ := GenIdInt64(NodeContactCustomerService) return id } // daily_statement func GenDailyStatementId() int64 { id, _ := GenIdInt64(NodeDailyStatement) return id } // withdraw_order func GenWithdrawOrderId() string { id, _ := GenIdString(NodeWithdrawOrderId) return id }