comment.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package comment
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. reqComment "hongze/hongze_yb/models/request/comment"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/wx_user"
  9. "hongze/hongze_yb/models/tables/yb_comment"
  10. "hongze/hongze_yb/services"
  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, req reqComment.ReqComment) (ret response.RespCommentAdd, err error) {
  19. var errMsg string
  20. defer func() {
  21. if err != nil {
  22. global.LOG.Critical(fmt.Sprintf("Comment: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  23. }
  24. }()
  25. //校验请求入参
  26. if req.IsShowName != 0 && req.IsShowName != 1 {
  27. err = errors.New("匿名设置出错")
  28. return
  29. }
  30. if req.Content == "" {
  31. err = errors.New("请输入留言内容")
  32. return
  33. }
  34. reportId := req.ReportId
  35. reportChapterId := req.ReportChapterId
  36. if req.SourceAgent == 0 {
  37. err = errors.New("请输入留言来源")
  38. return
  39. }
  40. if req.ReportChapterId <=0 && req.ReportId <=0 && req.OldReportId <=0 && req.OldReportChapterId <=0{
  41. err = errors.New("请输入报告ID")
  42. return
  43. }
  44. if req.ReportId <=0 && req.OldReportId <= 0 {
  45. err = errors.New("请输入报告ID")
  46. return
  47. }
  48. // 敏感词过滤
  49. if user.RecordInfo.OpenID != "" && user.RecordInfo.CreatePlatform == 6 { //只有小程序的用户才能走敏感词过滤接口
  50. checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, req.Content)
  51. /*if tErr != nil {
  52. errMsg = "敏感词过滤失败" + tErr.Error()
  53. err = errors.New("敏感词过滤失败")
  54. return
  55. }*/
  56. if tErr == nil {
  57. if checkResult.Result != nil {
  58. if checkResult.Result.Suggest != "pass" {
  59. errMsg = "含有违禁词,不允许发布:" + checkResult.Result.Suggest +".命中标签:"+strconv.Itoa(checkResult.Result.Label)
  60. err = errors.New("含有违禁词,不允许发布。")
  61. return
  62. }
  63. }
  64. }
  65. }
  66. // 判断是否有留言权限
  67. err, errMsg = services.CheckSimpleCompanyProduct(user)
  68. if err != nil {
  69. return
  70. }
  71. //处理老报告, 转化成新报告ID
  72. if req.ReportChapterId <=0 && req.ReportId <=0 && (req.OldReportId >0 || req.OldReportChapterId > 0) {
  73. reportId, reportChapterId, err, errMsg = services.GetReportIdReportChapterIdByOldReportId(uint64(req.OldReportId), uint64(req.OldReportChapterId))
  74. if err != nil {
  75. return
  76. }
  77. } else {
  78. // 判断报告ID是否正确
  79. err, errMsg = services.CheckReportExistByReportIdReportChapterId(reportId, reportChapterId)
  80. if err != nil {
  81. return
  82. }
  83. }
  84. //新增留言
  85. now := time.Now()
  86. commentInfo := &yb_comment.YbComment{
  87. UserId: user.UserID,
  88. RealName: user.RealName,
  89. Content: req.Content,
  90. ReportId: reportId,
  91. ReportChapterId: reportChapterId,
  92. IsShowName: req.IsShowName,
  93. SourceAgent: req.SourceAgent,
  94. Type: 1,
  95. Enabled: 1,
  96. CreateTime: now,
  97. ModifyTime: now,
  98. OldReportId: req.OldReportId,
  99. OldReportChapterId: req.OldReportChapterId,
  100. IsHot: 1,
  101. HotTopTime: time.Now(),
  102. }
  103. err = commentInfo.Create()
  104. if err != nil {
  105. errMsg = err.Error()
  106. err = errors.New("新增留言出错")
  107. return
  108. }
  109. ret.CommentId = commentInfo.CommentId
  110. return
  111. }
  112. // Delete 删除留言
  113. func Delete(user user.UserInfo, req *reqComment.ReqDel) (err error) {
  114. var errMsg string
  115. defer func() {
  116. if err != nil {
  117. global.LOG.Critical(fmt.Sprintf("comment Delete: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  118. }
  119. }()
  120. if req.CommentId <= 0 {
  121. err = errors.New("请输入留言ID")
  122. return
  123. }
  124. //校验请求入参
  125. commentInfo, err := yb_comment.GetSimpleByCommentId(req.CommentId)
  126. if err != nil {
  127. errMsg = err.Error()
  128. err = errors.New("查询留言出错")
  129. return
  130. }
  131. if commentInfo.CommentId <= 0 {
  132. err = errors.New("留言不存在")
  133. return
  134. }
  135. if commentInfo.UserId != user.UserID {
  136. err = errors.New("不允许删除他人的留言")
  137. return
  138. }
  139. if commentInfo.Type != 1 {
  140. err = errors.New("不允许删除回复")
  141. return
  142. }
  143. err = yb_comment.Delete(user.UserID, commentInfo.CommentId)
  144. if err != nil {
  145. errMsg = err.Error()
  146. err = errors.New("删除留言出错")
  147. return
  148. }
  149. return
  150. }
  151. // MyList 我的留言列表
  152. func MyList(user user.UserInfo, reportId, reportChapterId, oldReportId, oldReportChapterId int) (ret response.RespMyCommentList, err error) {
  153. list, err := List(user, reportId, reportChapterId, oldReportId, oldReportChapterId, false, 0, 0)
  154. if err != nil {
  155. return
  156. }
  157. //查询我的最新一条留言是否勾选匿名的设置
  158. lastComment, tErr := yb_comment.GetMyLatestComment(user.UserID)
  159. if tErr == nil {
  160. ret.IsShowName = lastComment.IsShowName
  161. }
  162. ret.List = list.List
  163. return
  164. }
  165. // List 查询精选留言列表或我的留言列表
  166. func List(user user.UserInfo, reportId, reportChapterId, oldReportId, oldReportChapterId int, hotFlag bool, pageIndex, pageSize int) (ret response.RespCommentList, err error) {
  167. var errMsg string
  168. defer func() {
  169. if err != nil {
  170. global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  171. }
  172. }()
  173. if reportId <=0 && reportChapterId <=0 && oldReportId <=0 && oldReportChapterId <=0{
  174. err = errors.New("请输入报告ID")
  175. return
  176. }
  177. if reportId <=0 && oldReportId <= 0 {
  178. err = errors.New("请输入报告ID")
  179. return
  180. }
  181. //处理老报告, 转化成新报告ID
  182. if reportId <=0 && reportChapterId <=0 && (oldReportId >0 || oldReportChapterId > 0) {
  183. reportId, reportChapterId, err, errMsg = services.GetReportIdReportChapterIdByOldReportId(uint64(oldReportId), uint64(oldReportChapterId))
  184. if err != nil {
  185. return
  186. }
  187. } else {
  188. // 判断报告ID是否正确
  189. err, errMsg = services.CheckReportExistByReportIdReportChapterId(reportId, reportChapterId)
  190. if err != nil {
  191. return
  192. }
  193. }
  194. offset := (pageIndex -1)*pageSize
  195. var commentList []*yb_comment.YbComment
  196. var total int64
  197. //查询精选留言
  198. if hotFlag {
  199. if reportId > 0 {
  200. commentList, err = yb_comment.GetHotListByReportId(reportId, reportChapterId, offset, pageSize)
  201. if err != nil {
  202. errMsg = err.Error()
  203. err = errors.New("查询精选留言列表出错")
  204. return
  205. }
  206. total, err = yb_comment.GetHotListTotalByReportId(reportId, reportChapterId)
  207. if err != nil {
  208. errMsg = err.Error()
  209. err = errors.New("查询精选留言总数出错")
  210. return
  211. }
  212. }else {
  213. commentList, err = yb_comment.GetHotListByOldReportId(oldReportId, oldReportChapterId, offset, pageSize)
  214. if err != nil {
  215. errMsg = err.Error()
  216. err = errors.New("查询精选留言列表出错")
  217. return
  218. }
  219. total, err = yb_comment.GetHotListTotalByOldReportId(oldReportId, oldReportChapterId)
  220. if err != nil {
  221. errMsg = err.Error()
  222. err = errors.New("查询精选留言总数出错")
  223. return
  224. }
  225. }
  226. } else {
  227. //查询个人留言
  228. if reportId > 0{
  229. commentList, err = yb_comment.GetListByUserIdReportId(user.UserID, reportId, reportChapterId)
  230. if err != nil {
  231. errMsg = err.Error()
  232. err = errors.New("查询精选留言列表出错")
  233. return
  234. }
  235. }else {
  236. commentList, err = yb_comment.GetListByUserIdOldReportId(user.UserID, oldReportId, oldReportChapterId)
  237. if err != nil {
  238. errMsg = err.Error()
  239. err = errors.New("查询精选留言列表出错")
  240. return
  241. }
  242. }
  243. }
  244. var commentIds []uint64
  245. var userIds []uint64
  246. for _, v := range commentList {
  247. commentIds = append(commentIds, v.CommentId)
  248. userIds = append(userIds, v.UserId)
  249. }
  250. // 查询所有的回复列表
  251. replyListMap := make(map[uint64][]*response.ReplyItem)
  252. if len(commentIds) > 0 {
  253. replyList, tErr := yb_comment.GetReplyListByReplyCommentId(commentIds)
  254. if tErr != nil {
  255. errMsg = tErr.Error()
  256. err = errors.New("查询回复出错")
  257. return
  258. }
  259. for _, v := range replyList {
  260. t := new(response.ReplyItem)
  261. t.CommentId = v.CommentId
  262. t.Content = v.Content
  263. t.AdminId = v.AdminId
  264. t.CreateTime = v.CreateTime
  265. t.ReplyCommentId = v.ReplyCommentId
  266. t.AdminName = "弘则研究"
  267. t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
  268. replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
  269. }
  270. }
  271. // 查询精选留言相关的用户
  272. var userOthers []*wx_user.WxUser
  273. if len(userIds) > 0 {
  274. userOthers, err = wx_user.GetByUserIds(userIds)
  275. if err != nil {
  276. errMsg = err.Error()
  277. err = errors.New("查询精选留言用户出错")
  278. return
  279. }
  280. }
  281. usersMap := make(map[uint64]*wx_user.WxUser)
  282. for _, v := range userOthers {
  283. usersMap[v.UserID] = v
  284. }
  285. var list []*response.RespCommentItem
  286. for _, v := range commentList {
  287. tmp := new(response.RespCommentItem)
  288. tmp.CommentId = v.CommentId
  289. tmp.UserId = v.UserId
  290. tmp.UserName = "匿名用户"+strconv.Itoa(int(3333+v.UserId))
  291. tmp.UserImgUrl = utils.DEFAULT_HONGZE_USER_LOGO
  292. if info, ok := usersMap[v.UserId]; ok && v.IsShowName == 1{
  293. tmp.UserName = info.NickName
  294. tmp.UserImgUrl = info.Headimgurl
  295. }
  296. tmp.Content = v.Content
  297. tmp.IsHot = v.IsHot
  298. tmp.IsTop = v.IsTop
  299. tmp.HotTopTime = v.HotTopTime
  300. tmp.CreateTime = v.CreateTime
  301. tmp.IsShowName = v.IsShowName
  302. if existList, ok := replyListMap[v.CommentId]; ok {
  303. tmp.ReplyList = existList
  304. }
  305. list = append(list, tmp)
  306. }
  307. ret.List = list
  308. if hotFlag {
  309. ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  310. }
  311. return
  312. }