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" "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(user user.UserInfo, reportId, reportChapterId, oldReportId, oldReportChapterId int) (ret response.RespMyCommentList, err error) { list, err := List(user, reportId, reportChapterId, oldReportId, oldReportChapterId, false, 0, 0) if err != nil { return } //查询我的最新一条留言是否勾选匿名的设置 lastComment, tErr := yb_comment.GetMyLatestComment(user.UserID) if tErr == nil { ret.IsShowName = lastComment.IsShowName } ret.List = list.List return } // List 查询精选留言列表或我的留言列表 func List(user user.UserInfo, reportId, reportChapterId, oldReportId, oldReportChapterId int, hotFlag bool, pageIndex, pageSize int) (ret response.RespCommentList, err error) { var errMsg string defer func() { if err != nil { global.LOG.Critical(fmt.Sprintf("comment List: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg)) } }() if reportId <= 0 && reportChapterId <= 0 && oldReportId <= 0 && oldReportChapterId <= 0 { err = errors.New("请输入报告ID") return } if reportId <= 0 && oldReportId <= 0 { err = errors.New("请输入报告ID") return } //处理老报告, 转化成新报告ID if reportId <= 0 && reportChapterId <= 0 && (oldReportId > 0 || oldReportChapterId > 0) { reportId, reportChapterId, err, errMsg = services.GetReportIdReportChapterIdByOldReportId(uint64(oldReportId), uint64(oldReportChapterId)) if err != nil { return } } else { // 判断报告ID是否正确 err, errMsg = services.CheckReportExistByReportIdReportChapterId(reportId, reportChapterId) if err != nil { return } } offset := (pageIndex - 1) * pageSize var commentList []*yb_comment.YbComment var total int64 //查询精选留言 if hotFlag { if reportId > 0 { commentList, err = yb_comment.GetHotListByReportId(reportId, reportChapterId, offset, pageSize) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言列表出错") return } total, err = yb_comment.GetHotListTotalByReportId(reportId, reportChapterId) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言总数出错") return } } else { commentList, err = yb_comment.GetHotListByOldReportId(oldReportId, oldReportChapterId, offset, pageSize) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言列表出错") return } total, err = yb_comment.GetHotListTotalByOldReportId(oldReportId, oldReportChapterId) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言总数出错") return } } } else { //查询个人留言 if reportId > 0 { commentList, err = yb_comment.GetListByUserIdReportId(user.UserID, reportId, reportChapterId) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言列表出错") return } } else { commentList, err = yb_comment.GetListByUserIdOldReportId(user.UserID, oldReportId, oldReportChapterId) if err != nil { errMsg = err.Error() err = errors.New("查询精选留言列表出错") return } } } var commentIds []uint64 var userIds []uint64 for _, v := range commentList { commentIds = append(commentIds, v.CommentId) userIds = append(userIds, v.UserId) } // 查询所有的回复列表 replyListMap := make(map[uint64][]*response.ReplyItem) if len(commentIds) > 0 { replyList, tErr := yb_comment.GetReplyListByReplyCommentId(commentIds) if tErr != nil { errMsg = tErr.Error() err = errors.New("查询回复出错") return } for _, v := range replyList { t := new(response.ReplyItem) t.CommentId = v.CommentId 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 } var list []*response.RespCommentItem for _, v := range commentList { tmp := new(response.RespCommentItem) tmp.CommentId = v.CommentId tmp.UserId = v.UserId tmp.UserName = "匿名用户" + strconv.Itoa(int(3333+v.UserId)) tmp.UserImgUrl = utils.DEFAULT_HONGZE_USER_LOGO if info, ok := usersMap[v.UserId]; ok && v.IsShowName == 1 { tmp.UserName = info.NickName tmp.UserImgUrl = info.Headimgurl } tmp.Content = v.Content tmp.IsHot = v.IsHot tmp.IsTop = v.IsTop tmp.HotTopTime = v.HotTopTime tmp.CreateTime = v.CreateTime tmp.IsShowName = v.IsShowName if existList, ok := replyListMap[v.CommentId]; ok { tmp.ReplyList = existList } list = append(list, tmp) } ret.List = list if hotFlag { ret.Paging = response.GetPaging(pageIndex, pageSize, int(total)) } 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 }