yb_community_question_comment.go 16 KB

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