yb_community_question_comment.go 16 KB

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