yb_community_question_comment.go 16 KB

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