Files

228 lines
7.1 KiB
Go
Raw Normal View History

2021-11-01 11:19:49 +08:00
package manage
import (
2022-01-05 11:29:27 +08:00
"SciencesServer/app/api/admin/model"
2021-11-01 11:19:49 +08:00
"SciencesServer/app/basic/config"
model2 "SciencesServer/app/common/model"
2022-01-06 22:02:09 +08:00
"SciencesServer/app/session"
2021-11-01 11:19:49 +08:00
"SciencesServer/serve/orm"
"errors"
"gorm.io/gorm"
"time"
)
type Examine struct {
2022-01-06 22:02:09 +08:00
*session.Admin
2021-11-01 11:19:49 +08:00
}
2022-01-06 22:02:09 +08:00
type ExamineHandle func(session *session.Admin) *Examine
2021-11-01 11:19:49 +08:00
type ExamineManageInfo struct {
2021-12-03 10:08:23 +08:00
IModel model2.IModel
UIDs []uint64 // 用户表UUID
2021-11-01 11:19:49 +08:00
}
// examineHandle 审核处理
2022-01-14 17:09:06 +08:00
var examineHandle = map[int]func(id, tenantID uint64) (*ExamineManageInfo, error){
2021-11-24 09:59:29 +08:00
config.TenantUserIdentityForCompany: examineCompany,
config.TenantUserIdentityForExpert: examineExpert,
config.TenantUserIdentityForResearch: examineResearch,
config.TenantUserIdentityForLaboratory: examineLaboratory,
2021-11-01 11:19:49 +08:00
}
func checkManage(IModel model2.IModel, id uint64) error {
IModel.SetID(id)
2022-01-14 17:09:06 +08:00
if isExist, err := model2.FirstField(IModel, []string{"id", "tenant_id", "examine_status"}); err != nil {
2021-11-01 11:19:49 +08:00
return err
} else if !isExist {
2022-01-14 17:09:06 +08:00
return errors.New("操作错误,数据信息不存在")
2021-11-01 11:19:49 +08:00
}
return nil
}
2022-01-14 17:09:06 +08:00
func examineCompany(id, tenantID uint64) (*ExamineManageInfo, error) {
2021-11-01 11:19:49 +08:00
mManageCompany := model.NewManageCompany()
err := checkManage(mManageCompany.ManageCompany, id)
if err != nil {
return nil, err
2022-01-14 17:09:06 +08:00
} else if tenantID > 0 && mManageCompany.TenantID != tenantID {
return nil, errors.New("操作错误,无权限操作")
2021-12-03 10:08:23 +08:00
} else if mManageCompany.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
2021-12-06 16:24:31 +08:00
return nil, errors.New("操作错误,当前入驻信息已审核")
2021-11-01 11:19:49 +08:00
}
2021-12-06 16:24:31 +08:00
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
2021-11-01 11:19:49 +08:00
}
2022-01-14 17:09:06 +08:00
func examineExpert(id, tenantID uint64) (*ExamineManageInfo, error) {
2021-11-01 11:19:49 +08:00
mManageExpert := model.NewManageExpert()
err := checkManage(mManageExpert.ManageExpert, id)
if err != nil {
return nil, err
2022-01-14 17:09:06 +08:00
} else if tenantID > 0 && mManageExpert.TenantID != tenantID {
return nil, errors.New("操作错误,无权限操作")
2021-12-03 10:08:23 +08:00
} else if mManageExpert.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
2021-12-06 16:24:31 +08:00
return nil, errors.New("操作错误,当前入驻信息已审核")
2021-11-01 11:19:49 +08:00
}
2021-12-06 16:24:31 +08:00
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
2021-11-24 09:59:29 +08:00
}
2022-01-14 17:09:06 +08:00
func examineResearch(id, tenantID uint64) (*ExamineManageInfo, error) {
2021-11-24 09:59:29 +08:00
mManageResearch := model.NewManageResearch()
err := checkManage(mManageResearch.ManageResearch, id)
if err != nil {
return nil, err
2022-01-14 17:09:06 +08:00
} else if tenantID > 0 && mManageResearch.TenantID != tenantID {
return nil, errors.New("操作错误,无权限操作")
2021-12-03 10:08:23 +08:00
} else if mManageResearch.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
2021-12-06 16:24:31 +08:00
return nil, errors.New("操作错误,当前入驻信息已审核")
2021-11-24 09:59:29 +08:00
}
2021-12-06 16:24:31 +08:00
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
2021-11-24 09:59:29 +08:00
}
2022-01-14 17:09:06 +08:00
func examineLaboratory(id, tenantID uint64) (*ExamineManageInfo, error) {
2021-11-24 09:59:29 +08:00
mManageLaboratory := model.NewManageLaboratory()
err := checkManage(mManageLaboratory.ManageLaboratory, id)
if err != nil {
return nil, err
2022-01-14 17:09:06 +08:00
} else if tenantID > 0 && mManageLaboratory.TenantID != tenantID {
return nil, errors.New("操作错误,无权限操作")
2021-12-03 10:08:23 +08:00
} else if mManageLaboratory.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
2021-12-06 16:24:31 +08:00
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
}
2022-01-14 17:09:06 +08:00
func examineAgent(id, tenantID uint64) (*ExamineManageInfo, error) {
2021-12-06 16:24:31 +08:00
mManageAgent := model.NewManageAgent()
err := checkManage(mManageAgent.ManageAgent, id)
if err != nil {
return nil, err
2022-01-14 17:09:06 +08:00
} else if tenantID > 0 && mManageAgent.TenantID != tenantID {
return nil, errors.New("操作错误,无权限操作")
2021-12-06 16:24:31 +08:00
} else if mManageAgent.Examine.ExamineStatus != model2.ExamineStatusForOngoing {
return nil, errors.New("操作错误,当前入驻信息已审核")
2021-11-24 09:59:29 +08:00
}
2021-12-06 16:24:31 +08:00
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
2021-11-01 11:19:49 +08:00
}
// Launch 发起审核
func (c *Examine) Launch(id uint64, identity, status int) error {
_status := model2.ExamineStatusKind(status)
if _status != model2.ExamineStatusForRefuse && _status != model2.ExamineStatusForAgree {
2022-01-14 17:09:06 +08:00
return errors.New("操作错误,未知的审核模式")
2021-11-01 11:19:49 +08:00
}
handle, has := examineHandle[identity]
if !has {
2022-01-14 17:09:06 +08:00
return errors.New("操作错误,未知的身份信息")
2021-11-01 11:19:49 +08:00
}
2022-01-14 17:09:06 +08:00
data, err := handle(id, c.TenantID)
2021-11-01 11:19:49 +08:00
if err != nil {
return err
}
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
2022-01-14 17:09:06 +08:00
if err = model2.Updates(data.IModel, map[string]interface{}{"examine_status": status, "updated_at": time.Now()}, tx); err != nil {
2021-11-01 11:19:49 +08:00
return err
}
// 拒绝后,不执行以下数据
if _status == model2.ExamineStatusForRefuse {
return nil
}
2021-12-06 16:24:31 +08:00
mUserInstance := model.NewUserInstance()
2021-11-01 11:19:49 +08:00
2021-12-06 16:24:31 +08:00
users := make([]*model2.UserInstance, 0)
2021-11-01 11:19:49 +08:00
2021-12-06 16:24:31 +08:00
if err = model2.ScanFields(mUserInstance.UserInstance, &users, []string{"id", "uuid", "identity"},
&model2.ModelWhereOrder{
Where: model2.NewWhereIn("uuid", data.UIDs),
}); err != nil {
2021-11-01 11:19:49 +08:00
return err
}
2021-12-06 16:24:31 +08:00
mUserIdentity := model.NewUserIdentity()
identitys := make([]*model2.UserIdentity, 0)
now := time.Now()
for _, v := range users {
identitys = append(identitys, &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
}
2021-11-01 11:19:49 +08:00
}
2021-12-06 16:24:31 +08:00
if len(identitys) > 0 {
if err = model2.Creates(mUserIdentity.UserIdentity, identity, tx); err != nil {
return err
}
2021-11-01 11:19:49 +08:00
}
return nil
})
}
func NewExamine() ExamineHandle {
2022-01-06 22:02:09 +08:00
return func(session *session.Admin) *Examine {
2022-01-14 17:09:06 +08:00
return &Examine{Admin: session}
2021-11-01 11:19:49 +08:00
}
}