108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package user
|
|
|
|
import (
|
|
"SciencesServer/app/api/enterprise/model"
|
|
"SciencesServer/app/basic/config"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/service"
|
|
config2 "SciencesServer/config"
|
|
"SciencesServer/serve/orm"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type Instance struct{ *service.SessionEnterprise }
|
|
|
|
type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance
|
|
|
|
type (
|
|
// InstanceInfo 基本信息
|
|
InstanceInfo struct {
|
|
Name string `json:"name"` // 名称
|
|
Identity int `json:"identity"` // 具体身份
|
|
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
|
|
}
|
|
// InstanceDetailInfo 详细信息
|
|
InstanceDetailInfo struct {
|
|
InstanceInfo
|
|
Email string `json:"email"` // 邮箱
|
|
Job string `json:"job"` // 职务
|
|
FixedPhone string `json:"fixed_phone"` // 固定电话
|
|
}
|
|
)
|
|
|
|
// Info 基本信息
|
|
func (c *Instance) Info() *InstanceInfo {
|
|
return &InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}
|
|
}
|
|
|
|
// Detail 详细信息
|
|
func (c *Instance) Detail() (*InstanceDetailInfo, error) {
|
|
resp := &InstanceDetailInfo{InstanceInfo: InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}}
|
|
|
|
mUserManage := model.NewUserManage()
|
|
|
|
isExist, err := model2.FirstField(mUserManage.UserManage, []string{"id", "tenant_id", "uuid", "name",
|
|
"email", "job", "fixed_phone", "other"},
|
|
model2.NewWhere("uid", c.UID), model2.NewWhere("identity", c.SelectIdentity))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if isExist {
|
|
resp.Name = mUserManage.Name
|
|
resp.Email = mUserManage.Email
|
|
resp.Job = mUserManage.Job
|
|
resp.FixedPhone = mUserManage.FixedPhone
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// SwitchIdentity 切换身份
|
|
func (c *Instance) SwitchIdentity(identity int) error {
|
|
if _, has := config.TenantUserIdentityData[identity]; !has {
|
|
return errors.New("未知的身份信息")
|
|
}
|
|
if c.SelectIdentity == identity {
|
|
return nil
|
|
}
|
|
c.TenantID, c.ManageUID = 0, 0
|
|
// 已存在相应的身份,更新最后
|
|
if c.Identity&identity > 0 {
|
|
mUserManage := model.NewUserManage()
|
|
// 查询用户身份
|
|
isExist, err := model2.FirstField(mUserManage.UserManage, []string{"id", "tenant_id", "name", "uuid"},
|
|
model2.NewWhere("uid", c.UID), model2.NewWhere("identity", identity))
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if isExist {
|
|
now := time.Now()
|
|
|
|
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = model2.UpdatesWhere(mUserManage.UserManage, map[string]interface{}{
|
|
"is_selected": model2.UserManageSelectedForNo, "updated_at": now,
|
|
}, []*model2.ModelWhere{model2.NewWhere("uid", c.ManageUID)}, tx); err != nil {
|
|
return err
|
|
}
|
|
return model2.Updates(mUserManage.UserManage, map[string]interface{}{
|
|
"is_selected": model2.UserManageSelectedForYes, "updated_at": now,
|
|
}, tx)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
c.TenantID = mUserManage.TenantID
|
|
c.ManageUID = mUserManage.UUID
|
|
}
|
|
c.SelectIdentity = identity
|
|
service.Publish(config2.EventForAccountLoginProduce, config2.RedisKeyForAccount, c.UIDToString(), c.SessionEnterprise)
|
|
return nil
|
|
}
|
|
|
|
func NewInstance() InstanceHandle {
|
|
return func(enterprise *service.SessionEnterprise) *Instance {
|
|
return &Instance{SessionEnterprise: enterprise}
|
|
}
|
|
}
|