need.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package dao
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/apm/model/need"
  5. "go-common/library/log"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _tableNeed = "needs"
  10. _tableLike = "user_likes"
  11. _addCountsSQL = "UPDATE needs SET like_counts=like_counts+?,dislike_counts=dislike_counts+? WHERE id=?"
  12. )
  13. //NeedInfoAdd add need info
  14. func (d *Dao) NeedInfoAdd(r *need.NAddReq, username string) (err error) {
  15. ni := &need.NInfo{
  16. Title: r.Title,
  17. Content: r.Content,
  18. Reporter: username,
  19. Status: 1,
  20. }
  21. if err = d.DB.Create(&ni).Error; err != nil {
  22. err = errors.WithStack(err)
  23. return
  24. }
  25. return
  26. }
  27. //NeedInfoList all need info
  28. func (d *Dao) NeedInfoList(arg *need.NListReq) (res []*need.NInfo, err error) {
  29. where := d.needInfoCondition(arg)
  30. if err = d.DB.Table(_tableNeed).Where(where).Order("id DESC").Offset((arg.Pn - 1) * arg.Ps).Limit(arg.Ps).Find(&res).Error; err != nil {
  31. err = errors.WithStack(err)
  32. log.Error("NeedInfoList:%s", err)
  33. return
  34. }
  35. return
  36. }
  37. //NeedInfoCount need info count
  38. func (d *Dao) NeedInfoCount(arg *need.NListReq) (count int64, err error) {
  39. where := d.needInfoCondition(arg)
  40. if err = d.DB.Table(_tableNeed).Where(where).Count(&count).Error; err != nil {
  41. log.Error("NeedInfoCount:%s", err)
  42. return
  43. }
  44. return
  45. }
  46. //needInfoCondition is
  47. func (d *Dao) needInfoCondition(arg *need.NListReq) (where string) {
  48. where = "status !=5"
  49. if arg.Status > 0 {
  50. where += fmt.Sprintf(" and `status`='%d'", arg.Status)
  51. }
  52. if arg.Reporter != "" {
  53. where += fmt.Sprintf(" and `reporter`='%s'", arg.Reporter)
  54. }
  55. return where
  56. }
  57. // GetNeedInfo is
  58. func (d *Dao) GetNeedInfo(id int64) (r *need.NInfo, err error) {
  59. r = &need.NInfo{}
  60. if err = d.DB.Table(_tableNeed).Where("id=?", id).Find(r).Error; err != nil {
  61. log.Error("GetNeedInfo:%s", err)
  62. return
  63. }
  64. return
  65. }
  66. //NeedInfoEdit is
  67. func (d *Dao) NeedInfoEdit(arg *need.NEditReq) (err error) {
  68. if err = d.DB.Table(_tableNeed).Where("id=?", arg.ID).Updates(map[string]interface{}{"content": arg.Content, "title": arg.Title}).Error; err != nil {
  69. log.Error("NeedInfoEdit:%s", err)
  70. return
  71. }
  72. return
  73. }
  74. //NeedVerify is
  75. func (d *Dao) NeedVerify(r *need.NVerifyReq) (err error) {
  76. tx := d.DB.Begin()
  77. if err = d.DB.Table(_tableNeed).Where("id=?", r.ID).Update("status", r.Status).Error; err != nil {
  78. log.Error("NeedVerify:%s", err)
  79. tx.Rollback()
  80. return
  81. }
  82. tx.Commit()
  83. return
  84. }
  85. //LikeCountsStats thumbsup counts
  86. func (d *Dao) LikeCountsStats(r *need.Likereq, like, dislike int) (err error) {
  87. tx := d.DB.Begin()
  88. if err = d.DB.Exec(_addCountsSQL, like, dislike, r.ReqID).Error; err != nil {
  89. err = errors.WithStack(err)
  90. log.Error("LikeCountsStats:%s", err)
  91. tx.Rollback()
  92. return
  93. }
  94. tx.Commit()
  95. return
  96. }
  97. //GetVoteInfo is
  98. func (d *Dao) GetVoteInfo(r *need.Likereq, username string) (u *need.UserLikes, err error) {
  99. u = &need.UserLikes{}
  100. if err = d.DB.Table(_tableLike).Where("req_id=? and `user`=?", r.ReqID, username).Find(u).Error; err != nil {
  101. err = errors.WithStack(err)
  102. return
  103. }
  104. return
  105. }
  106. //UpdateVoteInfo is
  107. func (d *Dao) UpdateVoteInfo(r *need.Likereq, username string) (err error) {
  108. if err = d.DB.Table(_tableLike).Where("req_id=? and `user`=?", r.ReqID, username).Update("like_type", r.LikeType).Error; err != nil {
  109. err = errors.WithStack(err)
  110. log.Error("UpdateVoteInfo:%s", err)
  111. return
  112. }
  113. return
  114. }
  115. //AddVoteInfo is
  116. func (d *Dao) AddVoteInfo(r *need.Likereq, username string) (err error) {
  117. ul := &need.UserLikes{
  118. ReqID: r.ReqID,
  119. User: username,
  120. LikeType: r.LikeType,
  121. }
  122. if err = d.DB.Create(&ul).Error; err != nil {
  123. err = errors.WithStack(err)
  124. return
  125. }
  126. return
  127. }
  128. //voteInfoCondition is
  129. func (d *Dao) voteInfoCondition(arg *need.Likereq) (where string) {
  130. where = " like_type != 0 "
  131. if arg.ReqID > 0 {
  132. where += fmt.Sprintf(" and `req_id`='%d'", arg.ReqID)
  133. }
  134. if arg.LikeType > 0 {
  135. where += fmt.Sprintf(" and `like_type`='%d'", arg.LikeType)
  136. }
  137. return where
  138. }
  139. //VoteInfoList is vote info
  140. func (d *Dao) VoteInfoList(arg *need.Likereq) (res []*need.UserLikes, err error) {
  141. where := d.voteInfoCondition(arg)
  142. if err = d.DB.Table(_tableLike).Where(where).Find(&res).Error; err != nil {
  143. err = errors.WithStack(err)
  144. log.Error("VoteInfoList:%s", err)
  145. return
  146. }
  147. return
  148. }
  149. //VoteInfoCounts vote info count
  150. func (d *Dao) VoteInfoCounts(arg *need.Likereq) (count int64, err error) {
  151. where := d.voteInfoCondition(arg)
  152. if err = d.DB.Table(_tableLike).Where(where).Count(&count).Error; err != nil {
  153. log.Error("VoteInfoCount:%s", err)
  154. return
  155. }
  156. return
  157. }