148 lines
3.8 KiB
Go
148 lines
3.8 KiB
Go
package imusermod
|
|
|
|
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.ImUser
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "imUserId", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "thirdPartyId", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func FindByUID(uid uint64) (data IMUser, err error) {
|
|
err = coll(nil).FindOne(&data, bson.M{"uid": uid})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindByUID", table, "FindOne", err), log.Any("uid", uid))
|
|
}
|
|
return
|
|
}
|
|
|
|
func FindByUIDQuiet(uid uint64) (data IMUser, err error) {
|
|
err = coll(nil).FindOne(&data, bson.M{"uid": uid})
|
|
return
|
|
}
|
|
|
|
func FindByIMUserIDQuiet(imUserID int64) (data IMUser, err error) {
|
|
err = coll(nil).FindOne(&data, bson.M{"imUserId": imUserID})
|
|
return
|
|
}
|
|
|
|
func FindByIMUserIDsQuiet(imUserIDs []int64) (data []IMUser, err error) {
|
|
if len(imUserIDs) == 0 {
|
|
return []IMUser{}, nil
|
|
}
|
|
err = coll(nil).Find(&data, bson.M{"imUserId": bson.M{"$in": imUserIDs}})
|
|
return
|
|
}
|
|
|
|
// FindByUIDsQuiet 批量按 uid 查 imUser 映射。
|
|
func FindByUIDsQuiet(uids []uint64) (data []IMUser, err error) {
|
|
if len(uids) == 0 {
|
|
return []IMUser{}, nil
|
|
}
|
|
err = coll(nil).Find(&data, bson.M{"uid": bson.M{"$in": uids}})
|
|
return
|
|
}
|
|
|
|
// IMUserIDByUID 解析单个 uid → imUserId,不存在返回 0。
|
|
func IMUserIDByUID(uid uint64) int64 {
|
|
m, _ := FindByUIDQuiet(uid)
|
|
return m.IMUserID
|
|
}
|
|
|
|
// IMUserIDMapByUIDs 批量解析 uid → imUserId(仅 imUserId>0 入表)。
|
|
func IMUserIDMapByUIDs(uids []uint64) (map[uint64]int64, error) {
|
|
list, err := FindByUIDsQuiet(uids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[uint64]int64, len(list))
|
|
for _, m := range list {
|
|
if m.IMUserID > 0 {
|
|
out[m.UID] = m.IMUserID
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// UIDByIMUserID 解析单个 imUserId → uid,不存在返回 0。
|
|
func UIDByIMUserID(imUserID int64) uint64 {
|
|
m, _ := FindByIMUserIDQuiet(imUserID)
|
|
return m.UID
|
|
}
|
|
|
|
// ListAfterUID 按 uid 升序游标分页:返回 uid > lastUID 的前 limit 条。
|
|
// lastUID 传 0 表示从头开始;分页恒定 O(limit),不走 skip。
|
|
// 仅返回 uid 与 imUserId 字段,配合现有 uid_1 索引足够,无需新建覆盖索引。
|
|
func ListAfterUID(lastUID uint64, limit int64) ([]IMUser, error) {
|
|
filter := bson.M{}
|
|
if lastUID > 0 {
|
|
filter["uid"] = bson.M{"$gt": lastUID}
|
|
}
|
|
opts := options.Find().
|
|
SetSort(bson.D{{Key: "uid", Value: 1}}).
|
|
SetLimit(limit).
|
|
SetProjection(bson.M{"uid": 1, "imUserId": 1})
|
|
var out []IMUser
|
|
if err := coll(nil).Find(&out, filter, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ListAfterUID", table, "Find", err), log.Any("lastUID", lastUID), log.Any("limit", limit))
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func UpsertByUID(uid uint64, imUserID int64, thirdPartyID string) error {
|
|
now := time.Now()
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"imUserId": imUserID,
|
|
"thirdPartyId": thirdPartyID,
|
|
"updatedAt": now,
|
|
},
|
|
"$setOnInsert": bson.M{
|
|
"uid": uid,
|
|
"createdAt": now,
|
|
},
|
|
}
|
|
var data IMUser
|
|
err := coll(nil).FindOneAndUpsert(&data, bson.M{"uid": uid}, update)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpsertByUID", table, "UpdateOne", err), log.Any("uid", uid), log.Any("imUserID", imUserID))
|
|
}
|
|
return err
|
|
}
|