249 lines
7.4 KiB
Go
249 lines
7.4 KiB
Go
package user
|
|
|
|
import (
|
|
"SciencesServer/app/api/enterprise/model"
|
|
"SciencesServer/app/basic/config"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/service"
|
|
"SciencesServer/app/session"
|
|
config2 "SciencesServer/config"
|
|
"SciencesServer/serve/orm"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type Identity struct{ *session.Enterprise }
|
|
|
|
type IdentityHandle func(session *session.Enterprise) *Identity
|
|
|
|
type (
|
|
// IdentityForCompany 公司信息
|
|
IdentityForCompany struct {
|
|
ID string `json:"id"`
|
|
*model2.ManageCompany
|
|
Industrys []string `json:"industrys"`
|
|
Keywords []string `json:"keywords"`
|
|
Directions []string `json:"directions"`
|
|
}
|
|
// IdentityForExpert 专家信息
|
|
IdentityForExpert struct {
|
|
ID string `json:"id"`
|
|
*model2.ManageExpert
|
|
Industrys []string `json:"industrys"`
|
|
Keywords []string `json:"keywords"`
|
|
Researchs []string `json:"researchs"`
|
|
}
|
|
// IdentityForResearch 科研机构信息
|
|
IdentityForResearch struct {
|
|
ID string `json:"id"`
|
|
*model2.ManageResearch
|
|
Industrys []string `json:"industrys"`
|
|
Keywords []string `json:"keywords"`
|
|
Researchs []string `json:"researchs"`
|
|
}
|
|
// IdentityForLaboratory 实验室信息
|
|
IdentityForLaboratory struct {
|
|
ID string `json:"id"`
|
|
*model2.ManageLaboratory
|
|
Industrys []string `json:"industrys"`
|
|
Keywords []string `json:"keywords"`
|
|
Researchs []string `json:"researchs"`
|
|
}
|
|
// IdentityForAgent 经纪人信息
|
|
IdentityForAgent struct {
|
|
ID string `json:"id"`
|
|
*model2.ManageAgent
|
|
Industrys []string `json:"industrys"`
|
|
Keywords []string `json:"keywords"`
|
|
IDImage *model2.ManageAgentIDImage `json:"id_images"`
|
|
CredentialImages []string `json:"credential_images"`
|
|
}
|
|
)
|
|
|
|
// company 公司信息
|
|
func (c *Identity) company() (*IdentityForCompany, error) {
|
|
mUserCompany := model.NewUserCompany()
|
|
out, err := mUserCompany.Company(c.UID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if out.UserCompanyID <= 0 {
|
|
return nil, nil
|
|
}
|
|
return &IdentityForCompany{
|
|
ID: (&model2.Model{ID: out.UserCompanyID}).GetEncodeID(),
|
|
ManageCompany: out.ManageCompany,
|
|
Industrys: out.GetIndustryAttribute(),
|
|
Keywords: out.GetKeywordAttribute(),
|
|
Directions: out.GetDirectionAttribute(),
|
|
}, nil
|
|
}
|
|
|
|
func (c *Identity) companyEdit() {
|
|
|
|
}
|
|
|
|
// expert 专家信息
|
|
func (c *Identity) expert() (*IdentityForExpert, error) {
|
|
mUserExpert := model.NewUserExpert()
|
|
out, err := mUserExpert.Expert(c.UID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if out.UserExpertID <= 0 {
|
|
return nil, nil
|
|
}
|
|
return &IdentityForExpert{
|
|
ID: (&model2.Model{ID: out.UserExpertID}).GetEncodeID(),
|
|
ManageExpert: out.ManageExpert,
|
|
Industrys: out.GetIndustryAttribute(),
|
|
Keywords: out.GetKeywordAttribute(),
|
|
Researchs: out.GetResearchAttribute(),
|
|
}, nil
|
|
}
|
|
|
|
// research 科研机构信息
|
|
func (c *Identity) research() (*IdentityForResearch, error) {
|
|
mUserResearch := model.NewUserResearch()
|
|
out, err := mUserResearch.Research(c.UID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if out.UserResearchID <= 0 {
|
|
return nil, nil
|
|
}
|
|
return &IdentityForResearch{
|
|
ID: (&model2.Model{ID: out.UserResearchID}).GetEncodeID(),
|
|
ManageResearch: out.ManageResearch,
|
|
Industrys: out.GetIndustryAttribute(),
|
|
Keywords: out.GetKeywordAttribute(),
|
|
Researchs: out.GetResearchAttribute(),
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Laboratory 实验室信息
|
|
func (c *Identity) laboratory() (*IdentityForLaboratory, error) {
|
|
mUserLaboratory := model.NewUserLaboratory()
|
|
out, err := mUserLaboratory.Laboratory(c.UID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if out.UserLaboratoryID <= 0 {
|
|
return nil, nil
|
|
}
|
|
return &IdentityForLaboratory{
|
|
ID: (&model2.Model{ID: out.UserLaboratoryID}).GetEncodeID(),
|
|
ManageLaboratory: out.ManageLaboratory,
|
|
Industrys: out.GetIndustryAttribute(),
|
|
Keywords: out.GetKeywordAttribute(),
|
|
Researchs: out.GetResearchAttribute(),
|
|
}, nil
|
|
}
|
|
|
|
// agent 经纪人信息
|
|
func (c *Identity) agent() (*IdentityForAgent, error) {
|
|
mUserAgent := model.NewUserAgent()
|
|
out, err := mUserAgent.Agent(c.UID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if out.UserAgentID <= 0 {
|
|
return nil, nil
|
|
}
|
|
return &IdentityForAgent{
|
|
ID: (&model2.Model{ID: out.UserAgentID}).GetEncodeID(),
|
|
ManageAgent: out.ManageAgent,
|
|
Industrys: out.GetIndustryAttribute(),
|
|
Keywords: out.GetKeywordAttribute(),
|
|
IDImage: out.GetIDImageAttribute(),
|
|
CredentialImages: out.GetCredentialImageAttribute(),
|
|
}, nil
|
|
}
|
|
|
|
// Basic 详细信息
|
|
func (c *Identity) Basic() (*InstanceDetailInfo, error) {
|
|
resp := &InstanceDetailInfo{InstanceInfo: InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}}
|
|
|
|
mUserIdentity := model.NewUserIdentity()
|
|
|
|
isExist, err := model2.FirstField(mUserIdentity.UserIdentity, []string{"id", "tenant_id", "uuid", "name",
|
|
"email", "job", "fixed_phone"},
|
|
model2.NewWhere("uid", c.UID), model2.NewWhere("identity", c.SelectIdentity))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !!isExist {
|
|
return nil, errors.New("操作错误,用户身份信息不存在或已被删除")
|
|
}
|
|
resp.Name = mUserIdentity.Name
|
|
resp.Email = mUserIdentity.Email
|
|
resp.Job = mUserIdentity.Job
|
|
resp.FixedPhone = mUserIdentity.FixedPhone
|
|
return resp, nil
|
|
}
|
|
|
|
// Detail 详细信息
|
|
func (c *Identity) Detail() (interface{}, error) {
|
|
if c.Identity == config.TenantUserIdentityForCompany { // 公司企业身份
|
|
return c.company()
|
|
} else if c.Identity == config.TenantUserIdentityForExpert { // 专家身份
|
|
return c.expert()
|
|
} else if c.Identity == config.TenantUserIdentityForResearch { // 科研机构身份
|
|
return c.research()
|
|
} else if c.Identity == config.TenantUserIdentityForLaboratory { // 实验室身份
|
|
return c.laboratory()
|
|
} else if c.Identity == config.TenantUserIdentityForAgent { // 经纪人身份
|
|
return c.agent()
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// Switch 切换身份
|
|
func (c *Identity) Switch(identity int) error {
|
|
if _, has := config.TenantUserIdentityData[identity]; !has {
|
|
return errors.New("未知的身份信息")
|
|
}
|
|
if c.SelectIdentity == identity {
|
|
return nil
|
|
}
|
|
c.IdentityUID = 0
|
|
// 已存在相应的身份,更新最后
|
|
if c.Identity&identity > 0 {
|
|
mUserIdentity := model.NewUserIdentity()
|
|
// 查询用户身份
|
|
isExist, err := model2.FirstField(mUserIdentity.UserIdentity, []string{"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(mUserIdentity.UserIdentity, map[string]interface{}{
|
|
"is_selected": model2.UserIdentitySelectedForNo, "updated_at": now,
|
|
}, []*model2.ModelWhere{model2.NewWhere("uuid", c.IdentityUID)}, tx); err != nil {
|
|
return err
|
|
}
|
|
return model2.Updates(mUserIdentity.UserIdentity, map[string]interface{}{
|
|
"is_selected": model2.UserIdentitySelectedForYes, "updated_at": now,
|
|
}, tx)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
c.SelectIdentity = identity
|
|
service.Publish(config2.EventForAccountLoginProduce, config2.RedisKeyForAccountEnterprise, c.GetStringUID(), c.Enterprise)
|
|
return nil
|
|
}
|
|
return errors.New("操作错误,无效的身份信息")
|
|
}
|
|
|
|
func NewIdentity() IdentityHandle {
|
|
return func(session *session.Enterprise) *Identity {
|
|
return &Identity{Enterprise: session}
|
|
}
|
|
}
|