yb_community_question_comment.go 12 KB

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