Files
2021-12-02 15:23:48 +08:00

245 lines
8.3 KiB
Go

package user
import (
"SciencesServer/app/api/enterprise/model"
"SciencesServer/app/basic/config"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/session"
"SciencesServer/serve/orm"
"errors"
"gorm.io/gorm"
"time"
)
// Settled 入驻
type Settled struct {
*session.Enterprise
local string
}
type SettledHandle func(session *session.Enterprise, local string) *Settled
// SettledParams 绑定信息
type SettledParams struct {
Name, Image, Code, Introduce string // 唯一编码
config.Area
Industrys, Keywords []string
}
// effect 入驻信息有效性
func (c *SettledParams) effect(iModel model2.IModel, local string) error {
//var count int64
//
//if err := model2.Count(iModel, &count, model2.NewWhere("uid", uid)); err != nil {
// return err
//} else if count > 0 {
// return errors.New("无权限操作,当前身份下已含有申请入驻信息")
//}
return nil
}
// pass 入驻信息通过性
func (c *SettledParams) pass(identity int, mStatus model2.ExamineStatusKind) (bool, error) {
if mStatus != model2.ExamineStatusForRefuse {
return false, nil
}
return true, nil
}
// filter 筛选信息
func (c *Settled) filter(identity int, where ...*model2.ModelWhere) (bool, error) {
mSysIdentity := model.NewSysIdentity()
_, err := model2.FirstField(mSysIdentity.SysIdentity, []string{"id", "register_count", "is_examine"}, model2.NewWhere("identity", identity))
if err != nil {
return true, err
}
var iModel model2.IModel
if identity&config.TenantUserIdentityForCompany > 0 {
iModel = model2.NewUserCompany()
} else if identity&config.TenantUserIdentityForExpert > 0 {
iModel = model2.NewUserExpert()
} else if identity&config.TenantUserIdentityForResearch > 0 {
iModel = model2.NewUserResearch()
} else if identity&config.TenantUserIdentityForLaboratory > 0 {
iModel = model2.NewUserLaboratory()
} else if identity&config.TenantUserIdentityForAgent > 0 {
iModel = model2.NewUserAgent()
}
var count int64
where = append(where, model2.NewWhere("status", model2.InvalidStatusForNot))
if err = model2.Count(iModel, &count, where...); err != nil {
return true, err
}
if count >= int64(mSysIdentity.RegisterCount) {
return true, errors.New("操作错误,已超过当前身份最大入驻人数")
}
return mSysIdentity.IsExamine == model2.SysIdentityExamineForYes, nil
}
// Company 公司企业
func (c *Settled) Company(params *SettledParams, inviterID uint64, other *config.IdentityForCompany) error {
mManageCompany := model.NewManageCompany()
isExist, err := model2.FirstField(mManageCompany.ManageCompany, []string{"id", "status"},
model2.NewWhere("code", params.Code), model2.NewWhere("local", c.local),
model2.NewWhere("status", model2.ExamineStatusForRefuse))
isExamine := true
if err != nil {
return err
} else if isExist {
if mManageCompany.Status == model2.ExamineStatusForOngoing {
return errors.New("操作错误,当前企业信息审核中,不可入驻")
}
// 筛选企业条件
if isExamine, err = c.filter(config.TenantUserIdentityForCompany, model2.NewWhere("company_id", mManageCompany.ID)); err != nil {
return err
}
}
mManageCompany.Local.Local = c.local
mManageCompany.InviterID = inviterID
mManageCompany.Name = params.Name
mManageCompany.Code = params.Code
mManageCompany.Image = model2.Image{Image: params.Image}
mManageCompany.Area = model2.Area{
Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
}
mManageCompany.WebUrl = other.WebUrl
mManageCompany.SetIndustryAttribute(params.Industrys)
mManageCompany.SetKeywordAttribute(params.Keywords)
mManageCompany.Introduce = params.Introduce
if isExamine {
mManageCompany.Status = model2.ExamineStatusForAgree
}
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
if mManageCompany.ID <= 0 {
if err = model2.Create(mManageCompany.ManageCompany, tx); err != nil {
return err
}
}
// 过滤用户其他公司入驻信息
mUserCompany := model.NewUserCompany()
if err := model2.UpdatesWhere(mUserCompany.UserCompany, map[string]interface{}{
"status": model2.InvalidStatusForYes, "updated_at": time.Now(),
}, []*model2.ModelWhere{model2.NewWhere("uid", c.UID)}, tx); err != nil {
return err
}
mUserCompany.UID = c.UID
mUserCompany.CompanyID = mManageCompany.ID
return model2.Create(mUserCompany.UserCompany, tx)
})
}
//
//// Expert 专家
//func (c *Settled) Expert(params *SettledParams, other *config.IdentityForExpert) error {
// mManageExpert := model3.NewManageExpert()
//
// err := params.effect(c.UID, mManageExpert.ManageExpert)
//
// if err != nil {
// return err
// }
// mManageExpert.Area = model2.Area{
// Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
// }
// mManageExpert.SetIndustryAttribute(params.Industrys)
// mManageExpert.SetKeywordAttribute(params.Keywords)
// mManageExpert.Introduce = params.Introduce
// mManageExpert.Position = utils.AnyToJSON(model2.Position{Longitude: other.Longitude, Latitude: other.Latitude})
// mManageExpert.School = other.School
// mManageExpert.Major = other.Major
// mManageExpert.Job = other.Job
// mManageExpert.Title = other.Title
// mManageExpert.WorkAt = utils.DataTimeToDate(other.WorkAt)
// mManageExpert.Research = utils.AnyToJSON(other.Research)
//
// if mManageExpert.ID <= 0 {
// mManageExpert.UID = c.UID
// return model2.Create(mManageExpert.ManageExpert)
// }
// if !params.pass(c.UID, mManageExpert.UID, mManageExpert.Status) {
// return errors.New("操作错误,无权限操作")
// }
// mManageExpert.Status = model2.ExamineStatusForOngoing
// return model2.Updates(mManageExpert.ManageExpert, mManageExpert.ManageExpert)
//}
//
//// Research 研究机构
//func (c *Settled) Research(params *SettledParams, other *config.IdentityForResearch) error {
// mManageResearch := model3.NewManageResearch()
//
// err := params.effect(c.UID, mManageResearch.ManageResearch)
//
// if err != nil {
// return err
// }
// mManageResearch.Area = model2.Area{
// Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
// }
// mManageResearch.Industry = params.Industrys
// mManageResearch.SetKeywordAttribute(params.Keywords)
// mManageResearch.Introduce = params.Introduce
// mManageResearch.Position = utils.AnyToJSON(model2.Position{Longitude: other.Longitude, Latitude: other.Latitude})
// mManageResearch.Research = other.Research
//
// if mManageResearch.ID <= 0 {
// mManageResearch.UID = c.UID
// return model2.Create(mManageResearch.ManageResearch)
// }
// if !params.pass(c.UID, mManageResearch.UID, mManageResearch.Status) {
// return errors.New("操作错误,无权限操作")
// }
// mManageResearch.Status = model2.ExamineStatusForOngoing
// return model2.Updates(mManageResearch.ManageResearch, mManageResearch.ManageResearch)
//}
//
//// Laboratory 实验室
//func (c *Settled) Laboratory(params *SettledParams, other *config.IdentityForLaboratory) error {
// mManageLaboratory := model3.NewManageLaboratory()
//
// err := params.effect(c.UID, mManageLaboratory.ManageLaboratory)
//
// if err != nil {
// return err
// }
// mManageLaboratory.Name = params.Name
// mManageLaboratory.Code = params.Code
// mManageLaboratory.Area = model2.Area{
// Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
// }
// mManageLaboratory.Industry = params.Industrys
// mManageLaboratory.SetKeywordAttribute(params.Keywords)
// mManageLaboratory.Introduce = params.Introduce
// mManageLaboratory.Position = utils.AnyToJSON(model2.Position{Longitude: other.Longitude, Latitude: other.Latitude})
// mManageLaboratory.Research = utils.AnyToJSON(other.Research)
//
// if mManageLaboratory.ID <= 0 {
// mManageLaboratory.UID = c.UID
// return model2.Create(mManageLaboratory.ManageLaboratory)
// }
// if !params.pass(c.UID, mManageLaboratory.UID, mManageLaboratory.Status) {
// return errors.New("操作错误,无权限操作")
// }
// mManageLaboratory.Status = model2.ExamineStatusForOngoing
// return model2.Updates(mManageLaboratory.ManageLaboratory, mManageLaboratory.ManageLaboratory)
//}
//
//// Agent 经纪人
//func (c *Settled) Agent(params *SettledParams) error {
// return nil
//}
func NewSettled() SettledHandle {
return func(session *session.Enterprise, local string) *Settled {
return &Settled{Enterprise: session, local: local}
}
}