227 lines
6.5 KiB
Go
227 lines
6.5 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"
|
|
"SciencesServer/utils"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type Examine struct {
|
|
*session.Admin
|
|
}
|
|
|
|
type ExamineHandle func(session *session.Admin) *Examine
|
|
|
|
type ExamineManageInfo struct {
|
|
IModel model2.IModel
|
|
UIDs []uint64 // 用户表UUID
|
|
}
|
|
|
|
// examineHandle 审核处理
|
|
var examineHandle = map[int]func(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.FirstField(IModel, []string{"id", "examine_status"}); err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("数据信息不存在")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func examineCompany(id uint64) (*ExamineManageInfo, error) {
|
|
mManageCompany := model.NewManageCompany()
|
|
|
|
err := checkManage(mManageCompany.ManageCompany, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} 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, UIDs: uids}, nil
|
|
}
|
|
|
|
func examineExpert(id uint64) (*ExamineManageInfo, error) {
|
|
mManageExpert := model.NewManageExpert()
|
|
|
|
err := checkManage(mManageExpert.ManageExpert, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} 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}, nil
|
|
}
|
|
|
|
func examineResearch(id uint64) (*ExamineManageInfo, error) {
|
|
mManageResearch := model.NewManageResearch()
|
|
|
|
err := checkManage(mManageResearch.ManageResearch, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} 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}, nil
|
|
}
|
|
|
|
func examineLaboratory(id uint64) (*ExamineManageInfo, error) {
|
|
mManageLaboratory := model.NewManageLaboratory()
|
|
|
|
err := checkManage(mManageLaboratory.ManageLaboratory, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} 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}, nil
|
|
}
|
|
|
|
func examineAgent(id uint64) (*ExamineManageInfo, error) {
|
|
mManageAgent := model.NewManageAgent()
|
|
|
|
err := checkManage(mManageAgent.ManageAgent, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} 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}, nil
|
|
}
|
|
|
|
// Launch 发起审核
|
|
func (c *Examine) Launch(id uint64, identity, status int) 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)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = model2.Updates(data.IModel, map[string]interface{}{
|
|
"examine_status": status, "updated_at": time.Now(),
|
|
}, tx); err != nil {
|
|
return err
|
|
}
|
|
// 拒绝后,不执行以下数据
|
|
if _status == model2.ExamineStatusForRefuse {
|
|
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
|
|
}
|
|
|
|
mUserIdentity := model.NewUserIdentity()
|
|
|
|
identitys := make([]*model2.UserIdentity, 0)
|
|
|
|
now := time.Now()
|
|
|
|
snowflake, _ := utils.NewSnowflake(1)
|
|
|
|
for _, v := range users {
|
|
identitys = append(identitys, &model2.UserIdentity{
|
|
UUID: uint64(snowflake.GetID()),
|
|
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
|
|
}
|
|
}
|
|
if len(identitys) > 0 {
|
|
if err = model2.Creates(mUserIdentity.UserIdentity, identity, tx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func NewExamine() ExamineHandle {
|
|
return func(session *session.Admin) *Examine {
|
|
return &Examine{
|
|
Admin: session,
|
|
}
|
|
}
|
|
}
|