87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
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
|
|
}
|
|
}
|
|
|
|
func (c *Shelf) Handle(iModel model.IModel, id uint64, callback func(kind model.ShelfStatusKind) error) error {
|
|
if c.Admin == nil && c.Enterprise == nil {
|
|
return errors.New("操作错误,未知的人员操作")
|
|
}
|
|
iModel.SetID(id)
|
|
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("操作错误,无权限操作")
|
|
}
|
|
}
|
|
shelfStatus := model.ShelfStatusForUp
|
|
|
|
if out.ShelfStatus == model.ShelfStatusForUp {
|
|
shelfStatus = model.ShelfStatusForDown
|
|
}
|
|
values := map[string]interface{}{
|
|
"shelf_status": shelfStatus,
|
|
"updated_at": time.Now(),
|
|
}
|
|
if err := model.Updates(iModel, values); err != nil {
|
|
return err
|
|
}
|
|
if callback != nil {
|
|
return callback(shelfStatus)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewShelf(options ...ShelfOption) *Shelf {
|
|
out := new(Shelf)
|
|
|
|
for _, option := range options {
|
|
option(out)
|
|
}
|
|
return out
|
|
}
|