118 lines
3.1 KiB
Go
118 lines
3.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"case-open-api/api/model"
|
|
"case-open-api/global"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type CaseCommentDao struct {
|
|
}
|
|
|
|
// GetCaseCommentById 获取数据-id
|
|
func (r *CaseCommentDao) GetCaseCommentById(commentId int64) (m *model.CaseComment, err error) {
|
|
err = global.Db.First(&m, commentId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCaseCommentPreloadById 获取数据-加载全部关联-id
|
|
func (r *CaseCommentDao) GetCaseCommentPreloadById(commentId int64) (m *model.CaseComment, err error) {
|
|
err = global.Db.Preload(clause.Associations).First(&m, commentId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteCaseComment 删除
|
|
func (r *CaseCommentDao) DeleteCaseComment(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.CaseComment{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteCaseCommentById 删除-id
|
|
func (r *CaseCommentDao) DeleteCaseCommentById(tx *gorm.DB, commentId int64) error {
|
|
if err := tx.Delete(&model.CaseComment{}, commentId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCaseComment 修改
|
|
func (r *CaseCommentDao) EditCaseComment(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.CaseComment{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditCaseCommentById 修改-id
|
|
func (r *CaseCommentDao) EditCaseCommentById(tx *gorm.DB, commentId int64, data interface{}) error {
|
|
err := tx.Model(&model.CaseComment{}).Where("comment_id = ?", commentId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCaseCommentList 获取列表
|
|
func (r *CaseCommentDao) GetCaseCommentList(maps interface{}) (m []*model.CaseComment, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCaseCommentListLimit 获取列表-限制数量
|
|
func (r *CaseCommentDao) GetCaseCommentListLimit(maps interface{}, limit int) (m []*model.CaseComment, err error) {
|
|
err = global.Db.Where(maps).Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetCaseCommentCount 获取数量
|
|
func (r *CaseCommentDao) GetCaseCommentCount(maps interface{}) (total int64, err error) {
|
|
err = global.Db.Model(&model.CaseComment{}).Where(maps).Count(&total).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
// GetCaseCommentListRand 获取列表-随机
|
|
func (r *CaseCommentDao) GetCaseCommentListRand(maps interface{}, limit int) (m []*model.CaseComment, err error) {
|
|
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddCaseComment 新增
|
|
func (r *CaseCommentDao) AddCaseComment(tx *gorm.DB, model *model.CaseComment) (*model.CaseComment, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetCaseComment 获取
|
|
func (r *CaseCommentDao) GetCaseComment(maps interface{}) (m *model.CaseComment, err error) {
|
|
err = global.Db.Where(maps).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|