140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"gateway/internal/otelTrace"
|
|
|
|
"time"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
USERINFO = "user_info"
|
|
)
|
|
|
|
type UserInfo struct {
|
|
Id int
|
|
UserId string
|
|
Passwd string
|
|
Nick string
|
|
Remark string
|
|
Ip string
|
|
Status string
|
|
Role string
|
|
RoleName string
|
|
CreateTime time.Time
|
|
UpdateTime time.Time
|
|
}
|
|
|
|
func GetUserInfoByUserID(ctx context.Context, userID string) UserInfo {
|
|
o := orm.NewOrm()
|
|
var userInfo UserInfo
|
|
err := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("user_id", userID).One(&userInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get user info fail: ", zap.Error(err))
|
|
}
|
|
return userInfo
|
|
}
|
|
|
|
func GetOperatorByMap(ctx context.Context, params map[string]string, displayCount, offset int) []UserInfo {
|
|
o := orm.NewOrm()
|
|
var userInfo []UserInfo
|
|
qs := o.QueryTable(USERINFO)
|
|
for k, v := range params {
|
|
if len(v) > 0 {
|
|
qs = qs.Filter(k, v)
|
|
}
|
|
}
|
|
_, err := qs.Exclude("status", "delete").Limit(displayCount, offset).OrderBy("-update_time").All(&userInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get operator by map fail: ", zap.Error(err))
|
|
}
|
|
return userInfo
|
|
}
|
|
|
|
func GetOperatorLenByMap(ctx context.Context, params map[string]string) int {
|
|
o := orm.NewOrm()
|
|
qs := o.QueryTable(USERINFO)
|
|
for k, v := range params {
|
|
qs = qs.Filter(k, v)
|
|
}
|
|
cnt, err := qs.Exclude("status", "delete").Count()
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get operator len by map fail: ", zap.Error(err))
|
|
}
|
|
return int(cnt)
|
|
}
|
|
|
|
func UpdateUserInfoIP(ctx context.Context, userInfo UserInfo) {
|
|
o := orm.NewOrm()
|
|
num, err := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("user_id", userInfo.UserId).Update(orm.Params{"ip": userInfo.Ip})
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("%s update user info ip fail: %v", zap.String("UserId", userInfo.UserId), zap.Error(err))
|
|
} else {
|
|
otelTrace.Logger.WithContext(ctx).Info("%s update user info ip success, num: %d", zap.String("UserId", userInfo.UserId), zap.Int64("num", num))
|
|
}
|
|
}
|
|
|
|
func UpdateUserInfoPassword(ctx context.Context, userInfo UserInfo) {
|
|
o := orm.NewOrm()
|
|
num, err := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("user_id", userInfo.UserId).Update(orm.Params{"passwd": userInfo.Passwd})
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("%s update user info password fail: %v", zap.String("UserId", userInfo.UserId), zap.Error(err))
|
|
} else {
|
|
otelTrace.Logger.WithContext(ctx).Info("%s update user info password success, update num: %d", zap.String("UserId", userInfo.UserId), zap.Int64("num", num))
|
|
}
|
|
}
|
|
|
|
func UpdateUserInfo(ctx context.Context, userInfo UserInfo) {
|
|
o := orm.NewOrm()
|
|
if num, err := o.Update(&userInfo); err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("update user info fail: ", zap.Error(err))
|
|
} else {
|
|
otelTrace.Logger.WithContext(ctx).Info("update user info success, num: ", zap.Int64("num", num))
|
|
}
|
|
}
|
|
|
|
func UpdateStauts(ctx context.Context, status, userId string) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.QueryTable(USERINFO).Filter("user_id", userId).Update(orm.Params{"status": status})
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("update status fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func UserInfoExistByUserId(userId string) bool {
|
|
o := orm.NewOrm()
|
|
exist := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("user_id", userId).Exist()
|
|
return exist
|
|
}
|
|
|
|
func NickIsExist(nick string) bool {
|
|
o := orm.NewOrm()
|
|
exist := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("nick", nick).Exist()
|
|
return exist
|
|
}
|
|
|
|
func InsertUser(ctx context.Context, userInfo UserInfo) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.Insert(&userInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("insert user fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func DeleteUserByUserId(ctx context.Context, userId string) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.QueryTable(USERINFO).Exclude("status", "delete").Filter("user_id", userId).Update(orm.Params{"status": "delete"})
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("delete user by userId fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
return true
|
|
}
|