From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 01 十一月 2023 19:20:21 +0800 Subject: [PATCH] 公海member_id使用0 --- model/user.go | 149 ++++++++++++++++++++++++++++++------------------- 1 files changed, 90 insertions(+), 59 deletions(-) diff --git a/model/user.go b/model/user.go index 529e9bd..44f7fdc 100644 --- a/model/user.go +++ b/model/user.go @@ -5,37 +5,29 @@ "aps_crm/pkg/mysqlx" "fmt" "gorm.io/gorm" - "time" ) type ( // User token閲岃竟鎶婄敤鎴稩D銆佺埗鐢ㄦ埛ID銆佽鑹查兘甯︿笂 User struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(255);comment:鐢ㄦ埛ID"` - Username string `json:"username" gorm:"index;type:varchar(255);comment:鐢ㄦ埛鐧诲綍鍚�"` - UserType constvar.UserType `json:"userType" gorm:"type:int(11);comment:鐢ㄦ埛绫诲瀷 1瓒呯骇绠$悊鍛� 2涓昏处鎴� 3瀛愯处鎴�"` - Password string `json:"-" gorm:"type:varchar(255);comment:鐢ㄦ埛鐧诲綍瀵嗙爜"` - NickName string `json:"nickName" gorm:"type:varchar(255);default:绯荤粺鐢ㄦ埛;comment:鐢ㄦ埛鏄电О"` - HeaderImg string `json:"headerImg" gorm:"type:varchar(255);comment:鐢ㄦ埛澶村儚"` - Phone string `json:"phone" gorm:"type:varchar(255);comment:鐢ㄦ埛鎵嬫満鍙�"` - Email string `json:"email" gorm:"type:varchar(255);comment:鐢ㄦ埛閭"` - Enable bool `json:"enable" gorm:"type:tinyint(1);comment:鐢ㄦ埛鏄惁琚喕缁�"` - ParentId string `json:"parentId" gorm:"type:varchar(255);comment:鐖剁敤鎴稩D"` - ParentName string `json:"parentName" gorm:"type:varchar(255);comment:鐖剁敤鎴峰悕绉�"` - CompanyName string `json:"companyName" gorm:"type:varchar(255);comment:鍏徃鍚嶇О"` - Pos string `json:"pos" gorm:"type:varchar(255);comment:宀椾綅"` - ModifiedPwd bool `json:"-" gorm:"type:tinyint(1);comment:鏄惁鏀硅繃瀵嗙爜"` - CreateTime int64 `json:"-" gorm:"type:bigint(20);comment:鍒涘缓鏃堕棿"` - UpdateTime int64 `json:"-" gorm:"type:bigint(20);comment:鏇存柊鏃堕棿"` - Menus []Menu `json:"-" gorm:"many2many:user_menu;"` // 鐢ㄦ埛鑿滃崟 - CreateAt string `json:"createAt" gorm:"-"` // 鍒涘缓鏃堕棿 - UpdateAt string `json:"updateAt" gorm:"-"` // 鏇存柊鏃堕棿 - MenuIds []uint `json:"menuIds" gorm:"-"` // 鑿滃崟ID鍒楄〃 - Ip string `json:"ip" gorm:"type:varchar(255);comment:闆嗙兢Ip"` - Port string `json:"port" gorm:"type:varchar(255);comment:绔彛鍙�"` - Status int `json:"status" gorm:"type:int(11);comment:鐢ㄦ埛瀹℃牳鐘舵�� 0:绂佺敤; 1:姝e父; 2:瀹℃牳涓�"` - AuthorityId uint `json:"authorityId" gorm:"default:888;comment:鐢ㄦ埛瑙掕壊ID"` // 鐢ㄦ埛瑙掕壊ID - Authority Authority `json:"authority" gorm:"foreignKey:AuthorityId"` + ID int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + UUID string `json:"uuid" gorm:"uniqueIndex;type:varchar(255);comment:鐢ㄦ埛ID"` + Username string `json:"username" gorm:"index;type:varchar(255);comment:鐢ㄦ埛鐧诲綍鍚�"` + UserType constvar.UserType `json:"userType" gorm:"type:int(11);comment:鐢ㄦ埛绫诲瀷 1瓒呯骇绠$悊鍛� 2涓昏处鎴� 3瀛愯处鎴�"` + Password string `json:"-" gorm:"type:varchar(255);comment:鐢ㄦ埛鐧诲綍瀵嗙爜"` + NickName string `json:"nickName" gorm:"type:varchar(255);default:绯荤粺鐢ㄦ埛;comment:鐢ㄦ埛鏄电О"` + RealName string `json:"realName" gorm:"type:varchar(255);comment:鐢ㄦ埛鐪熷疄濮撳悕"` + DepartmentId int `json:"departmentId" gorm:"type:int(11);comment:鐢ㄦ埛閮ㄩ棬ID"` + HeaderImg string `json:"headerImg" gorm:"type:varchar(255);comment:鐢ㄦ埛澶村儚"` + Phone string `json:"phone" gorm:"type:varchar(255);comment:鐢ㄦ埛鎵嬫満鍙�"` + Email string `json:"email" gorm:"type:varchar(255);comment:鐢ㄦ埛閭"` + Pos string `json:"pos" gorm:"type:varchar(255);comment:宀椾綅"` + Menus []Menu `json:"-" gorm:"many2many:user_menu;"` // 鐢ㄦ埛鑿滃崟 + MenuIds []uint `json:"menuIds" gorm:"-"` // 鑿滃崟ID鍒楄〃 + AuthorityId uint `json:"authorityId" gorm:"default:888;comment:鐢ㄦ埛瑙掕壊ID"` // 鐢ㄦ埛瑙掕壊ID + Authority Authority `json:"authority" gorm:"foreignKey:AuthorityId"` + SubUserIds string `json:"subUserIds" gorm:"sub_user_ids"` //涓嬪睘鍛樺伐鐢ㄦ埛id,鐢ㄩ�楀彿鍒嗗紑 + gorm.Model `json:"-"` } UserSearch struct { @@ -45,33 +37,12 @@ PageNum int PageSize int Orm *gorm.DB + IDs []int } ) func (slf User) TableName() string { return "user" -} - -func (slf *User) BeforeCreate(tx *gorm.DB) error { - slf.CreateTime = time.Now().Unix() - slf.UpdateTime = slf.CreateTime - return nil -} - -func (slf *User) BeforeSave(tx *gorm.DB) error { - slf.UpdateTime = time.Now().Unix() - return nil -} - -func (slf *User) BeforeUpdate(tx *gorm.DB) error { - slf.UpdateTime = time.Now().Unix() - return nil -} - -func (slf *User) AfterFind(tx *gorm.DB) error { - slf.CreateAt = time.Unix(slf.CreateTime, 0).Format("2006-01-02 15:04:05") - slf.UpdateAt = time.Unix(slf.UpdateTime, 0).Format("2006-01-02 15:04:05") - return nil } func NewUserSearch(db *gorm.DB) *UserSearch { @@ -97,17 +68,17 @@ } func (slf *UserSearch) SetId(id string) *UserSearch { - slf.ID = id + slf.UUID = id return slf } -func (slf *UserSearch) SetParentId(parentId string) *UserSearch { - slf.ParentId = parentId +func (slf *UserSearch) SetUserId(userId int) *UserSearch { + slf.ID = userId return slf } -func (slf *UserSearch) SetParentName(parentName string) *UserSearch { - slf.ParentName = parentName +func (slf *UserSearch) SetUserIds(userIds []int) *UserSearch { + slf.IDs = userIds return slf } @@ -124,16 +95,12 @@ func (slf *UserSearch) build() *gorm.DB { var db = slf.Orm.Model(&User{}).Preload("Menus") - if slf.ID != "" { - db = db.Where("id = ?", slf.ID) + if slf.UUID != "" { + db = db.Where("uuid = ?", slf.UUID) } if slf.Username != "" { db = db.Where("username = ?", slf.Username) - } - - if slf.ParentName != "" { - db = db.Where("parent_name = ?", slf.ParentName) } if slf.Keyword != "" { @@ -142,6 +109,14 @@ if slf.Order != "" { db = db.Order(slf.Order) + } + + if slf.ID != 0 { + db = db.Where("id = ?", slf.ID) + } + + if len(slf.IDs) > 0 { + db = db.Where("id in ?", slf.IDs) } return db @@ -303,3 +278,59 @@ func (slf *UserSearch) ReplaceMenu(user *User, menus []*Menu) error { return slf.Orm.Model(user).Association("Menus").Replace(menus) } + +func (slf *UserSearch) FindAll() ([]*User, error) { + var ( + records = make([]*User, 0) + db = slf.build() + ) + + if err := db.Find(&records).Error; err != nil { + return records, fmt.Errorf("find all err: %v", err) + } + + return records, nil +} + +func (slf *UserSearch) Upsert(record User, SubUserQueried bool) error { + + var db = slf.SetId(record.UUID).build() + old, err := slf.First() + if err == gorm.ErrRecordNotFound { + if err := db.Create(&record).Error; err != nil { + return fmt.Errorf("create user err: %v, record: %+v", err, record) + } + } else if old.Username != record.Username || + old.UserType != record.UserType || + old.NickName != record.NickName || + SubUserQueried && old.SubUserIds != record.SubUserIds { + old.Username = record.Username + old.UserType = record.UserType + old.NickName = record.NickName + old.SubUserIds = record.SubUserIds + if err := db.Save(&old).Error; err != nil { + return fmt.Errorf("update user err: %v, old record: %+v", err, old) + } + } + + return nil +} + +type IdPair struct { + ID int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + UUID string `json:"uuid" gorm:"unique;type:varchar(255);comment:鐢ㄦ埛ID"` +} + +func (slf *UserSearch) UUID2CrmUserId(userIds []string) ([]*IdPair, error) { + var ( + records = make([]*IdPair, 0) + db = slf.Orm.Model(&User{}) + ) + + db = db.Where("uuid in ?", userIds) + if err := db.Select("user.id, user.uuid").Find(&records).Error; err != nil { + return records, fmt.Errorf("find all err: %v", err) + } + + return records, nil +} -- Gitblit v1.8.0