yb_community_question_comment.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package yb_community_question
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/wx_user"
  8. "hongze/hongze_yb/models/tables/yb_comment"
  9. "hongze/hongze_yb/models/tables/yb_comment_anonymous_user"
  10. "hongze/hongze_yb/models/tables/yb_community_question_comment"
  11. "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/services/wx_app"
  13. "hongze/hongze_yb/utils"
  14. "strconv"
  15. "time"
  16. )
  17. // Comment 发布留言
  18. func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName int8) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
  19. errMsg = "发布留言失败"
  20. defer func() {
  21. if err != nil {
  22. global.LOG.Critical(fmt.Sprintf("yb_community_question Comment: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  23. }
  24. }()
  25. //校验请求入参
  26. if isShowName != 0 && isShowName != 1 {
  27. errMsg = "匿名设置出错"
  28. err = errors.New(errMsg)
  29. return
  30. }
  31. if content == "" {
  32. errMsg = "请输入留言内容"
  33. err = errors.New(errMsg)
  34. return
  35. }
  36. if sourceAgent == 0 {
  37. errMsg = "请输入留言来源"
  38. err = errors.New(errMsg)
  39. return
  40. }
  41. if communityQuestionID <= 0 {
  42. errMsg = "请输入问答ID"
  43. err = errors.New(errMsg)
  44. return
  45. }
  46. // 敏感词过滤
  47. if user.RecordInfo.OpenID != "" && user.RecordInfo.CreatePlatform == 6 { //只有小程序的用户才能走敏感词过滤接口
  48. checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content)
  49. /*if tErr != nil {
  50. errMsg = "敏感词过滤失败" + tErr.Error()
  51. err = errors.New("敏感词过滤失败")
  52. return
  53. }*/
  54. if tErr == nil {
  55. if checkResult.Result != nil {
  56. if checkResult.Result.Suggest != "pass" {
  57. errMsg = "含有违禁词,不允许发布:" + checkResult.Result.Suggest + ".命中标签:" + strconv.Itoa(checkResult.Result.Label)
  58. err = errors.New("含有违禁词,不允许发布。")
  59. return
  60. }
  61. }
  62. }
  63. }
  64. //新增留言
  65. now := time.Now()
  66. ybCommunityQuestionComment = &yb_community_question_comment.YbCommunityQuestionComment{
  67. //CommunityQuestionCommentID: 0,
  68. CommunityQuestionID: communityQuestionID,
  69. UserID: user.UserID,
  70. Content: content,
  71. //ReplyCommentID: 0,
  72. //IsTop: 0,
  73. //IsHot: 0,
  74. //HotTopTime: time.Time{},
  75. Type: 1,
  76. Enabled: 1,
  77. IsShowName: isShowName,
  78. SourceAgent: sourceAgent,
  79. //TopTime: time.Time{},
  80. //HotTime: time.Time{},
  81. ModifyTime: now,
  82. CreateTime: now,
  83. }
  84. err = ybCommunityQuestionComment.Create()
  85. if err != nil {
  86. errMsg = "新增留言失败"
  87. return
  88. }
  89. // TODO 给管理员发送模板消息
  90. return
  91. }
  92. // Delete 删除留言
  93. func Delete(user user.UserInfo, communityQuestionCommentID uint64) (err error, errMsg string) {
  94. errMsg = `删除留言失败`
  95. defer func() {
  96. if err != nil {
  97. global.LOG.Critical(fmt.Sprintf("comment Delete: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  98. }
  99. }()
  100. if communityQuestionCommentID <= 0 {
  101. errMsg = `请输入留言ID`
  102. err = errors.New(errMsg)
  103. return
  104. }
  105. //校验请求入参
  106. communityQuestionCommentInfo, err := yb_community_question_comment.GetByCommunityQuestionCommentId(communityQuestionCommentID)
  107. if err != nil {
  108. errMsg = `查询留言出错`
  109. return
  110. }
  111. if communityQuestionCommentInfo.CommunityQuestionCommentID <= 0 {
  112. errMsg = `留言不存在`
  113. err = errors.New(errMsg)
  114. return
  115. }
  116. if communityQuestionCommentInfo.UserID != user.UserID {
  117. errMsg = `不允许删除他人的留言`
  118. err = errors.New(errMsg)
  119. return
  120. }
  121. if communityQuestionCommentInfo.Type != 1 {
  122. errMsg = `不允许删除回复`
  123. err = errors.New(errMsg)
  124. return
  125. }
  126. if communityQuestionCommentInfo.Enabled == 0 {
  127. return
  128. }
  129. err = yb_community_question_comment.Delete(user.UserID, communityQuestionCommentInfo.CommunityQuestionCommentID)
  130. if err != nil {
  131. errMsg = `删除留言出错`
  132. return
  133. }
  134. return
  135. }
  136. // MyList 我的留言列表
  137. func MyList(userId uint64, communityQuestionCommentID int, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, isShowName int8, err error, errMsg string) {
  138. list, total, err, errMsg = List(userId, communityQuestionCommentID, false, page, pageSize)
  139. if err != nil {
  140. return
  141. }
  142. //查询我的最新一条留言是否勾选匿名的设置
  143. lastComment, tErr := yb_comment.GetMyLatestComment(userId)
  144. if tErr == nil {
  145. isShowName = lastComment.IsShowName
  146. }
  147. return
  148. }
  149. // List 查询精选留言列表或我的留言列表
  150. func List(userId uint64, communityQuestionCommentID int, hotFlag bool, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, err error, errMsg string) {
  151. defer func() {
  152. if err != nil {
  153. global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  154. }
  155. }()
  156. list = make([]*response.RespCommunityQuestionCommentItem, 0)
  157. if communityQuestionCommentID <= 0 {
  158. err = errors.New("请输入问答ID")
  159. return
  160. }
  161. var commentList []*yb_community_question_comment.YbCommunityQuestionComment
  162. //查询精选留言
  163. if hotFlag {
  164. commentList, err = yb_community_question_comment.GetHotListByCommunityQuestionCommentID(communityQuestionCommentID, (page-1)*pageSize, pageSize)
  165. if err != nil {
  166. errMsg = `查询精选留言列表出错`
  167. return
  168. }
  169. total, err = yb_community_question_comment.GetHotListTotalByCommunityQuestionCommentID(communityQuestionCommentID)
  170. if err != nil {
  171. errMsg = `查询精选留言总数出错`
  172. return
  173. }
  174. } else {
  175. //查询个人留言
  176. commentList, err = yb_community_question_comment.GetListByUserIdCommunityQuestionCommentID(userId, communityQuestionCommentID)
  177. if err != nil {
  178. errMsg = `查询我的留言列表出错`
  179. return
  180. }
  181. total, err = yb_community_question_comment.GetListTotalByUserIdCommunityQuestionCommentID(userId, communityQuestionCommentID)
  182. if err != nil {
  183. errMsg = `查询我的留言总数出错`
  184. return
  185. }
  186. }
  187. var commentIds []uint64
  188. var userIds []uint64
  189. for _, v := range commentList {
  190. commentIds = append(commentIds, v.CommunityQuestionCommentID)
  191. userIds = append(userIds, v.UserID)
  192. }
  193. // 查询所有的回复列表
  194. //{
  195. // replyListMap := make(map[uint64][]*response.ReplyItem)
  196. // if len(commentIds) > 0 {
  197. // replyList, tErr := yb_community_question_comment.GetReplyListByReplyCommentId(commentIds)
  198. // if tErr != nil {
  199. // errMsg = tErr.Error()
  200. // err = errors.New("查询回复出错")
  201. // return
  202. // }
  203. // for _, v := range replyList {
  204. // t := new(response.ReplyItem)
  205. // t.CommentId = v.CommunityQuestionCommentID
  206. // t.Content = v.Content
  207. // t.AdminId = v.AdminID
  208. // t.CreateTime = v.CreateTime
  209. // t.ReplyCommentId = v.ReplyCommentId
  210. // t.AdminName = "弘则研究"
  211. // t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
  212. // replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
  213. // }
  214. // }
  215. //}
  216. // 查询精选留言相关的用户
  217. var userOthers []*wx_user.WxUser
  218. if len(userIds) > 0 {
  219. userOthers, err = wx_user.GetByUserIds(userIds)
  220. if err != nil {
  221. errMsg = err.Error()
  222. err = errors.New("查询精选留言用户出错")
  223. return
  224. }
  225. }
  226. usersMap := make(map[uint64]*wx_user.WxUser)
  227. for _, v := range userOthers {
  228. usersMap[v.UserID] = v
  229. }
  230. for _, v := range commentList {
  231. tmp := &response.RespCommunityQuestionCommentItem{
  232. CommunityQuestionCommentID: v.CommunityQuestionCommentID,
  233. UserId: v.UserID,
  234. Content: v.Content,
  235. IsTop: v.IsTop,
  236. IsHot: v.IsHot,
  237. HotTopTime: v.HotTopTime,
  238. IsShowName: v.IsShowName,
  239. UserName: "匿名用户" + strconv.Itoa(int(3333+v.UserID)),
  240. UserImgUrl: utils.DEFAULT_HONGZE_USER_LOGO,
  241. CreateTime: v.CreateTime,
  242. ReplyList: nil,
  243. }
  244. if info, ok := usersMap[v.UserID]; ok && v.IsShowName == 1 {
  245. tmp.UserName = info.NickName
  246. tmp.UserImgUrl = info.Headimgurl
  247. }
  248. //if existList, ok := replyListMap[v.CommentId]; ok {
  249. // tmp.ReplyList = existList
  250. //}
  251. list = append(list, tmp)
  252. }
  253. return
  254. }
  255. // GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
  256. func GetNeedCommentAnonymousUserTips(userId uint64) (ok bool, err error) {
  257. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  258. if err != nil {
  259. return
  260. }
  261. //如果找不到,那么认为是第一次评论,那么需要弹框提示
  262. if ybCommentAnonymousUser.UserID <= 0 {
  263. ok = true
  264. }
  265. return
  266. }
  267. // SetYbCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
  268. func SetYbCommentAnonymousUserTips(userId uint64) (err error) {
  269. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  270. if err != nil {
  271. return
  272. }
  273. //如果找不到,那么认为是第一次评论,那么需要弹框提示
  274. if ybCommentAnonymousUser.UserID <= 0 {
  275. //ybCommentAnonymousUser = &yb_comment_anonymous_user.YbCommentAnonymousUser{
  276. // UserID: userId,
  277. // CreateTime: time.Now(),
  278. //}
  279. //err = ybCommentAnonymousUser.Create()
  280. err = yb_comment_anonymous_user.CreateBySql(userId, time.Now())
  281. }
  282. return
  283. }
  284. // HandleCommentByCommunityQuestionItemList 问答 评论 数据
  285. func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
  286. listLen := len(questionList)
  287. if listLen == 0 {
  288. return
  289. }
  290. idArr := make([]uint32, 0)
  291. idMap := make(map[uint32]int)
  292. for i := 0; i < listLen; i++ {
  293. idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
  294. idMap[uint32(questionList[i].CommunityQuestionID)] = 1
  295. }
  296. //精选评论数据、我的评论数据
  297. ybCommunityQuestionCommentMap := make(map[uint32]*yb_community_question_comment.YbCommunityQuestionComment)
  298. //获取精选评论数据
  299. hotList, err := yb_community_question_comment.GetLastHotListByCommunityQuestionIds(idArr)
  300. if err != nil {
  301. return
  302. }
  303. for _, v := range hotList {
  304. ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
  305. delete(idMap, v.CommunityQuestionID)
  306. }
  307. myIdArr := make([]uint32, 0)
  308. for id := range idMap {
  309. myIdArr = append(myIdArr, id)
  310. }
  311. //获取我的评论数据
  312. myList, err := yb_community_question_comment.GetLastMyListByCommunityQuestionIds(userId, idArr)
  313. if err != nil {
  314. return
  315. }
  316. for _, v := range myList {
  317. ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
  318. }
  319. // 获取留言汇总数
  320. numCommentMap := make(map[uint32]int)
  321. numCommentList, err := yb_community_question_comment.GetNumCommentByCommunityQuestionIds(idArr)
  322. if err != nil {
  323. return
  324. }
  325. for _, v := range numCommentList {
  326. numCommentMap[v.CommunityQuestionID] = v.Total
  327. }
  328. for _, v := range questionList {
  329. //评论汇总数
  330. if tmpTotal, ok := numCommentMap[uint32(v.CommunityQuestionID)]; ok {
  331. v.CommentTotal = tmpTotal
  332. }
  333. //最近一条评论
  334. if ybCommunityQuestionCommentInfo, ok := ybCommunityQuestionCommentMap[uint32(v.CommunityQuestionID)]; ok {
  335. v.Comment = ybCommunityQuestionCommentInfo.Content
  336. }
  337. }
  338. return
  339. }