need.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/apm/model/need"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. //NeedInfoAdd is
  10. func (s *Service) NeedInfoAdd(c context.Context, req *need.NAddReq, username string) (err error) {
  11. if err = s.dao.NeedInfoAdd(req, username); err != nil {
  12. return
  13. }
  14. return
  15. }
  16. //NeedInfoList is
  17. func (s *Service) NeedInfoList(c context.Context, arg *need.NListReq, username string) (res []*need.NInfo, count int64, err error) {
  18. var (
  19. like *need.UserLikes
  20. )
  21. if count, err = s.dao.NeedInfoCount(arg); err != nil {
  22. return
  23. }
  24. if count == 0 {
  25. return
  26. }
  27. if res, err = s.dao.NeedInfoList(arg); err != nil {
  28. return
  29. }
  30. for _, r := range res {
  31. lq := &need.Likereq{
  32. ReqID: r.ID,
  33. }
  34. if like, err = s.dao.GetVoteInfo(lq, username); err == nil {
  35. r.LikeState = like.LikeType
  36. }
  37. }
  38. err = nil
  39. return
  40. }
  41. //NeedInfoEdit is
  42. func (s *Service) NeedInfoEdit(c context.Context, arg *need.NEditReq, username string) (err error) {
  43. var (
  44. ni *need.NInfo
  45. )
  46. if ni, err = s.dao.GetNeedInfo(arg.ID); err != nil {
  47. err = ecode.NeedInfoErr
  48. return
  49. }
  50. if ni.Reporter != username {
  51. err = ecode.AccessDenied
  52. return
  53. }
  54. if ni.Status != 5 && ni.Status != 1 {
  55. err = ecode.NeedEditErr
  56. return
  57. }
  58. if err = s.dao.NeedInfoEdit(arg); err != nil {
  59. return
  60. }
  61. return
  62. }
  63. //NeedInfoVerify is
  64. func (s *Service) NeedInfoVerify(c context.Context, arg *need.NVerifyReq) (ni *need.NInfo, err error) {
  65. if ni, err = s.dao.GetNeedInfo(arg.ID); err != nil {
  66. err = ecode.NeedInfoErr
  67. return
  68. }
  69. if ni.Status != 1 {
  70. err = ecode.NeedVerifyErr
  71. return
  72. }
  73. if err = s.dao.NeedVerify(arg); err != nil {
  74. return
  75. }
  76. return
  77. }
  78. //NeedInfoVote is
  79. func (s *Service) NeedInfoVote(c context.Context, arg *need.Likereq, username string) (err error) {
  80. var (
  81. nu *need.UserLikes
  82. like, dislike int
  83. )
  84. if _, err = s.dao.GetNeedInfo(arg.ReqID); err != nil {
  85. err = ecode.NeedInfoErr
  86. return
  87. }
  88. if nu, err = s.dao.GetVoteInfo(arg, username); err != nil {
  89. if err = s.dao.AddVoteInfo(arg, username); err != nil {
  90. return
  91. }
  92. switch arg.LikeType {
  93. case need.TypeLike:
  94. like = 1
  95. dislike = 0
  96. case need.TypeDislike:
  97. like = 0
  98. dislike = 1
  99. }
  100. } else {
  101. if err = s.dao.UpdateVoteInfo(arg, username); err != nil {
  102. log.Error("arg.liketype:%d,db.like%+v,like:%d,dislike:%d", arg.LikeType, nu, like, dislike)
  103. return
  104. }
  105. if nu.LikeType != arg.LikeType {
  106. switch {
  107. case arg.LikeType == need.TypeCancel && nu.LikeType == need.TypeLike:
  108. like = -1
  109. case arg.LikeType == need.TypeCancel && nu.LikeType == need.TypeDislike:
  110. dislike = -1
  111. case arg.LikeType == need.TypeLike && nu.LikeType == need.TypeCancel:
  112. like = 1
  113. case arg.LikeType == need.TypeDislike && nu.LikeType == need.TypeCancel:
  114. dislike = 1
  115. case arg.LikeType == need.TypeLike && nu.LikeType == need.TypeDislike:
  116. like = 1
  117. dislike = -1
  118. case arg.LikeType == need.TypeDislike && nu.LikeType == need.TypeLike:
  119. like = -1
  120. dislike = 1
  121. }
  122. } else {
  123. return
  124. }
  125. }
  126. if err = s.dao.LikeCountsStats(arg, like, dislike); err != nil {
  127. if nu != nil {
  128. log.Error("arg.liketype:%d,db.like:%+v,like:%d,dislike:%d", arg.LikeType, nu, like, dislike)
  129. }
  130. return
  131. }
  132. return
  133. }
  134. //NeedVoteList is show votelist
  135. func (s *Service) NeedVoteList(c context.Context, arg *need.Likereq) (res []*need.UserLikes, count int64, err error) {
  136. if count, err = s.dao.VoteInfoCounts(arg); err != nil {
  137. return
  138. }
  139. if count == 0 {
  140. return
  141. }
  142. if res, err = s.dao.VoteInfoList(arg); err != nil {
  143. return
  144. }
  145. return
  146. }
  147. //SendWeMessage send wechat message
  148. func (s *Service) SendWeMessage(c context.Context, title, task, result, sender string, receiver []string) (err error) {
  149. var (
  150. msg = "[sven需求与建议来信 📨 ]\n"
  151. users []string
  152. )
  153. switch task {
  154. case need.VerifyType[need.NeedApply]:
  155. msg += fmt.Sprintf("%s童鞋认真地提了一份建议(%s),快去看看吧~\n%s\n", sender, title, "http://sven.bilibili.co/#/suggestion/list")
  156. users = append(users, receiver...)
  157. case need.VerifyType[need.NeedVerify]:
  158. msg += fmt.Sprintf("%s童鞋的建议(%s)反馈我们已经收到啦,先发一朵小红花感谢支持!🌺 \n", sender, title)
  159. users = append(users, sender)
  160. case need.VerifyType[need.NeedReview]:
  161. msg += fmt.Sprintf("%s童鞋的建议(%s)审核结果是%s, %s", receiver, title, result, "%s")
  162. users = append(users, receiver...)
  163. }
  164. switch result {
  165. case need.VerifyType[need.VerifyAccept]:
  166. msg = fmt.Sprintf(msg, "恭喜恭喜,喝杯快乐水开心一下~ 🍻 ")
  167. case need.VerifyType[need.VerifyReject]:
  168. msg = fmt.Sprintf(msg, "不要灰心,可能是需求描述或使用姿势不够准确,还请多多支持,欢迎再来!🙇‍ 🙆‍ ")
  169. case need.VerifyType[need.VerifyObserved]:
  170. msg = fmt.Sprintf(msg, "您的意见我们先保留啦,还请多多支持,欢迎再来补充!🙇‍ 🙆‍ ")
  171. }
  172. if err = s.dao.SendWechatToUsers(c, users, msg); err != nil {
  173. log.Error("apmSvc.SendWechatMessage error(%v)", err)
  174. return
  175. }
  176. return
  177. }