85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package notice
|
|
|
|
import (
|
|
"context"
|
|
"git.echol.cn/loser/lckt/global"
|
|
"git.echol.cn/loser/lckt/model/notice"
|
|
noticeReq "git.echol.cn/loser/lckt/model/notice/request"
|
|
)
|
|
|
|
type NoticeService struct{}
|
|
|
|
// CreateNotice 创建通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) CreateNotice(ctx context.Context, not *notice.Notice) (err error) {
|
|
err = global.GVA_DB.Create(not).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteNotice 删除通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) DeleteNotice(ctx context.Context, ID string) (err error) {
|
|
err = global.GVA_DB.Delete(¬ice.Notice{}, "id = ?", ID).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteNoticeByIds 批量删除通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) DeleteNoticeByIds(ctx context.Context, IDs []string) (err error) {
|
|
err = global.GVA_DB.Delete(&[]notice.Notice{}, "id in ?", IDs).Error
|
|
return err
|
|
}
|
|
|
|
// UpdateNotice 更新通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) UpdateNotice(ctx context.Context, not notice.Notice) (err error) {
|
|
err = global.GVA_DB.Model(¬ice.Notice{}).Where("id = ?", not.ID).Updates(¬).Error
|
|
return err
|
|
}
|
|
|
|
// GetNotice 根据ID获取通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) GetNotice(ctx context.Context, ID string) (not notice.Notice, err error) {
|
|
err = global.GVA_DB.Where("id = ?", ID).First(¬).Error
|
|
return
|
|
}
|
|
|
|
// GetNoticeInfoList 分页获取通知记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (notService *NoticeService) GetNoticeInfoList(ctx context.Context, info noticeReq.NoticeSearch) (list []notice.Notice, total int64, err error) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.GVA_DB.Model(¬ice.Notice{})
|
|
var nots []notice.Notice
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
|
|
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
var OrderStr string
|
|
orderMap := make(map[string]bool)
|
|
orderMap["title"] = true
|
|
if orderMap[info.Sort] {
|
|
OrderStr = info.Sort
|
|
if info.Order == "descending" {
|
|
OrderStr = OrderStr + " desc"
|
|
}
|
|
db = db.Order(OrderStr)
|
|
}
|
|
|
|
if limit != 0 {
|
|
db = db.Limit(limit).Offset(offset)
|
|
}
|
|
|
|
err = db.Find(¬s).Error
|
|
return nots, total, err
|
|
}
|
|
func (notService *NoticeService) GetNoticePublic(ctx context.Context) {
|
|
// 此方法为获取数据源定义的数据
|
|
// 请自行实现
|
|
}
|