cutego/modules/core/dao/notice_dao.go

89 lines
2.2 KiB
Go
Raw Normal View History

2022-03-01 13:50:13 +08:00
package dao
import (
2023-01-18 15:40:27 +08:00
"cutego/modules/core/api/v1/request"
2023-01-18 17:21:12 +08:00
"cutego/modules/core/dataobject"
2023-01-18 17:46:25 +08:00
"cutego/pkg/logging"
2022-03-01 13:50:13 +08:00
"cutego/pkg/page"
2023-01-18 17:09:49 +08:00
"cutego/refs"
2022-03-01 13:50:13 +08:00
"github.com/druidcaesa/gotool"
)
type NoticeDao struct {
}
// SelectPage 查询集合
2023-01-18 17:21:12 +08:00
func (d NoticeDao) SelectPage(query request.NoticeQuery) (*[]dataobject.SysNotice, int64) {
notices := make([]dataobject.SysNotice, 0)
session := refs.SqlDB.NewSession().Table(dataobject.SysNotice{}.TableName())
2022-03-01 13:50:13 +08:00
if gotool.StrUtils.HasNotEmpty(query.NoticeTitle) {
session.And("notice_title like concat('%', ?, '%')", query.NoticeTitle)
}
if gotool.StrUtils.HasNotEmpty(query.NoticeType) {
session.And("notice_type = ?", query.NoticeType)
}
if gotool.StrUtils.HasNotEmpty(query.CreateBy) {
session.And("create_by like concat('%', ?, '%')", query.CreateBy)
}
total, _ := page.GetTotal(session.Clone())
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&notices)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return nil, 0
}
return &notices, total
}
// Insert 添加数据
2023-01-18 17:21:12 +08:00
func (d NoticeDao) Insert(notice dataobject.SysNotice) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
insert, err := session.Insert(&notice)
if err != nil {
session.Rollback()
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return 0
}
session.Commit()
return insert
}
// Delete 批量删除
func (d NoticeDao) Delete(list []int64) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
2023-01-18 17:21:12 +08:00
i, err := session.In("notice_id", list).Delete(&dataobject.SysNotice{})
2022-03-01 13:50:13 +08:00
if err != nil {
session.Rollback()
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return 0
}
session.Commit()
return i
}
// SelectById 查询
2023-01-18 17:21:12 +08:00
func (d NoticeDao) SelectById(id int64) *dataobject.SysNotice {
notice := dataobject.SysNotice{}
2023-01-18 17:09:49 +08:00
_, err := refs.SqlDB.NewSession().Where("notice_id = ?", id).Get(&notice)
2022-03-01 13:50:13 +08:00
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return nil
}
return &notice
}
// Update 修改数据
2023-01-18 17:21:12 +08:00
func (d NoticeDao) Update(notice dataobject.SysNotice) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
update, err := session.Where("notice_id = ?", notice.NoticeId).Update(&notice)
if err != nil {
session.Rollback()
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
return 0
}
session.Commit()
return update
}