model.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package yb_community_question
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/models/tables/yb_community_question_audio"
  5. "time"
  6. )
  7. func (item *YbCommunityQuestion) Create() (err error) {
  8. err = global.DEFAULT_MYSQL.Create(item).Error
  9. return
  10. }
  11. func (item *YbCommunityQuestion) Update(updateCols []string) (err error) {
  12. err = global.DEFAULT_MYSQL.Model(item).Select(updateCols).Updates(*item).Error
  13. return
  14. }
  15. // GetPageListByCondition 获取问答列表-分页
  16. func GetPageListByCondition(condition string, pars []interface{}, pageIndex, pageSize int) (list []*YbCommunityQuestion, err error) {
  17. offset := (pageIndex - 1) * pageSize
  18. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).Where(condition, pars...).Offset(offset).Limit(pageSize).Order("create_time DESC").Scan(&list).Error
  19. return
  20. }
  21. // GetItemById 主键获取问答信息
  22. func GetItemById(questionId int) (item *YbCommunityQuestion, err error) {
  23. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
  24. Where("community_question_id = ? AND is_deleted = 0", questionId).
  25. First(&item).Error
  26. return
  27. }
  28. // UpdateQuestionAndAudioList 更新问答及音频
  29. func UpdateQuestionAndAudioList(item *YbCommunityQuestion, updateCols []string, audioList []*yb_community_question_audio.YbCommunityQuestionAudio) (err error) {
  30. tx := global.DEFAULT_MYSQL.Begin()
  31. defer func() {
  32. if err != nil {
  33. tx.Rollback()
  34. } else {
  35. tx.Commit()
  36. }
  37. }()
  38. if err = tx.Model(item).Select(updateCols).Updates(*item).Error; err != nil {
  39. return
  40. }
  41. if err = tx.Where("community_question_id = ?", item.CommunityQuestionID).
  42. Delete(yb_community_question_audio.YbCommunityQuestionAudio{}).Error; err != nil {
  43. return
  44. }
  45. if err = tx.Model(yb_community_question_audio.YbCommunityQuestionAudio{}).
  46. CreateInBatches(audioList, len(audioList)).Error; err != nil {
  47. }
  48. return
  49. }
  50. type QuestionListCount struct {
  51. ReplierUserId uint64 `json:"replier_user_id"`
  52. UserId uint64 `json:"user_id"`
  53. ReplyStatus int `json:"reply_status"`
  54. Total int `json:"total"`
  55. }
  56. // GetQuestionListCount 获取问答数量统计
  57. func GetQuestionListCount(condition string, pars []interface{}) (list []*QuestionListCount, err error) {
  58. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
  59. Select("reply_status, replier_user_id, user_id, COUNT(1) total").
  60. Where(condition, pars...).
  61. Group("reply_status, replier_user_id, user_id").
  62. Scan(&list).Error
  63. return
  64. }
  65. // GetUnreadNum 获取未读数
  66. func GetUnreadNum(condition string, pars []interface{}) (num int64, err error) {
  67. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).Where(condition, pars...).Count(&num).Error
  68. return
  69. }
  70. // UpdateReplierRead 更新回复人已读
  71. func UpdateReplierRead(replierUserId int, questionIds []int) (err error) {
  72. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
  73. Where("replier_user_id = ? AND community_question_id IN (?) AND replier_is_read=0", replierUserId, questionIds).
  74. Updates(YbCommunityQuestion{
  75. ReplierIsRead: 1,
  76. ModifyTime: time.Now().Local(),
  77. }).Error
  78. return
  79. }
  80. // UpdateUserRead 更新用户已读
  81. func UpdateUserRead(userId int, questionIds []int) (err error) {
  82. err = global.DEFAULT_MYSQL.Model(YbCommunityQuestion{}).
  83. Where("user_id = ? AND community_question_id IN (?) AND is_read=0", userId, questionIds).
  84. Updates(YbCommunityQuestion{
  85. IsRead: 1,
  86. ModifyTime: time.Now().Local(),
  87. }).Error
  88. return
  89. }