case-api/api/dao/CaseComment.go
2025-03-07 17:26:03 +08:00

197 lines
5.1 KiB
Go

package dao
import (
"case-api/api/model"
"case-api/api/requests"
"case-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
}
// GetCaseCommentPageSearch 获取列表-分页
func (r *CaseCommentDao) GetCaseCommentPageSearch(req requests.GetCaseCommentPage, page, pageSize int) (m []*model.CaseComment, total int64, err error) {
var totalRecords int64
// 构建查询条件
query := global.Db.Model(&model.CaseComment{})
// 用户12
query = query.Preload("User")
// 用户医院
query = query.Preload("User.Hospital")
query = query.Where("case_id = ?", req.CaseId)
//query = query.Where("platform_id = ?", req.PlatformId)
if req.RootId == "" {
query = query.Where("level = ?", 1)
} else {
query = query.Where("root_id = ?", req.RootId)
}
query = query.Where("status = ?", 1)
// 排序
query = query.Order("created_at desc")
// 查询总数量
if err = query.Count(&totalRecords).Error; err != nil {
return nil, 0, err
}
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
if err != nil {
return nil, 0, err
}
return m, totalRecords, nil
}
// GetCaseCommentPreloadListLimit 获取列表-限制数量
func (r *CaseCommentDao) GetCaseCommentPreloadListLimit(maps interface{}, limit int) (m []*model.CaseComment, err error) {
// 构建查询条件
query := global.Db.Model(&model.CaseComment{})
// 用户
query = query.Preload("User")
// 用户医院
query = query.Preload("User.Hospital")
// 排序
query = query.Order("created_at desc")
err = query.Where(maps).Limit(limit).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// Inc 自增
func (r *CaseCommentDao) Inc(tx *gorm.DB, commentId int64, field string, numeral int) error {
err := tx.Model(&model.CaseComment{}).Where("comment_id = ?", commentId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
if err != nil {
return err
}
return nil
}
// Dec 自减
func (r *CaseCommentDao) Dec(tx *gorm.DB, commentId int64, field string, numeral int) error {
err := tx.Model(&model.CaseComment{}).Where("comment_id = ?", commentId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
if err != nil {
return err
}
return nil
}