yb_community_question_comment.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/admin"
  8. "hongze/hongze_yb/models/tables/company"
  9. "hongze/hongze_yb/models/tables/company_product"
  10. "hongze/hongze_yb/models/tables/wx_user"
  11. "hongze/hongze_yb/models/tables/yb_comment_anonymous_user"
  12. "hongze/hongze_yb/models/tables/yb_community_question"
  13. "hongze/hongze_yb/models/tables/yb_community_question_comment"
  14. "hongze/hongze_yb/models/tables/yb_community_video"
  15. "hongze/hongze_yb/services/alarm_msg"
  16. "hongze/hongze_yb/services/company_approval_message"
  17. "hongze/hongze_yb/services/user"
  18. "hongze/hongze_yb/services/wechat"
  19. "hongze/hongze_yb/services/wx_app"
  20. "hongze/hongze_yb/utils"
  21. "strconv"
  22. "time"
  23. )
  24. // Comment 发布留言
  25. func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName, source int8, qaAvatarUrl string) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
  26. errMsg = "发布留言失败"
  27. defer func() {
  28. if err != nil {
  29. global.LOG.Critical(fmt.Sprintf("yb_community_question Comment: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  30. }
  31. }()
  32. //校验请求入参
  33. if isShowName != 0 && isShowName != 1 {
  34. errMsg = "匿名设置出错"
  35. err = errors.New(errMsg)
  36. return
  37. }
  38. if content == "" {
  39. errMsg = "请输入留言内容"
  40. err = errors.New(errMsg)
  41. return
  42. }
  43. if sourceAgent == 0 {
  44. errMsg = "请输入留言来源"
  45. err = errors.New(errMsg)
  46. return
  47. }
  48. if communityQuestionID <= 0 {
  49. errMsg = "请输入问答ID"
  50. err = errors.New(errMsg)
  51. return
  52. }
  53. if source <= 0 {
  54. errMsg = "来源有误"
  55. err = errors.New(errMsg)
  56. return
  57. }
  58. // 敏感词过滤
  59. if user.RecordInfo.OpenID != "" && user.RecordInfo.CreatePlatform == 6 { //只有小程序的用户才能走敏感词过滤接口
  60. checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content)
  61. /*if tErr != nil {
  62. errMsg = "敏感词过滤失败" + tErr.Error()
  63. err = errors.New("敏感词过滤失败")
  64. return
  65. }*/
  66. if tErr == nil {
  67. if checkResult.Result != nil {
  68. if checkResult.Result.Suggest != "pass" {
  69. errMsg = "含有违禁词,不允许发布:" + checkResult.Result.Suggest + ".命中标签:" + strconv.Itoa(checkResult.Result.Label)
  70. err = errors.New("含有违禁词,不允许发布。")
  71. return
  72. }
  73. }
  74. }
  75. }
  76. userName := "匿名用户" + strconv.Itoa(int(3333+user.UserID))
  77. if user.RealName != `` {
  78. userName = user.RealName
  79. }
  80. //新增留言
  81. now := time.Now()
  82. ybCommunityQuestionComment = &yb_community_question_comment.YbCommunityQuestionComment{
  83. //CommunityQuestionCommentID: 0,
  84. CommunityQuestionID: communityQuestionID,
  85. UserID: user.UserID,
  86. RealName: userName,
  87. Content: content,
  88. //ReplyCommentID: 0,
  89. //IsTop: 0,
  90. //IsHot: 0,
  91. //HotTopTime: time.Time{},
  92. Type: 1,
  93. Enabled: 1,
  94. IsShowName: isShowName,
  95. SourceAgent: sourceAgent,
  96. //TopTime: time.Time{},
  97. //HotTime: time.Time{},
  98. ModifyTime: now,
  99. CreateTime: now,
  100. QaAvatarUrl: qaAvatarUrl,
  101. Source: source,
  102. }
  103. err = ybCommunityQuestionComment.Create()
  104. if err != nil {
  105. errMsg = "新增留言失败"
  106. return
  107. }
  108. // 用户评论完后发送消息给管理员
  109. go messageToAdmin(user, ybCommunityQuestionComment)
  110. return
  111. }
  112. // Delete 删除留言
  113. func Delete(user user.UserInfo, communityQuestionCommentID uint64) (err error, errMsg string) {
  114. errMsg = `删除留言失败`
  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 communityQuestionCommentID <= 0 {
  121. errMsg = `请输入留言ID`
  122. err = errors.New(errMsg)
  123. return
  124. }
  125. //校验请求入参
  126. communityQuestionCommentInfo, err := yb_community_question_comment.GetByCommunityQuestionCommentId(communityQuestionCommentID)
  127. if err != nil {
  128. errMsg = `查询留言出错`
  129. return
  130. }
  131. if communityQuestionCommentInfo.CommunityQuestionCommentID <= 0 {
  132. errMsg = `留言不存在`
  133. err = errors.New(errMsg)
  134. return
  135. }
  136. if communityQuestionCommentInfo.UserID != user.UserID {
  137. errMsg = `不允许删除他人的留言`
  138. err = errors.New(errMsg)
  139. return
  140. }
  141. if communityQuestionCommentInfo.Type != 1 {
  142. errMsg = `不允许删除回复`
  143. err = errors.New(errMsg)
  144. return
  145. }
  146. if communityQuestionCommentInfo.Enabled == 0 {
  147. return
  148. }
  149. err = yb_community_question_comment.Delete(user.UserID, communityQuestionCommentInfo.CommunityQuestionCommentID)
  150. if err != nil {
  151. errMsg = `删除留言出错`
  152. return
  153. }
  154. go afterDelete(communityQuestionCommentInfo)
  155. return
  156. }
  157. // MyList 我的留言列表
  158. func MyList(userId uint64, communityQuestionID, source int, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, hotTotal, myTotal int64, err error, errMsg string) {
  159. list, hotTotal, myTotal, err, errMsg = List(userId, communityQuestionID, source, false, page, pageSize)
  160. if err != nil {
  161. return
  162. }
  163. return
  164. }
  165. // List 查询精选留言列表或我的留言列表
  166. func List(userId uint64, communityQuestionID, source int, hotFlag bool, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, hotTotal, myTotal int64, err error, errMsg string) {
  167. defer func() {
  168. if err != nil {
  169. global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
  170. }
  171. }()
  172. list = make([]*response.RespCommunityQuestionCommentItem, 0)
  173. if communityQuestionID <= 0 {
  174. err = errors.New("请输入问答ID")
  175. return
  176. }
  177. //精选留言数
  178. hotTotal, err = yb_community_question_comment.GetHotListTotalByCommunityQuestionID(communityQuestionID, source)
  179. if err != nil {
  180. errMsg = `查询精选留言总数出错`
  181. return
  182. }
  183. // 我的留言数
  184. myTotal, err = yb_community_question_comment.GetListTotalByUserIdCommunityQuestionID(userId, communityQuestionID, source)
  185. if err != nil {
  186. errMsg = `查询我的留言总数出错`
  187. return
  188. }
  189. var commentList []*yb_community_question_comment.YbCommunityQuestionComment
  190. //查询精选留言
  191. if hotFlag {
  192. commentList, err = yb_community_question_comment.GetHotListByCommunityQuestionID(communityQuestionID, (page-1)*pageSize, pageSize, source)
  193. if err != nil {
  194. errMsg = `查询精选留言列表出错`
  195. return
  196. }
  197. } else {
  198. //查询个人留言
  199. commentList, err = yb_community_question_comment.GetListByUserIdCommunityQuestionID(userId, communityQuestionID, source)
  200. if err != nil {
  201. errMsg = `查询我的留言列表出错`
  202. return
  203. }
  204. }
  205. var commentIds []uint64
  206. var userIds []uint64
  207. for _, v := range commentList {
  208. commentIds = append(commentIds, v.CommunityQuestionCommentID)
  209. userIds = append(userIds, v.UserID)
  210. }
  211. // 查询所有的回复列表
  212. //{
  213. // replyListMap := make(map[uint64][]*response.ReplyItem)
  214. // if len(commentIds) > 0 {
  215. // replyList, tErr := yb_community_question_comment.GetReplyListByReplyCommentId(commentIds)
  216. // if tErr != nil {
  217. // errMsg = tErr.Error()
  218. // err = errors.New("查询回复出错")
  219. // return
  220. // }
  221. // for _, v := range replyList {
  222. // t := new(response.ReplyItem)
  223. // t.CommentId = v.CommunityQuestionCommentID
  224. // t.Content = v.Content
  225. // t.AdminId = v.AdminID
  226. // t.CreateTime = v.CreateTime
  227. // t.ReplyCommentId = v.ReplyCommentId
  228. // t.AdminName = "弘则研究"
  229. // t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
  230. // replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
  231. // }
  232. // }
  233. //}
  234. // 查询精选留言相关的用户
  235. var userOthers []*wx_user.WxUser
  236. if len(userIds) > 0 {
  237. userOthers, err = wx_user.GetByUserIds(userIds)
  238. if err != nil {
  239. errMsg = err.Error()
  240. err = errors.New("查询精选留言用户出错")
  241. return
  242. }
  243. }
  244. usersMap := make(map[uint64]*wx_user.WxUser)
  245. for _, v := range userOthers {
  246. usersMap[v.UserID] = v
  247. }
  248. for _, v := range commentList {
  249. tmp := &response.RespCommunityQuestionCommentItem{
  250. CommunityQuestionCommentID: v.CommunityQuestionCommentID,
  251. UserId: v.UserID,
  252. Content: v.Content,
  253. IsTop: v.IsTop,
  254. IsHot: v.IsHot,
  255. HotTopTime: v.HotTopTime,
  256. IsShowName: v.IsShowName,
  257. UserName: "匿名用户" + strconv.Itoa(int(3333+v.UserID)),
  258. UserImgUrl: utils.DEFAULT_HONGZE_USER_LOGO,
  259. CreateTime: v.CreateTime,
  260. ReplyList: nil,
  261. QaAvatarUrl: v.QaAvatarUrl,
  262. }
  263. if info, ok := usersMap[v.UserID]; ok && v.IsShowName == 1 {
  264. tmp.UserName = info.NickName
  265. tmp.UserImgUrl = info.Headimgurl
  266. }
  267. //if existList, ok := replyListMap[v.CommentId]; ok {
  268. // tmp.ReplyList = existList
  269. //}
  270. list = append(list, tmp)
  271. }
  272. return
  273. }
  274. // GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
  275. func GetNeedCommentAnonymousUserTips(userId uint64) (ok bool, err error) {
  276. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  277. if err != nil {
  278. return
  279. }
  280. //如果能找到,那么认为是有设置过匿名评论,那么不需要需要弹框提示
  281. if ybCommentAnonymousUser.UserID > 0 {
  282. ok = true
  283. }
  284. return
  285. }
  286. // SetYbCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
  287. func SetYbCommentAnonymousUserTips(userId uint64) (err error) {
  288. ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
  289. if err != nil {
  290. return
  291. }
  292. //如果找不到,那么认为是第一次评论,那么需要弹框提示
  293. if ybCommentAnonymousUser.UserID <= 0 {
  294. //ybCommentAnonymousUser = &yb_comment_anonymous_user.YbCommentAnonymousUser{
  295. // UserID: userId,
  296. // CreateTime: time.Now(),
  297. //}
  298. //err = ybCommentAnonymousUser.Create()
  299. err = yb_comment_anonymous_user.CreateBySql(userId, time.Now())
  300. }
  301. return
  302. }
  303. // HandleCommentByCommunityQuestionItemList 问答 评论 数据
  304. func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
  305. listLen := len(questionList)
  306. if listLen == 0 {
  307. return
  308. }
  309. idArr := make([]uint32, 0)
  310. // 问题ID-精选评论列表
  311. questionIdCommentsMap := make(map[uint32][]*response.CommunityQuestionCommentListItem, 0)
  312. for i := 0; i < listLen; i++ {
  313. idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
  314. questionIdCommentsMap[uint32(questionList[i].CommunityQuestionID)] = make([]*response.CommunityQuestionCommentListItem, 0)
  315. }
  316. // 精选评论数据
  317. hotList, err := yb_community_question_comment.GetHotListByCommunityQuestionIds(idArr, yb_community_question_comment.SourceQuestion)
  318. if err != nil {
  319. return
  320. }
  321. for _, v := range hotList {
  322. questionIdCommentsMap[v.CommunityQuestionID] = append(questionIdCommentsMap[v.CommunityQuestionID], &response.CommunityQuestionCommentListItem{
  323. QaAvatarUrl: v.QaAvatarUrl,
  324. Comment: v.Content,
  325. })
  326. }
  327. for _, v := range questionList {
  328. comments := questionIdCommentsMap[uint32(v.CommunityQuestionID)]
  329. v.CommentTotal = len(comments)
  330. v.CommentList = comments
  331. }
  332. return
  333. }
  334. // messageToAdmin 添加站内消息
  335. func messageToAdmin(wxUser user.UserInfo, communityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment) {
  336. var err error
  337. defer func() {
  338. if err != nil {
  339. go alarm_msg.SendAlarmMsg("新增问答评论信息完成后,发送消息给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  340. }
  341. }()
  342. //因为产品说只要给沛总发送信息,那么没办法咯,只去获取沛总的信息 2022-07-19 11:29:16
  343. vWangInfo, err := admin.GetVWangInfo()
  344. if err != nil {
  345. return
  346. }
  347. //站内消息
  348. go systemMessageToAdmin(*vWangInfo, wxUser, communityQuestionComment)
  349. //微信模板消息(2022-0823评论后不再通知管理员)
  350. //go wxMessageToAdmin(*vWangInfo, communityQuestionComment)
  351. return
  352. }
  353. // systemMessageToAdmin 系统消息消息通知管理员
  354. func systemMessageToAdmin(adminInfo admin.Admin, wxUser user.UserInfo, communityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment) {
  355. var err error
  356. defer func() {
  357. if err != nil {
  358. go alarm_msg.SendAlarmMsg("新增问答评论信息完成后,站内评论信息发送给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  359. }
  360. }()
  361. // 接收人的admin_id
  362. receiveUserId := int(adminInfo.AdminID)
  363. var msgType, sourceType, approvalStatus int8
  364. msgType = company_approval_message.CompanyApprovalMessageMessageTypeByApply
  365. // 消息来源: 6-问答社区; 7-视频社区
  366. if communityQuestionComment.Source == 1 {
  367. sourceType = company_approval_message.CompanyApprovalMessageSourceTypeByQuestionComment
  368. } else {
  369. sourceType = company_approval_message.CompanyApprovalMessageSourceTypeByVideoComment
  370. }
  371. approvalStatus = company_approval_message.CompanyApprovalMessageApprovalStatusByPending
  372. companyInfo, err := company.GetByCompanyId(wxUser.CompanyID)
  373. if err != nil {
  374. return
  375. }
  376. productId := 1
  377. companyProductInfo, err := company_product.GetByCompany2ProductId(wxUser.CompanyID, int64(productId))
  378. if err != nil {
  379. return
  380. }
  381. companyName := companyInfo.CompanyName
  382. remark := `您有新的问答评论待查阅`
  383. content := `您有新的问答评论待查阅`
  384. if communityQuestionComment.Source == 2 {
  385. remark = `您有新的视频评论待查阅`
  386. content = `您有新的视频评论待查阅`
  387. }
  388. // 获取评论对应的问答/视频信息
  389. msgContent := ``
  390. extra := ``
  391. if communityQuestionComment.Source == 1 {
  392. communityQuestion, e := yb_community_question.GetItemById(int(communityQuestionComment.CommunityQuestionID))
  393. if e != nil {
  394. err = errors.New("获取评论对应的问答失败,err:" + e.Error())
  395. return
  396. }
  397. msgContent = communityQuestion.QuestionContent
  398. } else {
  399. communityVideo, e := yb_community_video.GetItemById(int(communityQuestionComment.CommunityQuestionID))
  400. if e != nil {
  401. err = errors.New("获取评论对应的视频失败,err:" + e.Error())
  402. return
  403. }
  404. msgContent = communityVideo.Title
  405. extra = communityVideo.VarietyTagName
  406. }
  407. messageInfo := company_approval_message.MessageInfo{
  408. CompanyName: companyInfo.CompanyName,
  409. ProductId: productId,
  410. CompanyProductStatus: companyProductInfo.Status,
  411. Title: communityQuestionComment.Content,
  412. Content: msgContent,
  413. UserId: wxUser.UserID,
  414. UserName: communityQuestionComment.RealName,
  415. CreateTime: communityQuestionComment.CreateTime,
  416. Extra: extra, // 附加字段
  417. }
  418. //客户添加消息
  419. err = company_approval_message.AddCompanyApprovalMessage(utils.AdminId, receiveUserId, int(wxUser.CompanyID), int(communityQuestionComment.CommunityQuestionCommentID), msgType, sourceType, approvalStatus, companyName, remark, content, messageInfo)
  420. return
  421. }
  422. // wxMessageToAdmin 微信模板消息通知管理员
  423. func wxMessageToAdmin(adminInfo admin.Admin, communityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment) {
  424. var err error
  425. defer func() {
  426. if err != nil {
  427. go alarm_msg.SendAlarmMsg("新增问答评论信息完成后,微信模板消息发送给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  428. }
  429. }()
  430. if global.CONFIG.Serve.RunMode == "debug" {
  431. adminInfo.Mobile = `18221983795`
  432. }
  433. err = wechat.SendQuestionCommentToAdmin(int(communityQuestionComment.CommunityQuestionCommentID), int(adminInfo.AdminID), adminInfo.OpenId, communityQuestionComment.Content)
  434. return
  435. }
  436. // 删除评论后的逻辑处理
  437. func afterDelete(communityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment) {
  438. var err error
  439. defer func() {
  440. if err != nil {
  441. go alarm_msg.SendAlarmMsg("问答评论信息删除后,标记站内消息失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  442. }
  443. }()
  444. sourceType := company_approval_message.CompanyApprovalMessageSourceTypeByQuestionComment
  445. if communityQuestionComment.Source == 2 {
  446. sourceType = company_approval_message.CompanyApprovalMessageSourceTypeByVideoComment
  447. }
  448. err = company_approval_message.CancelCompanyApprovalMessage(int(communityQuestionComment.CommunityQuestionCommentID), int8(sourceType))
  449. return
  450. }