188 lines
4.8 KiB
Go
188 lines
4.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"case-admin-api/api/model"
|
|
"case-admin-api/api/requests"
|
|
"case-admin-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
|
|
}
|
|
|
|
// 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{})
|
|
|
|
// 基础数据-用户
|
|
query = query.Preload("User")
|
|
|
|
// 用户医院
|
|
query = query.Preload("User.Hospital")
|
|
|
|
if req.PlatformId != "" {
|
|
query = query.Where("platform_id = ?", req.PlatformId)
|
|
}
|
|
|
|
if req.RootId != "" {
|
|
query = query.Where("root_id = ?", req.RootId)
|
|
}
|
|
|
|
if req.ParentId != "" {
|
|
query = query.Where("parent_id = ?", req.ParentId)
|
|
}
|
|
|
|
if req.CaseId != "" {
|
|
query = query.Where("case_id = ?", req.CaseId)
|
|
}
|
|
|
|
if req.IsHighQuality != nil {
|
|
query = query.Where("is_high_quality = ?", req.IsHighQuality)
|
|
}
|
|
|
|
// 搜索关键字-医生名称/医生手机号/评论内容/病例名称
|
|
if req.Keyword != "" {
|
|
keyword := "%" + req.Keyword + "%" //
|
|
|
|
// 评论内容
|
|
orQuery := global.Db.Model(&model.CaseComment{}).Or("content LIKE ?", keyword)
|
|
|
|
// 姓名/手机号
|
|
userSubQuery := global.Db.Model(&model.User{}).
|
|
Select("user_id").
|
|
Where("user_name LIKE ?", keyword)
|
|
|
|
// 执行组建
|
|
orQuery = orQuery.Or(gorm.Expr("user_id IN (?)", userSubQuery))
|
|
|
|
// 医院名称
|
|
basicHospitalSubQuery := global.Db.Model(&model.BasicHospital{}).
|
|
Select("hospital_id").
|
|
Where("hospital_name LIKE ?", keyword)
|
|
|
|
userHospitalIdSubQuery := global.Db.Model(&model.User{}).
|
|
Select("user_id").
|
|
Where(gorm.Expr("hospital_id IN (?)", basicHospitalSubQuery))
|
|
|
|
// 执行组建
|
|
orQuery = orQuery.Or(gorm.Expr("user_id IN (?)", userHospitalIdSubQuery))
|
|
|
|
query = query.Where(orQuery)
|
|
}
|
|
|
|
// 排序
|
|
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
|
|
}
|