init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package usermod
|
||||
|
||||
import (
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
func (user *User) GetID() int64 {
|
||||
return user.Id
|
||||
}
|
||||
|
||||
func (user *User) GetStatus() pubdef.UserStatus {
|
||||
return user.Status
|
||||
}
|
||||
|
||||
// CheckCoinEnough 检查金币是否足够扣除
|
||||
func (user *User) CheckCoinEnough(need int64) bool {
|
||||
return user.Recharge >= need
|
||||
}
|
||||
Executable
+125
@@ -0,0 +1,125 @@
|
||||
package usermod
|
||||
|
||||
type TagUser struct {
|
||||
Id string // ID
|
||||
Type string // 类型
|
||||
VipType string // Vip类型
|
||||
Status string // 状态
|
||||
Region string // 地区
|
||||
RegIp string // 注册IP
|
||||
LastIp string // 上次登陆IP
|
||||
Backgroud string // 背景图
|
||||
Avatar string // 头像
|
||||
NickName string // 昵称
|
||||
Introduction string // 简介
|
||||
Gender string // 性别 0-未知,1-男,2-女
|
||||
LoginType string // 登陆方式
|
||||
Age string // 年龄
|
||||
LastLoginAt string // 上次登陆时间
|
||||
InviteCode string // 邀请码
|
||||
DevID string // 设备唯一标示
|
||||
Mac string // 设备唯一mac地址
|
||||
DevV2 string // 设备唯一设备ID
|
||||
Device string // 设备
|
||||
SysType string // 系统类型 ios android
|
||||
AppVer string // app版本
|
||||
SessionID string // 会话ID
|
||||
DeviceBrand string // 设备品牌
|
||||
DeviceModel string // 设备型号
|
||||
SystemName string // 系统名称
|
||||
SystemVersion string // 系统版本
|
||||
Invites string // 我邀请的人的个数(我邀请他人)
|
||||
AgentInviteCode string // 被邀请的inviteCode
|
||||
Password string // 登陆密码
|
||||
Recharge string // 当前充值余额,金币个数
|
||||
RechargeTotal string // 累计充值额,金币个数
|
||||
ConsumptionTotal string // 金币总消费数量
|
||||
ModifyUserNameAt string // 上一次修改用户时间
|
||||
Auth string // 是否认证
|
||||
ConsecutiveLogin string // 连续登陆天数
|
||||
DistrictCode string // 商区码
|
||||
IsDirect string // 是否是直推 true:是直推用户
|
||||
IsRegister string // 是否已注册
|
||||
Score string // 积分
|
||||
ScoreTotal string // 累计积分
|
||||
ScoreLvName string // 积分等级名称
|
||||
ScoreInvites string // 邀请人获得的积分累计
|
||||
RegisterTime string //
|
||||
CreatedAt string // 创建时间
|
||||
UpdatedAt string // 更新时间
|
||||
CurrentVipExpire string // 当前vip过期时间
|
||||
SysMsgAt string // 获取系统消息时间
|
||||
ShowCardId string // 最新会员卡id
|
||||
ShowCardName string // 最新会员卡名称
|
||||
OriVer string // 注册时的app版本
|
||||
AccountNo string // 账号
|
||||
Email string // 邮箱
|
||||
MerchantExpiredAt string // 历史遗留字段
|
||||
PrivacyMode string // 隐私模式
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
User TagUser
|
||||
}
|
||||
var TN struct {
|
||||
User string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.User.Id = "_id"
|
||||
FN.User.Type = "type"
|
||||
FN.User.VipType = "VipType"
|
||||
FN.User.Status = "status"
|
||||
FN.User.Region = "region"
|
||||
FN.User.RegIp = "regIp"
|
||||
FN.User.LastIp = "lastIp"
|
||||
FN.User.Backgroud = "backgroud"
|
||||
FN.User.Avatar = "avatar"
|
||||
FN.User.NickName = "nickName"
|
||||
FN.User.Introduction = "introduction"
|
||||
FN.User.Gender = "gender"
|
||||
FN.User.LoginType = "loginType"
|
||||
FN.User.Age = "age"
|
||||
FN.User.LastLoginAt = "lastLoginAt"
|
||||
FN.User.InviteCode = "inviteCode"
|
||||
FN.User.DevID = "devID"
|
||||
FN.User.Mac = "mac"
|
||||
FN.User.DevV2 = "devV2"
|
||||
FN.User.Device = "device"
|
||||
FN.User.SysType = "sysType"
|
||||
FN.User.AppVer = "appVer"
|
||||
FN.User.SessionID = "sessionId"
|
||||
FN.User.DeviceBrand = "deviceBrand"
|
||||
FN.User.DeviceModel = "deviceModel"
|
||||
FN.User.SystemName = "systemName"
|
||||
FN.User.SystemVersion = "systemVersion"
|
||||
FN.User.Invites = "invites"
|
||||
FN.User.AgentInviteCode = "agentInviteCode"
|
||||
FN.User.Password = "password"
|
||||
FN.User.Recharge = "recharge"
|
||||
FN.User.RechargeTotal = "rechargeTotal"
|
||||
FN.User.ConsumptionTotal = "consumptionTotal"
|
||||
FN.User.ModifyUserNameAt = "modifyUserNameAt"
|
||||
FN.User.Auth = "auth"
|
||||
FN.User.ConsecutiveLogin = "consecutiveLogin"
|
||||
FN.User.DistrictCode = "districtCode"
|
||||
FN.User.IsDirect = "isDirect"
|
||||
FN.User.IsRegister = "isRegister"
|
||||
FN.User.Score = "score"
|
||||
FN.User.ScoreTotal = "scoreTotal"
|
||||
FN.User.ScoreLvName = "scoreLvName"
|
||||
FN.User.ScoreInvites = "scoreInvites"
|
||||
FN.User.RegisterTime = "registerTime"
|
||||
FN.User.CreatedAt = "createdAt"
|
||||
FN.User.UpdatedAt = "updatedAt"
|
||||
FN.User.CurrentVipExpire = "currentVipExpire"
|
||||
FN.User.SysMsgAt = "sysMsgAt"
|
||||
FN.User.ShowCardId = "showCardId"
|
||||
FN.User.ShowCardName = "showCardName"
|
||||
FN.User.OriVer = "oriVer"
|
||||
FN.User.AccountNo = "accountNo"
|
||||
FN.User.Email = "email"
|
||||
FN.User.MerchantExpiredAt = "merchantExpiredAt"
|
||||
FN.User.PrivacyMode = "privacyMode"
|
||||
TN.User = "user"
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
//go:generate gen at -t table -s User
|
||||
//go:generate gen bt
|
||||
//go:generate gen mm
|
||||
//go:generate gen g update
|
||||
package usermod
|
||||
|
||||
import (
|
||||
"time"
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
// NormalizeDate 将时间归一化为当天北京时间零点
|
||||
func NormalizeDate(t time.Time) time.Time {
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
t = t.In(loc)
|
||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||
}
|
||||
|
||||
type TagStat struct {
|
||||
//标签名字
|
||||
Tag string `json:"tag" bson:"tag"`
|
||||
Count int64 `json:"count" bson:"count"`
|
||||
}
|
||||
|
||||
type TagStatList []*TagStat
|
||||
|
||||
// @table:user
|
||||
//
|
||||
//go:generate fox mongo --tag
|
||||
type User struct {
|
||||
Id int64 `json:"id" bson:"_id"` // ID
|
||||
AccountNo string `json:"accountNo" bson:"accountNo"` // 账号
|
||||
Email string `json:"email" bson:"email"` // 邮箱
|
||||
Type pubdef.UserType `json:"type" bson:"type"` // 类型
|
||||
VipType pubdef.VipType `json:"vipType" bson:"VipType"` // Vip类型
|
||||
Status pubdef.UserStatus `json:"status" bson:"status"` // 状态
|
||||
Region string `json:"region" bson:"region"` // 地区
|
||||
RegIp string `json:"regIp" bson:"regIp"` // 注册IP
|
||||
LastIp string `json:"lastIp" bson:"lastIp"` // 上次登陆IP
|
||||
Backgroud string `json:"backgroud" bson:"backgroud"` // 背景图
|
||||
Avatar string `json:"avatar" bson:"avatar"` // 头像
|
||||
NickName string `json:"nickName" bson:"nickName"` // 昵称
|
||||
Introduction string `json:"introduction" bson:"introduction"` // 简介
|
||||
Gender int64 `json:"gender" bson:"gender"` // 性别 0-未知,1-男,2-女
|
||||
LoginType pubdef.LoginType `json:"loginType" bson:"loginType"` // 登陆方式
|
||||
Age int64 `json:"age" bson:"age"` // 年龄
|
||||
LastLoginAt time.Time `json:"lastLoginAt" bson:"lastLoginAt"` // 上次登陆时间
|
||||
InviteCode string `json:"inviteCode" bson:"inviteCode"` // 邀请码
|
||||
DevID string `json:"devID" bson:"devID"` // 设备唯一标示
|
||||
Mac string `json:"mac" bson:"mac"` // 设备唯一mac地址
|
||||
DevV2 string `json:"devV2" bson:"devV2"` // 设备唯一设备ID
|
||||
Device string `json:"device" bson:"device"` // 设备
|
||||
SysType string `json:"sysType" bson:"sysType"` // 系统类型 ios android
|
||||
AppVer string `json:"appVer" bson:"appVer"` // app版本
|
||||
SessionID string `json:"sessionId" bson:"sessionId"` // 会话ID
|
||||
DeviceBrand string `json:"deviceBrand" bson:"deviceBrand"` // 设备品牌
|
||||
DeviceModel string `json:"deviceModel" bson:"deviceModel"` // 设备型号
|
||||
SystemName string `json:"systemName" bson:"systemName"` // 系统名称
|
||||
SystemVersion string `json:"systemVersion" bson:"systemVersion"` // 系统版本
|
||||
Invites int64 `json:"invites" bson:"invites"` // 我邀请的人的个数(我邀请他人)
|
||||
AgentInviteCode string `json:"agentInviteCode" bson:"agentInviteCode"` // 被邀请的inviteCode
|
||||
Password string `json:"password" bson:"password"` // 登陆密码
|
||||
Recharge int64 `json:"recharge" bson:"recharge"` // 当前充值余额,金币个数
|
||||
RechargeTotal int64 `json:"rechargeTotal" bson:"rechargeTotal"` // 累计充值额,金币个数
|
||||
ConsumptionTotal int64 `json:"consumptionTotal" bson:"consumptionTotal"` // 金币总消费数量
|
||||
ModifyUserNameAt time.Time `json:"modifyUserNameAt" bson:"modifyUserNameAt"` // 上一次修改用户时间
|
||||
Auth bool `json:"auth" bson:"auth"` // 是否认证
|
||||
ConsecutiveLogin int64 `json:"consecutiveLogin" bson:"consecutiveLogin"` // 连续登陆天数
|
||||
DistrictCode string `json:"districtCode" bson:"districtCode"` // 商区码
|
||||
IsDirect bool `json:"isDirect" bson:"isDirect"` // 是否是直推 true:是直推用户
|
||||
IsRegister bool `json:"isRegister" bson:"isRegister"` // 是否已注册
|
||||
Score int64 `json:"score" bson:"score"` // 积分
|
||||
ScoreTotal int64 `json:"scoreTotal" bson:"scoreTotal"` // 累计积分
|
||||
ScoreLvName string `json:"scoreLvName" bson:"scoreLvName"` // 积分等级名称
|
||||
ScoreInvites int64 `json:"scoreInvites" bson:"scoreInvites"` // 邀请人获得的积分累计
|
||||
RegisterTime time.Time `json:"registerTime" bson:"registerTime"` //
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
CurrentVipExpire time.Time `json:"currentVipExpire" bson:"currentVipExpire"` // 当前vip过期时间
|
||||
SysMsgAt time.Time `json:"sysMsgAt" bson:"sysMsgAt"` // 获取系统消息时间
|
||||
ShowCardId int64 `json:"showCardId" bson:"showCardId"` // 最新会员卡id
|
||||
ShowCardName string `json:"showCardName" bson:"showCardName"` // 最新会员卡名称
|
||||
OriVer string `json:"oriVer" bson:"oriVer"` // 注册时的app版本
|
||||
MerchantExpiredAt time.Time `json:"merchantExpiredAt" bson:"merchantExpiredAt"` // 历史遗留字段
|
||||
PrivacyMode bool `json:"privacyMode" bson:"privacyMode"` // 隐私模式:true-开启,false-关闭(默认)
|
||||
|
||||
}
|
||||
|
||||
type UserList []*User
|
||||
type UserMap map[int64]*User
|
||||
|
||||
// CheckCoinNumber 检查金币数量是否足够
|
||||
func (user *User) CheckCoinNumber(needDeduct int64) bool {
|
||||
return user.Recharge >= needDeduct
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Code generated by gen g update. DO NOT EDIT.
|
||||
package usermod
|
||||
|
||||
import (
|
||||
"time"
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
type UserUpdate struct {
|
||||
Type *pubdef.UserType `json:"type" bson:"type,omitempty"` // 类型
|
||||
VipType *pubdef.VipType `json:"vipType" bson:"VipType,omitempty"` // Vip类型
|
||||
Status *pubdef.UserStatus `json:"status" bson:"status,omitempty"` // 状态
|
||||
Region *string `json:"region" bson:"region,omitempty"` // 地区
|
||||
RegIp *string `json:"regIp" bson:"regIp,omitempty"` // 注册IP
|
||||
LastIp *string `json:"lastIp" bson:"lastIp,omitempty"` // 上次登陆IP
|
||||
Backgroud *string `json:"backgroud" bson:"backgroud,omitempty"` // 背景图
|
||||
Avatar *string `json:"avatar" bson:"avatar,omitempty"` // 头像
|
||||
NickName *string `json:"nickName" bson:"nickName,omitempty"` // 昵称
|
||||
Introduction *string `json:"introduction" bson:"introduction,omitempty"` // 简介
|
||||
Gender *int64 `json:"gender" bson:"gender,omitempty"` // 性别 0-未知,1-男,2-女
|
||||
LoginType *pubdef.LoginType `json:"loginType" bson:"loginType,omitempty"` // 登陆方式
|
||||
Age *int64 `json:"age" bson:"age,omitempty"` // 年龄
|
||||
LastLoginAt *time.Time `json:"lastLoginAt" bson:"lastLoginAt,omitempty"` // 上次登陆时间
|
||||
InviteCode *string `json:"inviteCode" bson:"inviteCode,omitempty"` // 邀请码
|
||||
DevID *string `json:"devID" bson:"devID,omitempty"` // 设备唯一标示
|
||||
Mac *string `json:"mac" bson:"mac,omitempty"` // 设备唯一mac地址
|
||||
DevV2 *string `json:"devV2" bson:"devV2,omitempty"` // 设备唯一设备ID
|
||||
Device *string `json:"device" bson:"device,omitempty"` // 设备
|
||||
SysType *string `json:"sysType" bson:"sysType,omitempty"` // 系统类型 ios android
|
||||
AppVer *string `json:"appVer" bson:"appVer,omitempty"` // app版本
|
||||
SessionID *string `json:"sessionId" bson:"sessionId,omitempty"` // 会话ID
|
||||
DeviceBrand *string `json:"deviceBrand" bson:"deviceBrand,omitempty"` // 设备品牌
|
||||
DeviceModel *string `json:"deviceModel" bson:"deviceModel,omitempty"` // 设备型号
|
||||
SystemName *string `json:"systemName" bson:"systemName,omitempty"` // 系统名称
|
||||
SystemVersion *string `json:"systemVersion" bson:"systemVersion,omitempty"` // 系统版本
|
||||
Invites *int64 `json:"invites" bson:"invites,omitempty"` // 我邀请的人的个数(我邀请他人)
|
||||
AgentInviteCode *string `json:"agentInviteCode" bson:"agentInviteCode,omitempty"` // 被邀请的inviteCode
|
||||
Password *string `json:"password" bson:"password,omitempty"` // 登陆密码
|
||||
Recharge *int64 `json:"recharge" bson:"recharge,omitempty"` // 当前充值余额,金币个数
|
||||
RechargeTotal *int64 `json:"rechargeTotal" bson:"rechargeTotal,omitempty"` // 累计充值额,金币个数
|
||||
ConsumptionTotal *int64 `json:"consumptionTotal" bson:"consumptionTotal,omitempty"` // 金币总消费数量
|
||||
ModifyUserNameAt *time.Time `json:"modifyUserNameAt" bson:"modifyUserNameAt,omitempty"` // 上一次修改用户时间
|
||||
Auth *bool `json:"auth" bson:"auth,omitempty"` // 是否认证
|
||||
ConsecutiveLogin *int64 `json:"consecutiveLogin" bson:"consecutiveLogin,omitempty"` // 连续登陆天数
|
||||
DistrictCode *string `json:"districtCode" bson:"districtCode,omitempty"` // 商区码
|
||||
IsDirect *bool `json:"isDirect" bson:"isDirect,omitempty"` // 是否是直推 true:是直推用户
|
||||
IsRegister *bool `json:"isRegister" bson:"isRegister,omitempty"` // 是否已注册
|
||||
Score *int64 `json:"score" bson:"score,omitempty"` // 积分
|
||||
ScoreTotal *int64 `json:"scoreTotal" bson:"scoreTotal,omitempty"` // 累计积分
|
||||
ScoreLvName *string `json:"scoreLvName" bson:"scoreLvName,omitempty"` // 积分等级名称
|
||||
ScoreInvites *int64 `json:"scoreInvites" bson:"scoreInvites,omitempty"` // 邀请人获得的积分累计
|
||||
RegisterTime *time.Time `json:"registerTime" bson:"registerTime,omitempty"` //
|
||||
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` // 更新时间
|
||||
CurrentVipExpire *time.Time `json:"currentVipExpire" bson:"currentVipExpire,omitempty"` // 当前vip过期时间
|
||||
SysMsgAt *time.Time `json:"sysMsgAt" bson:"sysMsgAt,omitempty"` // 获取系统消息时间
|
||||
ShowCardId *int64 `json:"showCardId" bson:"showCardId,omitempty"` // 最新会员卡id
|
||||
ShowCardName *string `json:"showCardName" bson:"showCardName,omitempty"` // 最新会员卡名称
|
||||
OriVer *string `json:"oriVer" bson:"oriVer,omitempty"` // 注册时的app版本
|
||||
AccountNo *string `json:"accountNo" bson:"accountNo,omitempty"` // 账号
|
||||
Email *string `json:"email" bson:"email,omitempty"` // 邮箱
|
||||
MerchantExpiredAt *time.Time `json:"merchantExpiredAt" bson:"merchantExpiredAt,omitempty"` // 商家/兼职有效期
|
||||
}
|
||||
@@ -0,0 +1,427 @@
|
||||
package usermod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
db = mongos.Shop.Coll(TN.User)
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.Status, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.AgentInviteCode, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.InviteCode, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.CreatedAt, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.DevID, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.Mac, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.DevV2, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.Recharge, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.DistrictCode, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.Type, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.NickName, Value: "text"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.Status, Value: 1},
|
||||
{Key: FN.User.Type, Value: 1},
|
||||
},
|
||||
},
|
||||
// statcenter USER_BINDING:isRegister + updatedAt 范围 + sort,避免全表扫描
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.User.IsRegister, Value: 1},
|
||||
{Key: FN.User.UpdatedAt, Value: 1},
|
||||
},
|
||||
Options: options.Index().
|
||||
SetName("isRegister_1_updatedAt_1_bindingSync").
|
||||
SetPartialFilterExpression(bson.M{
|
||||
FN.User.IsRegister: true,
|
||||
FN.User.Email: bson.M{"$exists": true, "$gt": ""},
|
||||
}),
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(User:initUserIndex) Create User Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneUser 插入一个用户
|
||||
func InsertOneUser(ctx context.Context, user *User) *srvdef.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, user); err != nil {
|
||||
log.Warn("User:InsertOneUser db.InsertOne failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneUser 查找一个用户;opts 可用于调用方按需设置 projection,避免读取完整用户文档。
|
||||
func FindOneUser(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (*User, *srvdef.SrvErr) {
|
||||
var userInfo User
|
||||
if err := db.FindOneEx(ctx, &userInfo, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("User:FindOneUser db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrUserNotExsit, "", "")
|
||||
}
|
||||
return &userInfo, nil
|
||||
}
|
||||
|
||||
// DelOneUser 删除一个用户
|
||||
func DelOneUser(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("User:DelOneUser db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneUser 更新一个用户
|
||||
func UpdateOneUser(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateOneUser db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
return srvdef.New(srvdef.ErrUserNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountUsers 统计用户数量;无筛选条件时用 EstimatedCount,避免 user 全表 count 慢查
|
||||
func CountUsers(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
if len(filter) == 0 {
|
||||
count, err := db.EstimatedCount(ctx)
|
||||
if err != nil {
|
||||
log.Warn("User:CountUsers db.EstimatedCount failed", log.E(err))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("User:CountUsers db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateManyUser 更新满足条件的用户
|
||||
func UpdateManyUser(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
_, err := db.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("user:UpdateManyUser db.UpdateMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyUser 查找一批用户数据
|
||||
func FindManyUser(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (UserList, *srvdef.SrvErr) {
|
||||
data := make(UserList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("User:FindManyUser db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FindManyUserWithBind 用自定义结构查找一批用户数据
|
||||
func FindManyUserWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOptions) *srvdef.SrvErr {
|
||||
if err := db.Find(ctx, bind, filter, opts...); err != nil {
|
||||
log.Warn("User:FindManyUserWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateUsers 聚合查找一批用户信息
|
||||
func AggregateUsers(ctx context.Context, bind interface{}, pipeline []bson.M, opts ...*options.AggregateOptions) *srvdef.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline, opts...); err != nil {
|
||||
log.Warn("User:AggregateUsers db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ObjectAggregate 聚合查找
|
||||
func ObjectAggregate(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := db.ObjectAggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("User:ObjectAggregate db.ObjectAggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateRecharge 更新用户充值金额(正数为充值,负数为消费)
|
||||
func UpdateRecharge(ctx context.Context, userId, recharge, rechargeTotal int64) (*User, *srvdef.SrvErr) {
|
||||
userInfo := &User{}
|
||||
incDoc := bson.M{
|
||||
FN.User.Recharge: recharge,
|
||||
FN.User.RechargeTotal: rechargeTotal,
|
||||
}
|
||||
if recharge < 0 {
|
||||
incDoc[FN.User.ConsumptionTotal] = -recharge
|
||||
}
|
||||
err := db.FindOneAndUpdate(ctx, &userInfo, bson.M{
|
||||
FN.User.Id: userId,
|
||||
FN.User.Recharge: bson.M{"$gte": -recharge},
|
||||
}, bson.M{
|
||||
"$inc": incDoc,
|
||||
"$set": bson.M{FN.User.UpdatedAt: time.Now()},
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("UpdateRecharge:FindOneAndUpdate failed", log.E(err))
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "数据更新出错", "")
|
||||
}
|
||||
if userInfo.Id <= 0 {
|
||||
return nil, srvdef.New(srvdef.ErrUserNotExsit, "", "")
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
// UpdateScore 更新用户积分(正数加分,负数扣分)
|
||||
func UpdateScore(ctx context.Context, userId, score int64) (*User, *srvdef.SrvErr) {
|
||||
userInfo := &User{}
|
||||
incDoc := bson.M{FN.User.Score: score}
|
||||
filter := bson.M{FN.User.Id: userId}
|
||||
if score > 0 {
|
||||
incDoc[FN.User.ScoreTotal] = score
|
||||
} else {
|
||||
filter[FN.User.Score] = bson.M{"$gte": -score}
|
||||
}
|
||||
err := db.FindOneAndUpdate(ctx, &userInfo, filter, bson.M{
|
||||
"$inc": incDoc,
|
||||
"$set": bson.M{FN.User.UpdatedAt: time.Now()},
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateScore FindOneAndUpdate failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "数据更新出错", "")
|
||||
}
|
||||
if userInfo.Id <= 0 {
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "为0", "")
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
type OnAddScore func(userId int64, add int64)
|
||||
|
||||
// UpdateScoreWithEvent 更新用户积分并触发回调
|
||||
func UpdateScoreWithEvent(ctx context.Context, userId, score int64, onAdd OnAddScore) (*User, *srvdef.SrvErr) {
|
||||
userInfo := &User{}
|
||||
incDoc := bson.M{FN.User.Score: score}
|
||||
filter := bson.M{FN.User.Id: userId}
|
||||
if score > 0 {
|
||||
incDoc[FN.User.ScoreTotal] = score
|
||||
}
|
||||
err := db.FindOneAndUpdate(ctx, &userInfo, filter, bson.M{
|
||||
"$inc": incDoc,
|
||||
"$set": bson.M{FN.User.UpdatedAt: time.Now()},
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateScoreWithEvent FindOneAndUpdate failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "数据更新出错", "")
|
||||
}
|
||||
if userInfo.Id <= 0 {
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "为0", "")
|
||||
}
|
||||
if score > 0 && onAdd != nil {
|
||||
onAdd(userInfo.Id, score)
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
// UserPay 扣除用户充值金币
|
||||
func UserPay(ctx context.Context, userId int64, amount int64) (*User, *srvdef.SrvErr) {
|
||||
if amount <= 0 {
|
||||
return nil, srvdef.New(srvdef.ErrParamError, "扣除金额不能小于0", "扣除金额不能小于0")
|
||||
}
|
||||
userInfo := &User{}
|
||||
err := db.FindOneAndUpdate(ctx, &userInfo, bson.M{
|
||||
FN.User.Id: userId,
|
||||
FN.User.Recharge: bson.M{"$gte": amount},
|
||||
}, bson.M{
|
||||
"$inc": bson.M{
|
||||
FN.User.Recharge: -amount,
|
||||
FN.User.ConsumptionTotal: amount,
|
||||
},
|
||||
"$set": bson.M{FN.User.UpdatedAt: time.Now()},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "数据更新出错", "")
|
||||
}
|
||||
if userInfo.Id <= 0 {
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "余额不足或用户不存在", "")
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
// FindOneUpdateAndReturnNew 更新用户数据并返回新的用户数据
|
||||
func FindOneUpdateAndReturnNew(ctx context.Context, query bson.M, update bson.M, opts ...*options.FindOneAndUpdateOptions) (*User, *srvdef.SrvErr) {
|
||||
userInfo := &User{}
|
||||
err := db.FindOneAndUpdate(ctx, &userInfo, query, update)
|
||||
if err != nil {
|
||||
return nil, srvdef.New(srvdef.DBUpdate, "数据更新出错", "")
|
||||
}
|
||||
if userInfo.Id <= 0 {
|
||||
return nil, srvdef.New(srvdef.ErrUserNotExsit, "", "")
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
|
||||
// GetMapUsers 获取 map 结构的 users
|
||||
func GetMapUsers(ctx context.Context, ids []int64) (UserMap, *srvdef.SrvErr) {
|
||||
mapUsers := make(UserMap)
|
||||
if len(ids) == 0 {
|
||||
return mapUsers, nil
|
||||
}
|
||||
listUsers, serr := FindManyUser(ctx, bson.M{FN.User.Id: bson.M{"$in": ids}})
|
||||
if serr != nil {
|
||||
return mapUsers, serr
|
||||
}
|
||||
for _, user := range listUsers {
|
||||
mapUsers[user.Id] = user
|
||||
}
|
||||
return mapUsers, nil
|
||||
}
|
||||
|
||||
// FindManyUserByQuery 从 user 表中查找一批数据(自定义 bind)
|
||||
func FindManyUserByQuery(ctx context.Context, bind interface{}, query bson.M, opts ...*options.FindOptions) *srvdef.SrvErr {
|
||||
if err := db.Find(ctx, bind, query, opts...); err != nil {
|
||||
log.Warn("user:FindManyUserByQuery db.Find failed",
|
||||
log.E(err), log.Any("filter", query))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertManyUser 插入一批用户
|
||||
func InsertManyUser(ctx context.Context, users *UserList) *srvdef.SrvErr {
|
||||
if _, err := db.InsertMany(ctx, users); err != nil {
|
||||
log.Warn("User:InsertManyUser db.InsertMany failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StatcenterSyncList 按创建时间增量拉取用户列表(数据统计同步用)
|
||||
func StatcenterSyncList(ctx context.Context, createAt time.Time, size int64) (UserList, error) {
|
||||
opt := (&options.FindOptions{})
|
||||
opt.SetLimit(size)
|
||||
opt.SetSort(bson.M{FN.User.Id: 1})
|
||||
userList := UserList{}
|
||||
err := db.Find(ctx, &userList, bson.M{FN.User.CreatedAt: bson.M{"$gt": createAt}}, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return userList, nil
|
||||
}
|
||||
|
||||
// StatcenterSyncListByUserID 按 UserID 游标拉取注册用户(新统计中心)
|
||||
func StatcenterSyncListByUserID(ctx context.Context, userID int64, size int64) (UserList, error) {
|
||||
opt := (&options.FindOptions{})
|
||||
opt.SetLimit(size)
|
||||
opt.SetSort(bson.D{{Key: FN.User.Id, Value: 1}})
|
||||
filter := bson.M{
|
||||
FN.User.Id: bson.M{"$gt": userID},
|
||||
}
|
||||
userList := UserList{}
|
||||
err := db.Find(ctx, &userList, filter, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return userList, nil
|
||||
}
|
||||
|
||||
// StatcenterSyncBindUserList 增量拉取已正式注册(绑定邮箱)用户
|
||||
func StatcenterSyncBindUserList(ctx context.Context, bindAt time.Time, size int64) (UserList, error) {
|
||||
opt := (&options.FindOptions{}).SetLimit(size).SetSort(bson.M{FN.User.UpdatedAt: 1})
|
||||
filter := bson.M{
|
||||
FN.User.IsRegister: true,
|
||||
FN.User.Email: bson.M{"$exists": true, "$gt": ""},
|
||||
FN.User.UpdatedAt: bson.M{"$gt": bindAt},
|
||||
}
|
||||
userList := UserList{}
|
||||
if err := db.Find(ctx, &userList, filter, opt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return userList, nil
|
||||
}
|
||||
|
||||
// StatcenterSyncInviteList 增量拉取带上级邀请码的用户
|
||||
func StatcenterSyncInviteList(ctx context.Context, inviteAt time.Time, size int64) (UserList, error) {
|
||||
opt := (&options.FindOptions{}).SetLimit(size).SetSort(bson.M{FN.User.CreatedAt: 1})
|
||||
filter := bson.M{
|
||||
FN.User.AgentInviteCode: bson.M{"$nin": []interface{}{"", nil}},
|
||||
FN.User.CreatedAt: bson.M{"$gt": inviteAt},
|
||||
}
|
||||
userList := UserList{}
|
||||
if err := db.Find(ctx, &userList, filter, opt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return userList, nil
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
// Code generated by gen mm. DO NOT EDIT.
|
||||
package usermod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
var (
|
||||
collUser *mongos.Collection
|
||||
)
|
||||
|
||||
type getIndexeser interface {
|
||||
GetIndexes() []mongo.IndexModel
|
||||
}
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
collUser = mongos.Shop.Coll(TN.User)
|
||||
|
||||
|
||||
// create indexes for collection 'user's
|
||||
if v, ok := interface{}(&User{}).(getIndexeser); ok {
|
||||
if indexes := v.GetIndexes(); len(indexes) > 0 {
|
||||
if _, err := collUser.CreateIndex(ctx, indexes); err != nil {
|
||||
log.Fatal("collUser.CreateIndex failed", log.E(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
// InsertOneUser 创建一个User
|
||||
func InsertOneUser(ctx context.Context, data *User) *srvdef.SrvErr {
|
||||
_, err := db.InsertOne(ctx, data)
|
||||
if err != nil {
|
||||
log.Error("usermod:InsertOneUser db.InsertOne failed",
|
||||
log.Any("doc", data),
|
||||
log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// InsertManyUsers 创建多个User
|
||||
func InsertManyUsers(ctx context.Context, data []*User, opts ...*options.InsertManyOptions) *srvdef.SrvErr {
|
||||
_, err := db.InsertMany(ctx, &data)
|
||||
if err != nil {
|
||||
log.Error("usermod:InsertManyUsers db.InsertMany failed",
|
||||
log.Any("doc", data),
|
||||
log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
// FindOneUser 查找一个User
|
||||
func FindOneUser(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (
|
||||
*User, *srvdef.SrvErr) {
|
||||
var data *User
|
||||
if err := db.FindOneEx(ctx, &data, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("usermod:FindOneUser db.FindOneEx failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
*/
|
||||
// FindOneUserById 根据Id查找一个User
|
||||
func FindOneUserById(ctx context.Context, id int64, opts ...*options.FindOneOptions) (
|
||||
*User, *srvdef.SrvErr) {
|
||||
filter := bson.M{
|
||||
FN.User.Id: id,
|
||||
}
|
||||
var data *User
|
||||
if err := db.FindOneEx(ctx, &data, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("usermod:FindOneUser db.FindOneEx failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FindOneUserWithBind 用自定义结构查找一个User
|
||||
func FindOneUserWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOneOptions) *srvdef.SrvErr {
|
||||
if err := db.FindOneEx(ctx, bind, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("usermod:FindOneUserWithBind db.FindOneEx failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneUserAndUpdate 更新查找一个User
|
||||
func FindOneUserAndUpdate(ctx context.Context, filter bson.M, update bson.M, opts ...*options.FindOneAndUpdateOptions) (
|
||||
*User, *srvdef.SrvErr) {
|
||||
var data User
|
||||
err := db.FindOneAndUpdate(ctx, &data, filter, update, opts...)
|
||||
if err != nil {
|
||||
log.Warn("usermod:FindOneUserAndUpdate db.FindOneAndUpdate failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// DelOneUser 删除一个User
|
||||
func DelOneUser(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("usermod:DelOneUser db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// DelOneUserById 根据Id删除一个User
|
||||
func DelOneUserById(ctx context.Context, id int64, opts ...*options.FindOneOptions) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
filter := bson.M{
|
||||
FN.User.Id: id,
|
||||
}
|
||||
return DelOneUser(ctx, filter)
|
||||
}
|
||||
|
||||
// DelManyUsers 删除多个User
|
||||
func DelManyUsers(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteMany(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("usermod:DelManyUsers} db.DeleteMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
} /*
|
||||
// UpdateOneUser 更新一个User
|
||||
func UpdateOneUser(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
_, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateOneUser db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}*/
|
||||
// MustUpdateOneUser 更新一个User
|
||||
func MustUpdateOneUser(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateOneUser db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateOneUserWithResult 更新一个User
|
||||
func UpdateOneUserWithResult(ctx context.Context, filter bson.M, update bson.M, opts ...*options.UpdateOptions) (*mongo.UpdateResult, *srvdef.SrvErr) {
|
||||
result, err := db.UpdateOne(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateOneUserWithResult db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateManyUsers 更新多个User
|
||||
func UpdateManyUsers(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpdateManyUsers db.UpdateMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount <= 0 {
|
||||
log.Warn("usermod:UpdateManyUsers db.UpdateMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// CountUsers 统计User数量
|
||||
func CountUsers(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("usermod:CountUsers db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// FindManyUsers 查找一批User
|
||||
func FindManyUsers(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) ([]*User, *srvdef.SrvErr) {
|
||||
data := make([]*User, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("usermod:FindManyUsers db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FindManyUsers 查找一批User
|
||||
func FindManyUsersAsInt64Map(ctx context.Context, filter bson.M, keyFn func(*User) int64, opts ...*options.FindOptions) (map[int64]*User, *srvdef.SrvErr) {
|
||||
data := make([]*User, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("usermod:FindManyUsers db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
result := make(map[int64]*User)
|
||||
for _, item := range data {
|
||||
key := keyFn(item)
|
||||
if _, ok := result[key]; !ok {
|
||||
result[key] = item
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FindManyUsersWithBind 用自己定义结构查找一批User数据
|
||||
func FindManyUsersWithBind(ctx context.Context, bind interface{}, filter bson.M,
|
||||
opts ...*options.FindOptions) *srvdef.SrvErr {
|
||||
if err := db.Find(ctx, bind, filter, opts...); err != nil {
|
||||
log.Warn("usermod:FindManyUsersWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// AggregateUsers 聚合查找一批User
|
||||
func AggregateUsers(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("usermod:AggregateUsers db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// ObjectAggregateUsers 聚合查找一批User数据
|
||||
func ObjectAggregateUsers(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := db.ObjectAggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("usermod:ObjectAggregateUsers db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BulkWriteUsers 批量写入或者更新User数据
|
||||
func BulkWriteUsers(ctx context.Context,
|
||||
docs []mongo.WriteModel, opts ...*options.BulkWriteOptions) *srvdef.SrvErr {
|
||||
_, err := db.BulkWrite(ctx, docs, opts...)
|
||||
if err != nil {
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpsertOneUser UpsertOneUser
|
||||
func UpsertOneUser(ctx context.Context, filter bson.M, update bson.M) (*mongo.UpdateResult, *srvdef.SrvErr) {
|
||||
result, err := db.UpsertOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("usermod:UpsertOneUser db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// DistinctUser 查找指定字段的不同值
|
||||
func DistinctUser(ctx context.Context, filter bson.M, fieldName string) ([]interface{}, *srvdef.SrvErr) {
|
||||
result, err := db.Distince(ctx, filter, fieldName)
|
||||
if err != nil {
|
||||
log.Warn("usermod:DistinctUser db.Distince failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user