Initial commit

This commit is contained in:
2026-05-20 16:13:58 +08:00
commit ceffbe382e
500 changed files with 157469 additions and 0 deletions
+161
View File
@@ -0,0 +1,161 @@
package genId
import (
"stat/common/genId/lid"
"stat/common/mongos"
)
var roleIdLid *lid.Lid // 角色id
var landingpageIdLid *lid.Lid // 落地页id
var versionLid *lid.Lid // 版本id自增器
var commodityLid *lid.Lid // 商品自增器
var adsLocLid *lid.Lid // 广告位置id自增器
var adsLid *lid.Lid // 广告id自增器
var lfLid *lid.Lid // 楼凤id自增器
var lfComplaintLid *lid.Lid // 楼凤投诉自增器
var modelBrokerLid *lid.Lid // 嫩模经纪人(茶老板)自增器
var modelLid *lid.Lid // 嫩模自增器
var novelLid *lid.Lid // 小说自增器
var audioBook *lid.Lid // 有声小说自增器
var landpageConfig *lid.Lid // 落地页配置id
var appCategory *lid.Lid //
var appLocLid *lid.Lid // 应用中心位置id自增器
func Start() {
var err error
collection := mongos.MgoManager.Database().Collection("generate_id")
roleIdLid, err = lid.NewLid(collection, "role_id", 1)
if err != nil {
panic(err)
}
landingpageIdLid, err = lid.NewLid(collection, "langdingpage_id", 1)
if err != nil {
panic(err)
}
versionLid, err = lid.NewLid(collection, "version_id", 1)
if err != nil {
panic(err)
}
commodityLid, err = lid.NewLid(collection, "commodity_id", 1)
if err != nil {
panic(err)
}
adsLocLid, err = lid.NewLid(collection, "advertise_loc_id", 1)
if err != nil {
panic(err)
}
adsLid, err = lid.NewLid(collection, "advertise_id", 1)
if err != nil {
panic(err)
}
lfLid, err = lid.NewLid(collection, "loufeng_id", 1)
if err != nil {
panic(err)
}
lfComplaintLid, err = lid.NewLid(collection, "loufeng_complaint_id", 1)
if err != nil {
panic(err)
}
modelBrokerLid, err = lid.NewLid(collection, "young_model_broker_id", 1)
if err != nil {
panic(err)
}
modelLid, err = lid.NewLid(collection, "young_model_id", 1)
if err != nil {
panic(err)
}
novelLid, err = lid.NewLid(collection, "novel_id", 100)
if err != nil {
panic(err)
}
audioBook, err = lid.NewLid(collection, "audio_book_id", 100)
if err != nil {
panic(err)
}
landpageConfig, err = lid.NewLid(collection, "landpage_config_id", 100)
if err != nil {
panic(err)
}
appCategory, err = lid.NewLid(collection, "app_category_id", 100)
if err != nil {
panic(err)
}
appLocLid, err = lid.NewLid(collection, "app_loc_id", 100)
if err != nil {
panic(err)
}
}
func GetNextRoleId() int64 {
return roleIdLid.Get()
}
func GetNextLandingpageId() int64 {
return landingpageIdLid.Get()
}
func GetNextVersionId() int64 {
return versionLid.Get()
}
func GetNextCommodity() int64 {
return commodityLid.Get()
}
func GetNextAdsLocId() int64 {
return adsLocLid.Get()
}
func GetNextAdsId() int64 {
return adsLid.Get()
}
func GetNextLfId() int64 {
return lfLid.Get()
}
func GetNextLfComplaintId() int64 {
return lfComplaintLid.Get()
}
func GetNextModelBrokerId() int64 {
return modelBrokerLid.Get()
}
func GetNextModelId() int64 {
return modelLid.Get()
}
func GetNextNovelId() int64 {
return novelLid.Get()
}
func GetNextAudioBookId() int64 {
return audioBook.Get()
}
func GetLandpageConfigId() int64 {
return landpageConfig.Get()
}
func GetAppCategoryId() int64 {
return appCategory.Get()
}
func GetNextAppLocId() int64 {
return appLocLid.Get()
}
+102
View File
@@ -0,0 +1,102 @@
package lid
import (
"context"
"stat/common/mongos"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const notFindMsg = "mongo: no documents in result"
type IdModel struct {
BusinessId string `bson:"business_id"` //业务id
Step int64 `bson:"step"` //步长
MaxId int64 `bson:"max_id"` //最大id
Description string `bson:"description"` //描述
CreatedAt time.Time `bson:"createdAt"` //创建时间
UpdatedAt time.Time `bson:"updatedAt"` //更改时间
}
type Lid struct {
db *mongo.Collection // 数据库连接
businessId string // 业务id
ch chan int64 // id缓冲池
min, max int64 // id段最小值,最大值
}
// NewLid 创建一个lid,db:数据库连接;businessId:业务id;len:缓冲池大小
func NewLid(db *mongo.Collection, businessId string, len int) (*Lid, error) {
lid := Lid{
db: db,
businessId: businessId,
ch: make(chan int64, len),
}
go lid.productId()
return &lid, nil
}
// Get 获取自增id
func (l *Lid) Get() int64 {
return <-l.ch
}
// productId 生产id
func (l *Lid) productId() {
l.reset()
for {
if l.min >= l.max {
l.reset()
}
l.min++
l.ch <- l.min
}
}
// reset 不断尝试从数据库获取,直到成功
func (l *Lid) reset() {
for {
err := l.getFromDB()
if err == nil {
return
}
time.Sleep(time.Second)
continue
}
}
// getFromDB 从数据库获取id段
func (l *Lid) getFromDB() error {
var (
maxId int64
step int64
idModel IdModel
)
txErr := mongos.Transaction(context.Background(), func(sctx mongo.SessionContext) (err error) {
err = l.db.FindOne(sctx, bson.M{"business_id": l.businessId}).Decode(&idModel)
if err != nil {
if err.Error() == notFindMsg {
_, err = l.db.InsertOne(sctx, bson.M{"business_id": l.businessId, "max_id": 0, "step": 10, "description": "id自增器", "createdAt": time.Now(), "updatedAt": time.Now()})
if err != nil {
return
}
} else {
return
}
}
maxId = idModel.MaxId
step = idModel.Step
_, err = l.db.UpdateOne(sctx, bson.M{"business_id": l.businessId}, bson.M{"$set": bson.M{"max_id": maxId + step}})
if err != nil {
return
}
l.min = maxId
l.max = maxId + step
return
})
return txErr
}