111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
package idmod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type IDType string
|
|
|
|
const (
|
|
IDTypeUID IDType = "uid"
|
|
IDTypeMsgID IDType = "msgID"
|
|
IDTypeMerchantID IDType = "merchID"
|
|
IDTypeQuestionnaireId IDType = "questionnaireId"
|
|
IDTypeLouFengNumber IDType = "loufengNumber"
|
|
|
|
table = models.IDTable
|
|
)
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 初始化索引
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// InitNextUID 初始化UID
|
|
func initNextID() {
|
|
initNextAutoID(IDTypeUID)
|
|
initNextAutoID(IDTypeMsgID)
|
|
initNextAutoID(IDTypeMerchantID)
|
|
initNextAutoID(IDTypeQuestionnaireId)
|
|
}
|
|
|
|
// GetNextUID 下一个用户的ID
|
|
func GetNextUID() (uint64, error) {
|
|
return getNextAutoID(IDTypeUID)
|
|
}
|
|
|
|
// GetNextUID 下一个消息的ID
|
|
func GetNextMsgID() (uint64, error) {
|
|
return getNextAutoID(IDTypeMsgID)
|
|
}
|
|
|
|
// GetNextUID 下一个商人的ID
|
|
func GetNextMerchantID() (uint64, error) {
|
|
return getNextAutoID(IDTypeMerchantID)
|
|
}
|
|
|
|
// GetNextUID 下一个调查问卷的ID
|
|
func GetQuestionnaireId() (uint64, error) {
|
|
return getNextAutoID(IDTypeQuestionnaireId)
|
|
}
|
|
|
|
func initNextAutoID(idType IDType) {
|
|
now := time.Now()
|
|
if _, err := coll(nil).UpsertOne(
|
|
bson.M{"type": idType},
|
|
bson.M{"$setOnInsert": bson.M{"type": idType, "createdAt": now, "updatedAt": now}}); err != nil {
|
|
panic(fmt.Sprintf("init auto id err:%+v", err))
|
|
}
|
|
}
|
|
|
|
func getNextAutoID(idType IDType) (uint64, error) {
|
|
u := AutoID{}
|
|
if err := coll(nil).FindOneAndUpdate(
|
|
&u,
|
|
bson.M{"type": idType},
|
|
bson.M{"$inc": bson.M{"autoId": 1}, "$set": bson.M{"updatedAt": time.Now()}}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getNextAutoID", table, "FindOneAndUpdate", err), log.Any("idType", idType))
|
|
return 0, err
|
|
}
|
|
return u.AID, nil
|
|
}
|
|
|
|
func GetLouFengNumber(businessDate string) (int, error) {
|
|
now := time.Now()
|
|
n := AutoNumber{}
|
|
if err := coll(nil).FindOneAndUpsert(
|
|
&n,
|
|
bson.M{"type": IDTypeLouFengNumber},
|
|
bson.M{"$inc": bson.M{"numberMap." + businessDate: 1}, "$set": bson.M{"updatedAt": now},
|
|
"$setOnInsert": bson.M{"type": IDTypeLouFengNumber, "createdAt": now}},
|
|
); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getLouFengNumber", table, "FindOneAndUpsert", err), log.Any("businessDate", businessDate))
|
|
return 0, err
|
|
}
|
|
return n.NumberMap[businessDate], nil
|
|
}
|