2021-09-28 11:47:19 +08:00
|
|
|
package tenant
|
|
|
|
|
|
|
|
import (
|
2022-01-05 16:09:55 +08:00
|
|
|
"SciencesServer/app/api/admin/model"
|
|
|
|
"SciencesServer/app/basic/config"
|
|
|
|
"SciencesServer/app/basic/controller"
|
2021-09-28 11:47:19 +08:00
|
|
|
model2 "SciencesServer/app/common/model"
|
|
|
|
"SciencesServer/app/service"
|
2022-01-05 16:09:55 +08:00
|
|
|
"SciencesServer/app/session"
|
|
|
|
config2 "SciencesServer/config"
|
|
|
|
"SciencesServer/utils"
|
2021-09-28 11:47:19 +08:00
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
type Instance struct {
|
|
|
|
session *session.Admin
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
type InstanceHandle func(session *session.Admin) *Instance
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
type (
|
|
|
|
// InstanceInfo 租户信息
|
|
|
|
InstanceInfo struct {
|
2022-01-05 16:09:55 +08:00
|
|
|
ID string `json:"id"`
|
|
|
|
*model2.SysTenant
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
// InstanceParams 参数信息
|
2021-09-28 11:47:19 +08:00
|
|
|
InstanceParams struct {
|
2022-01-05 16:09:55 +08:00
|
|
|
ID uint64
|
|
|
|
Name, Domain, Remark string
|
|
|
|
Area config.Area
|
|
|
|
Contact, ContactMobile string
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
// Index 首页列表信息
|
|
|
|
func (c *Instance) Index(name string, page, pageSize int) (*controller.ReturnPages, error) {
|
|
|
|
mSysTenant := model.NewSysTenant()
|
|
|
|
out := make([]*model2.SysTenant, 0)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
where := []*model2.ModelWhereOrder{
|
|
|
|
&model2.ModelWhereOrder{Order: model2.NewOrder("id", model2.OrderModeToDesc)},
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
if name != "" {
|
2022-01-05 16:09:55 +08:00
|
|
|
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereLike("name", name)})
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
var count int64
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
if err := model2.Pages(mSysTenant.SysTenant, &out, page, pageSize, &count, where...); err != nil {
|
2021-09-28 11:47:19 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list := make([]*InstanceInfo, 0)
|
|
|
|
|
|
|
|
for _, v := range out {
|
2022-01-05 16:09:55 +08:00
|
|
|
list = append(list, &InstanceInfo{
|
|
|
|
ID: v.GetEncodeID(),
|
|
|
|
SysTenant: v,
|
|
|
|
})
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2021-10-12 16:53:49 +08:00
|
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
// Form 数据操作
|
|
|
|
func (c *Instance) Form(params *InstanceParams) error {
|
|
|
|
mSysTenant := model.NewSysTenant()
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
if params.ID > 0 {
|
|
|
|
mSysTenant.ID = params.ID
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
isExist, err := model2.First(mSysTenant.SysTenant)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
if err != nil {
|
2021-09-28 11:47:19 +08:00
|
|
|
return err
|
2022-01-05 16:09:55 +08:00
|
|
|
} else if !isExist {
|
|
|
|
return errors.New("操作错误,平台信息不存在或已被删除")
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
mSysTenant.Name = params.Name
|
|
|
|
mSysTenant.Area = model2.Area{
|
|
|
|
Province: params.Area.Province,
|
|
|
|
City: params.Area.City,
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
mSysTenant.Contact = params.Contact
|
|
|
|
mSysTenant.ContactMobile = params.ContactMobile
|
|
|
|
mSysTenant.Remark = params.Remark
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
if mSysTenant.ID > 0 {
|
|
|
|
return model2.Updates(mSysTenant.SysTenant, mSysTenant.SysTenant)
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
mSysTenant.Key = utils.GetRandomString(12)
|
|
|
|
mSysTenant.Domain = params.Domain
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
if err := model2.Create(mSysTenant.SysTenant); err != nil {
|
2021-09-28 11:47:19 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
_session := session.NewTenant()
|
|
|
|
_session.ID = mSysTenant.ID
|
|
|
|
_session.Name = mSysTenant.Name
|
|
|
|
_session.Domain = mSysTenant.Domain
|
2021-09-28 11:47:19 +08:00
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
service.Publish(config2.EventForRedisHashProduce, config2.RedisKeyForTenant, _session.Domain, _session)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
// Delete 删除操作
|
|
|
|
func (c *Instance) Delete(id uint64) error {
|
|
|
|
mSysTenant := model.NewSysTenant()
|
2021-09-28 11:47:19 +08:00
|
|
|
mSysTenant.ID = id
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
isExist, err := model2.FirstField(mSysTenant.SysTenant, []string{"id", "name", "domain"})
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !isExist {
|
2022-01-05 16:09:55 +08:00
|
|
|
return errors.New("操作错误,平台信息不存在或已被删除")
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
if err = model2.Delete(mSysTenant.SysTenant); err != nil {
|
2021-09-28 11:47:19 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
service.Publish(config2.EventForRedisHashDestroy, config2.RedisKeyForTenant, mSysTenant.Domain)
|
2021-09-28 11:47:19 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstance() InstanceHandle {
|
2022-01-05 16:09:55 +08:00
|
|
|
return func(session *session.Admin) *Instance {
|
|
|
|
return &Instance{
|
|
|
|
session: session,
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
}
|