yb_community_question_comment.go 16 KB

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