yb_community_question_comment.go 18 KB

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