123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- package yb_community_question
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/tables/wx_user"
- "hongze/hongze_yb/models/tables/yb_comment"
- "hongze/hongze_yb/models/tables/yb_comment_anonymous_user"
- "hongze/hongze_yb/models/tables/yb_community_question_comment"
- "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/services/wx_app"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- // Comment 发布留言
- func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName int8) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
- errMsg = "发布留言失败"
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("yb_community_question Comment: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
- }
- }()
- //校验请求入参
- if isShowName != 0 && isShowName != 1 {
- errMsg = "匿名设置出错"
- err = errors.New(errMsg)
- return
- }
- if content == "" {
- errMsg = "请输入留言内容"
- err = errors.New(errMsg)
- return
- }
- if sourceAgent == 0 {
- errMsg = "请输入留言来源"
- err = errors.New(errMsg)
- return
- }
- if communityQuestionID <= 0 {
- errMsg = "请输入问答ID"
- err = errors.New(errMsg)
- return
- }
- // 敏感词过滤
- if user.RecordInfo.OpenID != "" && user.RecordInfo.CreatePlatform == 6 { //只有小程序的用户才能走敏感词过滤接口
- checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content)
- /*if tErr != nil {
- errMsg = "敏感词过滤失败" + tErr.Error()
- err = errors.New("敏感词过滤失败")
- return
- }*/
- if tErr == nil {
- if checkResult.Result != nil {
- if checkResult.Result.Suggest != "pass" {
- errMsg = "含有违禁词,不允许发布:" + checkResult.Result.Suggest + ".命中标签:" + strconv.Itoa(checkResult.Result.Label)
- err = errors.New("含有违禁词,不允许发布。")
- return
- }
- }
- }
- }
- //新增留言
- now := time.Now()
- ybCommunityQuestionComment = &yb_community_question_comment.YbCommunityQuestionComment{
- //CommunityQuestionCommentID: 0,
- CommunityQuestionID: communityQuestionID,
- UserID: user.UserID,
- Content: content,
- //ReplyCommentID: 0,
- //IsTop: 0,
- //IsHot: 0,
- //HotTopTime: time.Time{},
- Type: 1,
- Enabled: 1,
- IsShowName: isShowName,
- SourceAgent: sourceAgent,
- //TopTime: time.Time{},
- //HotTime: time.Time{},
- ModifyTime: now,
- CreateTime: now,
- }
- err = ybCommunityQuestionComment.Create()
- if err != nil {
- errMsg = "新增留言失败"
- return
- }
- // TODO 给管理员发送模板消息
- return
- }
- // Delete 删除留言
- func Delete(user user.UserInfo, communityQuestionCommentID uint64) (err error, errMsg string) {
- errMsg = `删除留言失败`
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("comment Delete: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
- }
- }()
- if communityQuestionCommentID <= 0 {
- errMsg = `请输入留言ID`
- err = errors.New(errMsg)
- return
- }
- //校验请求入参
- communityQuestionCommentInfo, err := yb_community_question_comment.GetByCommunityQuestionCommentId(communityQuestionCommentID)
- if err != nil {
- errMsg = `查询留言出错`
- return
- }
- if communityQuestionCommentInfo.CommunityQuestionCommentID <= 0 {
- errMsg = `留言不存在`
- err = errors.New(errMsg)
- return
- }
- if communityQuestionCommentInfo.UserID != user.UserID {
- errMsg = `不允许删除他人的留言`
- err = errors.New(errMsg)
- return
- }
- if communityQuestionCommentInfo.Type != 1 {
- errMsg = `不允许删除回复`
- err = errors.New(errMsg)
- return
- }
- if communityQuestionCommentInfo.Enabled == 0 {
- return
- }
- err = yb_community_question_comment.Delete(user.UserID, communityQuestionCommentInfo.CommunityQuestionCommentID)
- if err != nil {
- errMsg = `删除留言出错`
- return
- }
- return
- }
- // MyList 我的留言列表
- func MyList(userId uint64, communityQuestionCommentID int, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, isShowName int8, err error, errMsg string) {
- list, total, err, errMsg = List(userId, communityQuestionCommentID, false, page, pageSize)
- if err != nil {
- return
- }
- //查询我的最新一条留言是否勾选匿名的设置
- lastComment, tErr := yb_comment.GetMyLatestComment(userId)
- if tErr == nil {
- isShowName = lastComment.IsShowName
- }
- return
- }
- // List 查询精选留言列表或我的留言列表
- func List(userId uint64, communityQuestionCommentID int, hotFlag bool, page, pageSize int) (list []*response.RespCommunityQuestionCommentItem, total int64, err error, errMsg string) {
- defer func() {
- if err != nil {
- global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", userId, err.Error(), errMsg))
- }
- }()
- list = make([]*response.RespCommunityQuestionCommentItem, 0)
- if communityQuestionCommentID <= 0 {
- err = errors.New("请输入问答ID")
- return
- }
- var commentList []*yb_community_question_comment.YbCommunityQuestionComment
- //查询精选留言
- if hotFlag {
- commentList, err = yb_community_question_comment.GetHotListByCommunityQuestionCommentID(communityQuestionCommentID, (page-1)*pageSize, pageSize)
- if err != nil {
- errMsg = `查询精选留言列表出错`
- return
- }
- total, err = yb_community_question_comment.GetHotListTotalByCommunityQuestionCommentID(communityQuestionCommentID)
- if err != nil {
- errMsg = `查询精选留言总数出错`
- return
- }
- } else {
- //查询个人留言
- commentList, err = yb_community_question_comment.GetListByUserIdCommunityQuestionCommentID(userId, communityQuestionCommentID)
- if err != nil {
- errMsg = `查询我的留言列表出错`
- return
- }
- total, err = yb_community_question_comment.GetListTotalByUserIdCommunityQuestionCommentID(userId, communityQuestionCommentID)
- if err != nil {
- errMsg = `查询我的留言总数出错`
- return
- }
- }
- var commentIds []uint64
- var userIds []uint64
- for _, v := range commentList {
- commentIds = append(commentIds, v.CommunityQuestionCommentID)
- userIds = append(userIds, v.UserID)
- }
- // 查询所有的回复列表
- //{
- // replyListMap := make(map[uint64][]*response.ReplyItem)
- // if len(commentIds) > 0 {
- // replyList, tErr := yb_community_question_comment.GetReplyListByReplyCommentId(commentIds)
- // if tErr != nil {
- // errMsg = tErr.Error()
- // err = errors.New("查询回复出错")
- // return
- // }
- // for _, v := range replyList {
- // t := new(response.ReplyItem)
- // t.CommentId = v.CommunityQuestionCommentID
- // t.Content = v.Content
- // t.AdminId = v.AdminID
- // t.CreateTime = v.CreateTime
- // t.ReplyCommentId = v.ReplyCommentId
- // t.AdminName = "弘则研究"
- // t.AdminImgUrl = utils.DEFAULT_HONGZE_SYS_LOGO
- // replyListMap[v.ReplyCommentId] = append(replyListMap[v.ReplyCommentId], t)
- // }
- // }
- //}
- // 查询精选留言相关的用户
- var userOthers []*wx_user.WxUser
- if len(userIds) > 0 {
- userOthers, err = wx_user.GetByUserIds(userIds)
- if err != nil {
- errMsg = err.Error()
- err = errors.New("查询精选留言用户出错")
- return
- }
- }
- usersMap := make(map[uint64]*wx_user.WxUser)
- for _, v := range userOthers {
- usersMap[v.UserID] = v
- }
- for _, v := range commentList {
- tmp := &response.RespCommunityQuestionCommentItem{
- CommunityQuestionCommentID: v.CommunityQuestionCommentID,
- UserId: v.UserID,
- Content: v.Content,
- IsTop: v.IsTop,
- IsHot: v.IsHot,
- HotTopTime: v.HotTopTime,
- IsShowName: v.IsShowName,
- UserName: "匿名用户" + strconv.Itoa(int(3333+v.UserID)),
- UserImgUrl: utils.DEFAULT_HONGZE_USER_LOGO,
- CreateTime: v.CreateTime,
- ReplyList: nil,
- }
- if info, ok := usersMap[v.UserID]; ok && v.IsShowName == 1 {
- tmp.UserName = info.NickName
- tmp.UserImgUrl = info.Headimgurl
- }
- //if existList, ok := replyListMap[v.CommentId]; ok {
- // tmp.ReplyList = existList
- //}
- list = append(list, tmp)
- }
- return
- }
- // GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
- func GetNeedCommentAnonymousUserTips(userId uint64) (ok bool, err error) {
- ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
- if err != nil {
- return
- }
- //如果找不到,那么认为是第一次评论,那么需要弹框提示
- if ybCommentAnonymousUser.UserID <= 0 {
- ok = true
- }
- return
- }
- // SetYbCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
- func SetYbCommentAnonymousUserTips(userId uint64) (err error) {
- ybCommentAnonymousUser, err := yb_comment_anonymous_user.GetByUserId(userId)
- if err != nil {
- return
- }
- //如果找不到,那么认为是第一次评论,那么需要弹框提示
- if ybCommentAnonymousUser.UserID <= 0 {
- //ybCommentAnonymousUser = &yb_comment_anonymous_user.YbCommentAnonymousUser{
- // UserID: userId,
- // CreateTime: time.Now(),
- //}
- //err = ybCommentAnonymousUser.Create()
- err = yb_comment_anonymous_user.CreateBySql(userId, time.Now())
- }
- return
- }
- // HandleCommentByCommunityQuestionItemList 问答 评论 数据
- func HandleCommentByCommunityQuestionItemList(userId uint64, questionList []*response.CommunityQuestionItem) (err error) {
- listLen := len(questionList)
- if listLen == 0 {
- return
- }
- idArr := make([]uint32, 0)
- idMap := make(map[uint32]int)
- for i := 0; i < listLen; i++ {
- idArr = append(idArr, uint32(questionList[i].CommunityQuestionID))
- idMap[uint32(questionList[i].CommunityQuestionID)] = 1
- }
- //精选评论数据、我的评论数据
- ybCommunityQuestionCommentMap := make(map[uint32]*yb_community_question_comment.YbCommunityQuestionComment)
- //获取精选评论数据
- hotList, err := yb_community_question_comment.GetLastHotListByCommunityQuestionIds(idArr)
- if err != nil {
- return
- }
- for _, v := range hotList {
- ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
- delete(idMap, v.CommunityQuestionID)
- }
- myIdArr := make([]uint32, 0)
- for id := range idMap {
- myIdArr = append(myIdArr, id)
- }
- //获取我的评论数据
- myList, err := yb_community_question_comment.GetLastMyListByCommunityQuestionIds(userId, idArr)
- if err != nil {
- return
- }
- for _, v := range myList {
- ybCommunityQuestionCommentMap[v.CommunityQuestionID] = v
- }
- // 获取留言汇总数
- numCommentMap := make(map[uint32]int)
- numCommentList, err := yb_community_question_comment.GetNumCommentByCommunityQuestionIds(idArr)
- if err != nil {
- return
- }
- for _, v := range numCommentList {
- numCommentMap[v.CommunityQuestionID] = v.Total
- }
- for _, v := range questionList {
- //评论汇总数
- if tmpTotal, ok := numCommentMap[uint32(v.CommunityQuestionID)]; ok {
- v.CommentTotal = tmpTotal
- }
- //最近一条评论
- if ybCommunityQuestionCommentInfo, ok := ybCommunityQuestionCommentMap[uint32(v.CommunityQuestionID)]; ok {
- v.Comment = ybCommunityQuestionCommentInfo.Content
- }
- }
- return
- }
|