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