Files
2021-11-12 11:54:39 +08:00

88 lines
2.1 KiB
Go

package manage
import (
model2 "ArmedPolice/app/common/model"
"ArmedPolice/app/controller/basic"
"ArmedPolice/app/model"
"ArmedPolice/app/service"
"errors"
"time"
)
type Notice struct{ *service.Session }
type NoticeHandle func(session *service.Session) *Notice
type NoticeInfo struct {
ID string `json:"id"`
*model2.ManageNotice
}
// List 列表信息
func (c *Notice) List(page, pageSize int) (*basic.PageDataResponse, error) {
mManageNotice := model.NewManageNotice()
out := make([]*model2.ManageNotice, 0)
var count int64
if err := model2.Pages(mManageNotice.ManageNotice, &out, page, pageSize, &count, &model2.ModelWhereOrder{
Where: model2.NewWhere("tenant_id", c.TenantID),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}); err != nil {
return nil, err
}
list := make([]*NoticeInfo, 0)
for _, v := range out {
list = append(list, &NoticeInfo{
ID: v.GetEncodeID(),
ManageNotice: v,
})
}
return &basic.PageDataResponse{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Notice) Detail(id uint64) (*model2.ManageNotice, error) {
mManageNotice := model.NewManageNotice()
mManageNotice.ID = id
isExist, err := model2.FirstWhere(mManageNotice.ManageNotice)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,公告信息不存在或已被删除")
}
return mManageNotice.ManageNotice, nil
}
// Form 数据操作
func (c *Notice) Form(id uint64, title, content string) error {
mManageNotice := model.NewManageNotice()
if id > 0 {
mManageNotice.ID = id
return model2.Updates(mManageNotice.ManageNotice, map[string]interface{}{
"title": title, "content": content, "updated_at": time.Now(),
})
}
mManageNotice.Title = title
mManageNotice.Content = content
return model2.Create(mManageNotice)
}
func (c *Notice) Delete(id uint64) error {
mManageNotice := model.NewManageNotice()
mManageNotice.ID = id
return model2.Delete(mManageNotice.ManageNotice)
}
func NewNotice() NoticeHandle {
return func(session *service.Session) *Notice {
return &Notice{session}
}
}