123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- package community
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/models/request"
- "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/tables/research_group"
- "hongze/hongze_yb/models/tables/user_record"
- "hongze/hongze_yb/models/tables/yb_community_question"
- "hongze/hongze_yb/models/tables/yb_community_question_audio"
- "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/services/wechat"
- "hongze/hongze_yb/utils"
- "strconv"
- "strings"
- "time"
- )
- // GetQuestionList 获取问答列表
- func GetQuestionList(pageIndex, pageSize, onlyMine, chartPermissionId, replyStatus, groupId int, userInfo user.UserInfo) (resp []*response.CommunityQuestionItem, err error) {
- condition := make(map[string]interface{})
- condition["is_deleted ="] = 0
- // 用户身份
- isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
- if e != nil {
- err = errors.New("获取用户身份失败 Err:" + e.Error())
- return
- }
- if onlyMine == 1 {
- if isResearcher {
- condition["replier_user_id ="] = userInfo.UserID
- } else {
- condition["user_id ="] = userInfo.UserID
- }
- }
- if replyStatus > 0 {
- if replyStatus == 2 && !isResearcher {
- // 普通用户待回答为待分配和待回答
- condition["reply_status >"] = 0
- condition["reply_status <"] = 3
- } else {
- condition["reply_status ="] = replyStatus
- }
- } else {
- if isResearcher {
- // 不展示未分配的
- condition["reply_status >"] = 1
- }
- }
- if chartPermissionId > 0 {
- condition["chart_permission_id ="] = chartPermissionId
- }
- if groupId > 0 {
- condition["research_group_second_id ="] = groupId
- }
- // 问题列表
- questionList, e := yb_community_question.GetPageListByCondition(condition, pageIndex, pageSize)
- if e != nil {
- err = errors.New("获取问题列表失败 Err:" + e.Error())
- return
- }
- listLen := len(questionList)
- if listLen == 0 {
- return
- }
- idArr := make([]int, 0)
- for i := 0; i < listLen; i++ {
- idArr = append(idArr, questionList[i].CommunityQuestionID)
- }
- // 音频列表
- audioList, e := yb_community_question_audio.GetListByQuestrionIds(idArr)
- if e != nil {
- err = errors.New("获取音频列表失败 Err:" + e.Error())
- return
- }
- // 用户权限
- //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
- //if e != nil {
- // err = errors.New("获取用户权限失败 Err:" + e.Error())
- // return
- //}
- userId := int(userInfo.UserID)
- resp = make([]*response.CommunityQuestionItem, 0)
- for _, v := range questionList {
- audios := make([]*response.CommunityQuestionAudioItem, 0)
- for _, a := range audioList {
- if a.CommunityQuestionID == v.CommunityQuestionID {
- audios = append(audios, &response.CommunityQuestionAudioItem{
- CommunityQuestionID: a.CommunityQuestionID,
- AudioURL: a.AudioURL,
- AudioPlaySeconds: a.AudioPlaySeconds,
- AudioSize: a.AudioSize,
- Sort: a.Sort,
- })
- }
- }
- replierRank := fmt.Sprintf("弘则%s研究员", v.ResearchGroupFirstName)
- avatar := v.ReplierAvatar
- if avatar == "" {
- avatar = utils.HZ_DEFAULT_AVATAR
- }
- item := &response.CommunityQuestionItem{
- CommunityQuestionID: v.CommunityQuestionID,
- UserId: v.UserID,
- QuestionContent: v.QuestionContent,
- ReplierRealName: v.ReplierRealName,
- ReplierRank: replierRank,
- ReplierAvatar: avatar,
- ChartPermissionID: v.CommunityQuestionID,
- ChartPermissionName: v.ChartPermissionName,
- ResearchGroupSecondId: v.ResearchGroupSecondID,
- ResearchGroupSecondName: v.ResearchGroupSecondName,
- IsRead: v.IsRead,
- ReplierIsRead: v.ReplierIsRead,
- ReplyStatus: v.ReplyStatus,
- CreateTime: v.CreateTime.Format(utils.FormatDateTime),
- ReplyTime: v.ReplyTime.Format(utils.FormatDateTime),
- //AuthOk: authOk,
- //PermissionInfo: permissionInfo,
- AudioList: audios,
- }
- if !isResearcher && item.IsRead == 0 && item.UserId == userId {
- item.IsTop = 1
- }
- resp = append(resp, item)
- }
- return
- }
- // GetQuestionDetail 获取问答详情
- func GetQuestionDetail(questionId int, userInfo user.UserInfo) (item *response.CommunityQuestionItem, errMsg string, err error) {
- detail, e := yb_community_question.GetItemById(questionId)
- errMsg = "获取失败"
- if e != nil {
- if e == utils.ErrNoRow {
- errMsg = "问题已被删除"
- }
- err = errors.New("获取问题详情失败 Err:" + e.Error())
- return
- }
- audioList, e := yb_community_question_audio.GetListByQuestionId(questionId)
- if e != nil {
- err = errors.New("获取问题音频失败 Err:" + e.Error())
- return
- }
- audios := make([]*response.CommunityQuestionAudioItem, 0)
- for _, a := range audioList {
- audios = append(audios, &response.CommunityQuestionAudioItem{
- CommunityQuestionID: a.CommunityQuestionID,
- AudioURL: a.AudioURL,
- AudioPlaySeconds: a.AudioPlaySeconds,
- AudioSize: a.AudioSize,
- Sort: a.Sort,
- })
- }
- replierRank := fmt.Sprintf("弘则%s研究员", detail.ResearchGroupFirstName)
- // 用户权限
- //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
- //if e != nil {
- // err = errors.New("获取用户权限失败 Err:" + e.Error())
- // return
- //}
- avatar := detail.ReplierAvatar
- if avatar == "" {
- avatar = utils.HZ_DEFAULT_AVATAR
- }
- item = &response.CommunityQuestionItem{
- CommunityQuestionID: detail.CommunityQuestionID,
- UserId: detail.UserID,
- QuestionContent: detail.QuestionContent,
- ReplierRealName: detail.ReplierRealName,
- ReplierRank: replierRank,
- ReplierAvatar: detail.ReplierAvatar,
- ChartPermissionID: detail.ChartPermissionID,
- ChartPermissionName: detail.ChartPermissionName,
- ResearchGroupSecondId: detail.ResearchGroupSecondID,
- ResearchGroupSecondName: detail.ResearchGroupSecondName,
- IsRead: detail.IsRead,
- ReplierIsRead: detail.ReplierIsRead,
- ReplyStatus: detail.ReplyStatus,
- CreateTime: detail.CreateTime.Format(utils.FormatDateTime),
- ReplyTime: detail.ReplyTime.Format(utils.FormatDateTime),
- //AuthOk: authOk,
- //PermissionInfo: permissionInfo,
- AudioList: audios,
- }
- errMsg = "获取成功"
- return
- }
- // CreateQuestion 新增问答
- func CreateQuestion(userId int, mobile, realName, content string) (err error) {
- // 获取用户公众号openid, 获取不到则置空
- userRecord, e := user_record.GetByUserId(userId, utils.USER_RECORD_PLATFORM_RDDP)
- if e != nil && e != utils.ErrNoRow {
- err = errors.New("获取用户公众号openid失败 Err:" + e.Error())
- return
- }
- openid := ""
- if userRecord != nil {
- openid = userRecord.OpenID
- }
- item := &yb_community_question.YbCommunityQuestion{
- UserID: userId,
- UserOpenid: openid,
- Mobile: mobile,
- RealName: realName,
- QuestionContent: content,
- ReplyStatus: 1,
- IsRead: 1,
- ReplierIsRead: 1,
- }
- if e := item.Create(); e != nil {
- err = errors.New("新增问题失败 Err:" + e.Error())
- }
- return
- }
- // ReplyUserQuestion 回复问题
- func ReplyUserQuestion(replierId, questionId int, audios []*request.ReplyReqAudioList) (errMsg string, err error) {
- item, e := yb_community_question.GetItemById(questionId)
- if e != nil {
- errMsg = "问答信息有误"
- err = errors.New("获取提问信息失败 Err:" + e.Error())
- return
- }
- if item.ReplyStatus < 2 {
- errMsg = "回复状态有误"
- err = errors.New("回复状态有误")
- return
- }
- if item.ReplyStatus == 3 {
- errMsg = "请勿重复提交"
- err = errors.New("问题已回复,请勿重复提交")
- return
- }
- if item.ReplierUserID != replierId {
- errMsg = "回复人不匹配"
- err = errors.New(fmt.Sprintf("回复人与分配人不匹配, 当前回复人ID: %d, 分配的回复人ID: %d", replierId, item.ReplierUserID))
- return
- }
- // 问题
- updateCols := make([]string, 0)
- updateCols = append(updateCols, "reply_status", "reply_time", "modify_time", "msg_send_status", "is_read")
- nowTime := time.Now().Local()
- item.ReplyStatus = 3
- item.ReplyTime = nowTime
- item.ModifyTime = nowTime
- item.MsgSendStatus = 2
- item.IsRead = 0
- // 音频
- audioList := make([]*yb_community_question_audio.YbCommunityQuestionAudio, 0)
- for _, v := range audios {
- audioList = append(audioList, &yb_community_question_audio.YbCommunityQuestionAudio{
- CommunityQuestionID: questionId,
- AudioURL: v.AudioUrl,
- AudioPlaySeconds: v.AudioPlaySeconds,
- AudioSize: v.AudioSize,
- Sort: v.Sort,
- CreateTime: nowTime,
- })
- }
- if e := yb_community_question.UpdateQuestionAndAudioList(item, updateCols, audioList); e != nil {
- err = errors.New("UpdateQuestionAndAudioList Err:" + e.Error())
- return
- }
- // 推送回复消息给用户
- go wechat.SendQuestionReplyWxMsg(item.CommunityQuestionID, item.UserID, item.QuestionContent)
- return
- }
- // ReadQuestionReply 回复已读
- func ReadQuestionReply(questionIds string, userInfo user.UserInfo) (err error) {
- if questionIds == "" {
- return
- }
- questionIdArr := make([]int, 0)
- questionIdStrArr := strings.Split(questionIds, ",")
- for _, v := range questionIdStrArr {
- i, _ := strconv.Atoi(v)
- questionIdArr = append(questionIdArr, i)
- }
- if len(questionIdArr) == 0 {
- return
- }
- isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
- if e != nil {
- err = errors.New("获取用户身份失败 Err:" + e.Error())
- return
- }
- if isResearcher {
- e = yb_community_question.UpdateReplierRead(int(userInfo.UserID), questionIdArr)
- } else {
- e = yb_community_question.UpdateUserRead(int(userInfo.UserID), questionIdArr)
- }
- if e != nil {
- err = errors.New("更新问答已读失败 Err:" + e.Error())
- }
- return
- }
- // GetQuestionListTotal 获取问答列表数量统计
- func GetQuestionListTotal(userInfo user.UserInfo) (resp *response.CommunityQuestionListTotal, err error) {
- isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
- if e != nil {
- err = errors.New("获取用户身份失败 Err:" + e.Error())
- return
- }
- condition := make(map[string]interface{}, 0)
- condition["is_deleted ="] = 0
- if isResearcher {
- condition["replier_user_id ="] = userInfo.UserID
- } else {
- condition["user_id ="] = userInfo.UserID
- }
- countList, e := yb_community_question.GetQuestionListCount(condition)
- if e != nil {
- err = errors.New("获取回复人问题统计失败 Err:" + e.Error())
- return
- }
- resp = new(response.CommunityQuestionListTotal)
- var distribute, wait, replied int
- for _, v := range countList {
- if v.ReplyStatus == 1 {
- distribute = v.Total
- }
- if v.ReplyStatus == 2 {
- wait = v.Total
- }
- if v.ReplyStatus == 3 {
- replied = v.Total
- }
- }
- if isResearcher {
- resp.Wait = wait
- } else {
- resp.Wait = distribute + wait
- }
- resp.Replied = replied
- resp.Total = resp.Wait + resp.Replied
- return
- }
- // GetMyQuestionUnread 获取我的未读数
- func GetMyQuestionUnread(userInfo user.UserInfo) (total int, err error) {
- isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
- if e != nil {
- err = errors.New("获取用户身份失败 Err:" + e.Error())
- return
- }
- condition := make(map[string]interface{}, 0)
- condition["is_deleted ="] = 0
- if isResearcher {
- condition["replier_user_id ="] = userInfo.UserID
- condition["replier_is_read ="] = 0
- } else {
- condition["user_id ="] = userInfo.UserID
- condition["is_read ="] = 0
- }
- num, e := yb_community_question.GetUnreadNum(condition)
- if e != nil {
- err = errors.New("获取我的未读数失败 Err:" + e.Error())
- return
- }
- total = int(num)
- return
- }
- // GetResearchGroupTree 获取研究方向分组
- func GetResearchGroupTree() (respList []*response.ResearchGroupItem, err error) {
- respList = make([]*response.ResearchGroupItem, 0)
- list, e := research_group.GetResearchGroupList()
- if e != nil {
- err = errors.New("获取研究方向分组失败, Err:" + e.Error())
- return
- }
- listLen := len(list)
- if listLen == 0 {
- return
- }
- // 分类
- firstList := make([]*response.ResearchGroupItem, 0)
- secondList := make([]*response.ResearchGroupItem, 0)
- for i := 0; i < listLen; i++ {
- item := new(response.ResearchGroupItem)
- item.ResearchGroupId = list[i].ResearchGroupID
- item.ResearchGroupName = list[i].ResearchGroupName
- item.ParentId = list[i].ParentID
- item.ChartPermissionId = list[i].ChartPermissionID
- item.Sort = list[i].Sort
- if list[i].ParentID == 0 {
- firstList = append(firstList, item)
- } else {
- secondList = append(secondList, item)
- }
- }
- if len(firstList) == 0 {
- return
- }
- // 匹配子分类
- for _, p := range firstList {
- children := make([]*response.ResearchGroupItem, 0)
- for _, child := range secondList {
- if p.ResearchGroupId == child.ParentId {
- children = append(children, child)
- }
- }
- p.Children = children
- }
- respList = firstList
- return
- }
|