Files

87 lines
1.8 KiB
Go
Raw Normal View History

2022-01-24 11:28:21 +08:00
package controller
import (
"SciencesServer/app/common/model"
"SciencesServer/app/session"
"errors"
"time"
)
type Shelf struct {
*session.Admin
*session.Enterprise
}
type ShelfOption func(object *Shelf)
type IModelInfo struct {
model.Model
UID uint64 `json:"uid"`
model.ModelTenant
model.Shelf
}
func WithShelfSessionAdmin(session *session.Admin) ShelfOption {
return func(object *Shelf) {
object.Admin = session
}
}
func WithShelfSessionEnterprise(session *session.Enterprise) ShelfOption {
return func(object *Shelf) {
object.Enterprise = session
}
}
2022-02-08 18:26:40 +08:00
func (c *Shelf) Handle(iModel model.IModel, id uint64, callback func(kind model.ShelfStatusKind) error) error {
2022-01-24 11:28:21 +08:00
if c.Admin == nil && c.Enterprise == nil {
return errors.New("操作错误,未知的人员操作")
}
2022-02-08 18:26:40 +08:00
iModel.SetID(id)
2022-01-24 11:28:21 +08:00
out := new(IModelInfo)
err := model.ScanFields(iModel, out, []string{"id", "tenant_id", "uid", "shelf_status"},
&model.ModelWhereOrder{Where: model.NewWhere("id", id)})
if err != nil {
return err
} else if out.ID <= 0 {
return errors.New("操作错误,数据信息不存在或已被删除")
}
if c.Admin != nil {
if c.Admin.TenantID > 0 && c.Admin.TenantID != out.TenantID {
return errors.New("操作错误,无权限操作")
}
} else {
if c.Enterprise.UID != out.UID {
return errors.New("操作错误,无权限操作")
}
}
2022-02-08 18:26:40 +08:00
shelfStatus := model.ShelfStatusForUp
if out.ShelfStatus == model.ShelfStatusForUp {
shelfStatus = model.ShelfStatusForDown
}
2022-01-24 11:28:21 +08:00
values := map[string]interface{}{
2022-02-08 18:26:40 +08:00
"shelf_status": shelfStatus,
2022-01-24 11:28:21 +08:00
"updated_at": time.Now(),
}
2022-02-08 18:26:40 +08:00
if err := model.Updates(iModel, values); err != nil {
return err
}
if callback != nil {
return callback(shelfStatus)
2022-01-24 11:28:21 +08:00
}
2022-02-08 18:26:40 +08:00
return nil
2022-01-24 11:28:21 +08:00
}
func NewShelf(options ...ShelfOption) *Shelf {
out := new(Shelf)
for _, option := range options {
option(out)
}
return out
}