71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package fsuidmod
|
|
|
|
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"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.Fsid2UID
|
|
|
|
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: "fsid", Value: 1}, {Key: "uid", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// InsertFsid2UID 插入一条映射关系
|
|
func InsertFsid2UID(fsid string, uid uint64) error {
|
|
record := Fs2UID{
|
|
Fsid: fsid,
|
|
UID: uid,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
if _, err := coll(nil).InsertOne(&record); err != nil {
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return nil
|
|
}
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertFsid2UID", table, "InsertOne", err),
|
|
log.Any("uid", uid), log.Any("fsid", fsid))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindFsid2UID 查找条映射关系
|
|
func FindFsid2UID(fsid string) (uint64, error) {
|
|
var record Fs2UID
|
|
if err := coll(nil).FindOne(&record, bson.M{"fsid": fsid}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertFsid2UID", table, "InsertOne", err),
|
|
log.Any("fsid", fsid))
|
|
return 0, err
|
|
}
|
|
return record.UID, nil
|
|
}
|