262 lines
8.8 KiB
Go
262 lines
8.8 KiB
Go
package manage
|
||
|
||
import (
|
||
"SciencesServer/app/api/admin/model"
|
||
"SciencesServer/app/basic/config"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/serve/orm"
|
||
"errors"
|
||
"gorm.io/gorm"
|
||
"time"
|
||
)
|
||
|
||
type Examine struct {
|
||
*session.Admin
|
||
}
|
||
|
||
type ExamineHandle func(session *session.Admin) *Examine
|
||
|
||
type ExamineManageInfo struct {
|
||
IModel model2.IModel
|
||
IUserModel model2.IModel
|
||
UIDs []uint64 // 用户表UUID
|
||
Name string
|
||
Keywords, Industrys, Researchs []string
|
||
}
|
||
|
||
// examineHandle 审核处理
|
||
var examineHandle = map[int]func(id, tenantID uint64) (*ExamineManageInfo, error){
|
||
config.TenantUserIdentityForCompany: examineCompany,
|
||
config.TenantUserIdentityForExpert: examineExpert,
|
||
config.TenantUserIdentityForResearch: examineResearch,
|
||
config.TenantUserIdentityForLaboratory: examineLaboratory,
|
||
}
|
||
|
||
func checkManage(IModel model2.IModel, id uint64) error {
|
||
IModel.SetID(id)
|
||
|
||
if isExist, err := model2.First(IModel); err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,数据信息不存在")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func examineCompany(id, tenantID uint64) (*ExamineManageInfo, error) {
|
||
mManageCompany := model.NewManageCompany()
|
||
|
||
err := checkManage(mManageCompany.ManageCompany, id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if tenantID > 0 && mManageCompany.TenantID != tenantID {
|
||
return nil, errors.New("操作错误,无权限操作")
|
||
} else if mManageCompany.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
|
||
return nil, errors.New("操作错误,当前入驻信息已审核")
|
||
}
|
||
mUserCompany := model.NewUserCompany()
|
||
uids := make([]uint64, 0)
|
||
|
||
if err = model2.Pluck(mUserCompany.UserCompany, "uid", &uids, model2.NewWhere("company_id", id),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
|
||
return nil, err
|
||
}
|
||
return &ExamineManageInfo{IModel: mManageCompany.ManageCompany, IUserModel: mUserCompany.UserCompany,
|
||
UIDs: uids, Name: mManageCompany.Name, Industrys: mManageCompany.GetIndustryAttribute(),
|
||
Keywords: mManageCompany.GetKeywordAttribute()}, nil
|
||
}
|
||
|
||
func examineExpert(id, tenantID uint64) (*ExamineManageInfo, error) {
|
||
mManageExpert := model.NewManageExpert()
|
||
|
||
err := checkManage(mManageExpert.ManageExpert, id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if tenantID > 0 && mManageExpert.TenantID != tenantID {
|
||
return nil, errors.New("操作错误,无权限操作")
|
||
} else if mManageExpert.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
|
||
return nil, errors.New("操作错误,当前入驻信息已审核")
|
||
}
|
||
mUserExpert := model.NewUserExpert()
|
||
uids := make([]uint64, 0)
|
||
|
||
if err = model2.Pluck(mUserExpert.UserExpert, "uid", &uids, model2.NewWhere("expert_id", id),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
|
||
return nil, err
|
||
}
|
||
return &ExamineManageInfo{IModel: mManageExpert.ManageExpert, UIDs: uids,
|
||
Name: mManageExpert.Name, Industrys: mManageExpert.GetIndustryAttribute(),
|
||
Keywords: mManageExpert.GetKeywordAttribute(), Researchs: mManageExpert.GetResearchAttribute()}, nil
|
||
}
|
||
|
||
func examineResearch(id, tenantID uint64) (*ExamineManageInfo, error) {
|
||
mManageResearch := model.NewManageResearch()
|
||
|
||
err := checkManage(mManageResearch.ManageResearch, id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if tenantID > 0 && mManageResearch.TenantID != tenantID {
|
||
return nil, errors.New("操作错误,无权限操作")
|
||
} else if mManageResearch.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
|
||
return nil, errors.New("操作错误,当前入驻信息已审核")
|
||
}
|
||
|
||
mUserResearch := model.NewUserResearch()
|
||
uids := make([]uint64, 0)
|
||
|
||
if err = model2.Pluck(mUserResearch.UserResearch, "uid", &uids, model2.NewWhere("research_id", id),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
|
||
return nil, err
|
||
}
|
||
return &ExamineManageInfo{IModel: mManageResearch.ManageResearch, UIDs: uids,
|
||
Name: mManageResearch.Name, Industrys: mManageResearch.GetIndustryAttribute(),
|
||
Keywords: mManageResearch.GetKeywordAttribute(), Researchs: mManageResearch.GetResearchAttribute()}, nil
|
||
}
|
||
|
||
func examineLaboratory(id, tenantID uint64) (*ExamineManageInfo, error) {
|
||
mManageLaboratory := model.NewManageLaboratory()
|
||
|
||
err := checkManage(mManageLaboratory.ManageLaboratory, id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if tenantID > 0 && mManageLaboratory.TenantID != tenantID {
|
||
return nil, errors.New("操作错误,无权限操作")
|
||
} else if mManageLaboratory.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
|
||
return nil, errors.New("操作错误,当前入驻信息已审核")
|
||
}
|
||
|
||
mUserLaboratory := model.NewUserLaboratory()
|
||
uids := make([]uint64, 0)
|
||
|
||
if err = model2.Pluck(mUserLaboratory.UserLaboratory, "uid", &uids, model2.NewWhere("laboratory_id", id),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
|
||
return nil, err
|
||
}
|
||
return &ExamineManageInfo{IModel: mManageLaboratory.ManageLaboratory, UIDs: uids,
|
||
Name: mManageLaboratory.Name, Industrys: mManageLaboratory.GetIndustryAttribute(),
|
||
Keywords: mManageLaboratory.GetKeywordAttribute(), Researchs: mManageLaboratory.GetResearchAttribute()}, nil
|
||
}
|
||
|
||
func examineAgent(id, tenantID uint64) (*ExamineManageInfo, error) {
|
||
mManageAgent := model.NewManageAgent()
|
||
|
||
err := checkManage(mManageAgent.ManageAgent, id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if tenantID > 0 && mManageAgent.TenantID != tenantID {
|
||
return nil, errors.New("操作错误,无权限操作")
|
||
} else if mManageAgent.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
|
||
return nil, errors.New("操作错误,当前入驻信息已审核")
|
||
}
|
||
|
||
mUserAgent := model.NewUserAgent()
|
||
uids := make([]uint64, 0)
|
||
|
||
if err = model2.Pluck(mUserAgent.UserAgent, "uid", &uids, model2.NewWhere("laboratory_id", id),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
|
||
return nil, err
|
||
}
|
||
return &ExamineManageInfo{IModel: mManageAgent.ManageAgent, UIDs: uids,
|
||
Name: mManageAgent.Name, Industrys: mManageAgent.GetIndustryAttribute(),
|
||
Keywords: mManageAgent.GetKeywordAttribute()}, nil
|
||
}
|
||
|
||
// Launch 发起审核
|
||
func (c *Examine) Launch(id uint64, identity, status int, remark string, params map[string]interface{}) error {
|
||
_status := model2.ExamineStatusKind(status)
|
||
|
||
if _status != model2.ExamineStatusForRefuse && _status != model2.ExamineStatusForAgree {
|
||
return errors.New("操作错误,未知的审核模式")
|
||
}
|
||
handle, has := examineHandle[identity]
|
||
|
||
if !has {
|
||
return errors.New("操作错误,未知的身份信息")
|
||
}
|
||
data, err := handle(id, c.TenantID)
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
values := map[string]interface{}{
|
||
"examine_status": status,
|
||
"examine_remark": remark, "updated_at": time.Now()}
|
||
|
||
if params != nil && len(params) > 0 {
|
||
if identity == config.TenantUserIdentityForCompany {
|
||
for k, v := range params {
|
||
values[k] = v
|
||
}
|
||
}
|
||
}
|
||
if err = model2.Updates(data.IModel, values, tx); err != nil {
|
||
return err
|
||
}
|
||
// 拒绝后,不执行以下数据
|
||
if _status == model2.ExamineStatusForRefuse {
|
||
// 清除所有的入驻信息变为失效
|
||
if len(data.UIDs) > 0 {
|
||
if err = model2.UpdatesWhere(data.IUserModel, map[string]interface{}{
|
||
"invalid_status": model2.InvalidStatusForYes, "updated_at": time.Now(),
|
||
}, []*model2.ModelWhere{model2.NewWhere("uid", data.UIDs),
|
||
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)}, tx); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
mUserInstance := model.NewUserInstance()
|
||
|
||
users := make([]*model2.UserInstance, 0)
|
||
|
||
if err = model2.ScanFields(mUserInstance.UserInstance, &users, []string{"id", "uuid", "identity"},
|
||
&model2.ModelWhereOrder{Where: model2.NewWhereIn("uuid", data.UIDs)}); err != nil {
|
||
return err
|
||
}
|
||
userIdentitys := make([]*model2.UserIdentity, 0)
|
||
|
||
now := time.Now()
|
||
|
||
for _, v := range users {
|
||
userIdentitys = append(userIdentitys, &model2.UserIdentity{
|
||
UID: v.UUID,
|
||
Identity: identity,
|
||
ModelAt: model2.ModelAt{CreatedAt: now, UpdatedAt: now},
|
||
})
|
||
if err = model2.UpdatesWhere(mUserInstance.UserInstance, map[string]interface{}{
|
||
"identity": mUserInstance.Identity | identity, "updated_at": now,
|
||
}, []*model2.ModelWhere{
|
||
model2.NewWhere("id", v.ID),
|
||
}, tx); err != nil {
|
||
return err
|
||
}
|
||
// TODO:需要同步redis
|
||
}
|
||
if len(userIdentitys) > 0 {
|
||
if err = model2.Creates(model.NewUserIdentity().UserIdentity, userIdentitys, tx); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
// 存放es中
|
||
(&ESParams{
|
||
ID: data.IModel.GetID(),
|
||
Identity: identity, Name: data.Name, Keywords: data.Keywords,
|
||
Industrys: data.Industrys, Researchs: data.Researchs,
|
||
}).Set()
|
||
return nil
|
||
})
|
||
}
|
||
|
||
func NewExamine() ExamineHandle {
|
||
return func(session *session.Admin) *Examine {
|
||
return &Examine{Admin: session}
|
||
}
|
||
}
|