question.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package community
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/models/request"
  6. "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/research_group"
  8. "hongze/hongze_yb/models/tables/user_record"
  9. "hongze/hongze_yb/models/tables/yb_community_audio_listen_log"
  10. "hongze/hongze_yb/models/tables/yb_community_question"
  11. "hongze/hongze_yb/models/tables/yb_community_question_audio"
  12. "hongze/hongze_yb/services/user"
  13. "hongze/hongze_yb/services/wechat"
  14. "hongze/hongze_yb/utils"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // GetQuestionList 获取问答列表
  20. func GetQuestionList(pageIndex, pageSize, onlyMine, varietyTagId, replyStatus, groupId int, userInfo user.UserInfo) (resp []*response.CommunityQuestionItem, err error) {
  21. condition := " is_deleted = 0"
  22. var pars []interface{}
  23. // 用户身份
  24. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  25. if e != nil {
  26. err = errors.New("获取用户身份失败 Err:" + e.Error())
  27. return
  28. }
  29. if onlyMine == 1 {
  30. if isResearcher { //如果是研究员
  31. if replyStatus == 4 { //分配给研究员未回答的问题
  32. condition += " and replier_user_id=? and reply_status = 2"
  33. pars = append(pars, userInfo.UserID)
  34. } else if replyStatus == 2 { //研究员提问的问题未分配或者未回答的问题
  35. condition += " and user_id=? and reply_status >0 and reply_status <3"
  36. pars = append(pars, userInfo.UserID)
  37. } else if replyStatus == 3 { //分配给研究员的已回答和研究员提问的被回答的问题
  38. condition += " and (replier_user_id=? or user_id=?) and reply_status =3"
  39. pars = append(pars, userInfo.UserID, userInfo.UserID)
  40. }else if replyStatus == 0 { //分配给研究员或者研究员提问的所以问题
  41. condition += " and (replier_user_id=? or user_id=?)"
  42. pars = append(pars, userInfo.UserID, userInfo.UserID)
  43. }
  44. } else {
  45. condition += " and user_id=?"
  46. pars = append(pars, userInfo.UserID)
  47. if replyStatus == 2 { // 普通用户未回答为待分配和未回答
  48. condition += " and reply_status >0 and reply_status <3"
  49. } else if replyStatus == 3{
  50. condition += " and reply_status = 3"
  51. }
  52. }
  53. }else{
  54. if replyStatus == 3 {
  55. condition += " and reply_status = 3"
  56. }
  57. }
  58. if varietyTagId > 0 {
  59. condition += " and variety_tag_id =?"
  60. pars = append(pars, varietyTagId)
  61. }
  62. if groupId > 0 {
  63. condition += " and research_group_second_id =?"
  64. pars = append(pars, groupId)
  65. }
  66. // 问题列表
  67. questionList, e := yb_community_question.GetPageListByCondition(condition, pars, pageIndex, pageSize)
  68. if e != nil {
  69. err = errors.New("获取问题列表失败 Err:" + e.Error())
  70. return
  71. }
  72. listLen := len(questionList)
  73. if listLen == 0 {
  74. return
  75. }
  76. idArr := make([]int, 0)
  77. for i := 0; i < listLen; i++ {
  78. idArr = append(idArr, questionList[i].CommunityQuestionID)
  79. }
  80. // 音频列表
  81. audioList, e := yb_community_question_audio.GetListByQuestrionIds(idArr)
  82. if e != nil {
  83. err = errors.New("获取音频列表失败 Err:" + e.Error())
  84. return
  85. }
  86. // 用户权限
  87. //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
  88. //if e != nil {
  89. // err = errors.New("获取用户权限失败 Err:" + e.Error())
  90. // return
  91. //}
  92. userId := int(userInfo.UserID)
  93. resp = make([]*response.CommunityQuestionItem, 0)
  94. for _, v := range questionList {
  95. audios := make([]*response.CommunityQuestionAudioItem, 0)
  96. for _, a := range audioList {
  97. if a.CommunityQuestionID == v.CommunityQuestionID {
  98. audios = append(audios, &response.CommunityQuestionAudioItem{
  99. CommunityQuestionAudioID: a.CommunityQuestionAudioID,
  100. CommunityQuestionID: a.CommunityQuestionID,
  101. AudioURL: a.AudioURL,
  102. AudioPlaySeconds: a.AudioPlaySeconds,
  103. AudioSize: a.AudioSize,
  104. Sort: a.Sort,
  105. })
  106. }
  107. }
  108. replierRank := fmt.Sprintf("弘则%s研究员", v.ResearchGroupFirstName)
  109. avatar := v.ReplierAvatar
  110. if avatar == "" {
  111. avatar = utils.HZ_DEFAULT_AVATAR
  112. }
  113. item := &response.CommunityQuestionItem{
  114. CommunityQuestionID: v.CommunityQuestionID,
  115. UserId: v.UserID,
  116. QuestionContent: v.QuestionContent,
  117. ReplierRealName: v.ReplierRealName,
  118. ReplierRank: replierRank,
  119. ReplierAvatar: avatar,
  120. VarietyTagId: v.VarietyTagID,
  121. VarietyTagName: v.VarietyTagName,
  122. ResearchGroupSecondId: v.ResearchGroupSecondID,
  123. ResearchGroupSecondName: v.ResearchGroupSecondName,
  124. IsRead: v.IsRead,
  125. ReplierUserID: v.ReplierUserID,
  126. ReplierIsRead: v.ReplierIsRead,
  127. ReplyStatus: v.ReplyStatus,
  128. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  129. ReplyTime: v.ReplyTime.Format(utils.FormatDateTime),
  130. //AuthOk: authOk,
  131. //PermissionInfo: permissionInfo,
  132. AudioList: audios,
  133. }
  134. if item.IsRead == 0 && item.UserId == userId {
  135. item.IsTop = 1
  136. }
  137. resp = append(resp, item)
  138. }
  139. return
  140. }
  141. // GetQuestionDetail 获取问答详情
  142. func GetQuestionDetail(questionId int, userInfo user.UserInfo) (item *response.CommunityQuestionItem, errMsg string, err error) {
  143. detail, e := yb_community_question.GetItemById(questionId)
  144. errMsg = "获取失败"
  145. if e != nil {
  146. if e == utils.ErrNoRow {
  147. errMsg = "问题已被删除"
  148. }
  149. err = errors.New("获取问题详情失败 Err:" + e.Error())
  150. return
  151. }
  152. audioList, e := yb_community_question_audio.GetListByQuestionId(questionId)
  153. if e != nil {
  154. err = errors.New("获取问题音频失败 Err:" + e.Error())
  155. return
  156. }
  157. audios := make([]*response.CommunityQuestionAudioItem, 0)
  158. for _, a := range audioList {
  159. audios = append(audios, &response.CommunityQuestionAudioItem{
  160. CommunityQuestionAudioID: a.CommunityQuestionAudioID,
  161. CommunityQuestionID: a.CommunityQuestionID,
  162. AudioURL: a.AudioURL,
  163. AudioPlaySeconds: a.AudioPlaySeconds,
  164. AudioSize: a.AudioSize,
  165. Sort: a.Sort,
  166. })
  167. }
  168. replierRank := fmt.Sprintf("弘则%s研究员", detail.ResearchGroupFirstName)
  169. // 用户权限
  170. //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
  171. //if e != nil {
  172. // err = errors.New("获取用户权限失败 Err:" + e.Error())
  173. // return
  174. //}
  175. avatar := detail.ReplierAvatar
  176. if avatar == "" {
  177. avatar = utils.HZ_DEFAULT_AVATAR
  178. }
  179. item = &response.CommunityQuestionItem{
  180. CommunityQuestionID: detail.CommunityQuestionID,
  181. UserId: detail.UserID,
  182. QuestionContent: detail.QuestionContent,
  183. ReplierRealName: detail.ReplierRealName,
  184. ReplierRank: replierRank,
  185. ReplierAvatar: detail.ReplierAvatar,
  186. VarietyTagId: detail.VarietyTagID,
  187. VarietyTagName: detail.VarietyTagName,
  188. ResearchGroupSecondId: detail.ResearchGroupSecondID,
  189. ResearchGroupSecondName: detail.ResearchGroupSecondName,
  190. IsRead: detail.IsRead,
  191. ReplierIsRead: detail.ReplierIsRead,
  192. ReplyStatus: detail.ReplyStatus,
  193. CreateTime: detail.CreateTime.Format(utils.FormatDateTime),
  194. ReplyTime: detail.ReplyTime.Format(utils.FormatDateTime),
  195. //AuthOk: authOk,
  196. //PermissionInfo: permissionInfo,
  197. AudioList: audios,
  198. }
  199. errMsg = "获取成功"
  200. return
  201. }
  202. // CreateQuestion 新增问答
  203. func CreateQuestion(userId int, mobile, realName, content string) (err error) {
  204. // 获取用户公众号openid, 获取不到则置空
  205. userRecord, e := user_record.GetByUserId(userId, utils.USER_RECORD_PLATFORM_RDDP)
  206. if e != nil && e != utils.ErrNoRow {
  207. err = errors.New("获取用户公众号openid失败 Err:" + e.Error())
  208. return
  209. }
  210. openid := ""
  211. if userRecord != nil {
  212. openid = userRecord.OpenID
  213. }
  214. item := &yb_community_question.YbCommunityQuestion{
  215. UserID: userId,
  216. UserOpenid: openid,
  217. Mobile: mobile,
  218. RealName: realName,
  219. QuestionContent: content,
  220. ReplyStatus: 1,
  221. IsRead: 1,
  222. ReplierIsRead: 1,
  223. }
  224. if e := item.Create(); e != nil {
  225. err = errors.New("新增问题失败 Err:" + e.Error())
  226. }
  227. return
  228. }
  229. // ReplyUserQuestion 回复问题
  230. func ReplyUserQuestion(replierId, questionId int, audios []*request.ReplyReqAudioList) (errMsg string, err error) {
  231. item, e := yb_community_question.GetItemById(questionId)
  232. if e != nil {
  233. errMsg = "问答信息有误"
  234. err = errors.New("获取提问信息失败 Err:" + e.Error())
  235. return
  236. }
  237. if item.ReplyStatus < 2 {
  238. errMsg = "回复状态有误"
  239. err = errors.New("回复状态有误")
  240. return
  241. }
  242. if item.ReplyStatus == 3 {
  243. errMsg = "请勿重复提交"
  244. err = errors.New("问题已回复,请勿重复提交")
  245. return
  246. }
  247. if item.ReplierUserID != replierId {
  248. errMsg = "回复人不匹配"
  249. err = errors.New(fmt.Sprintf("回复人与分配人不匹配, 当前回复人ID: %d, 分配的回复人ID: %d", replierId, item.ReplierUserID))
  250. return
  251. }
  252. // 问题
  253. updateCols := make([]string, 0)
  254. updateCols = append(updateCols, "reply_status", "reply_time", "modify_time", "msg_send_status", "is_read")
  255. nowTime := time.Now().Local()
  256. item.ReplyStatus = 3
  257. item.ReplyTime = nowTime
  258. item.ModifyTime = nowTime
  259. item.MsgSendStatus = 2
  260. item.IsRead = 0
  261. // 音频
  262. audioList := make([]*yb_community_question_audio.YbCommunityQuestionAudio, 0)
  263. for _, v := range audios {
  264. audioList = append(audioList, &yb_community_question_audio.YbCommunityQuestionAudio{
  265. CommunityQuestionID: questionId,
  266. AudioURL: v.AudioUrl,
  267. AudioPlaySeconds: v.AudioPlaySeconds,
  268. AudioSize: v.AudioSize,
  269. Sort: v.Sort,
  270. CreateTime: nowTime,
  271. })
  272. }
  273. if e := yb_community_question.UpdateQuestionAndAudioList(item, updateCols, audioList); e != nil {
  274. err = errors.New("UpdateQuestionAndAudioList Err:" + e.Error())
  275. return
  276. }
  277. // 推送回复消息给用户
  278. go wechat.SendQuestionReplyWxMsg(item.CommunityQuestionID, item.UserID, item.QuestionContent)
  279. return
  280. }
  281. // ReadQuestionReply 回复已读
  282. func ReadQuestionReply(questionIds string, userInfo user.UserInfo) (err error) {
  283. if questionIds == "" {
  284. return
  285. }
  286. questionIdArr := make([]int, 0)
  287. questionIdStrArr := strings.Split(questionIds, ",")
  288. for _, v := range questionIdStrArr {
  289. i, _ := strconv.Atoi(v)
  290. questionIdArr = append(questionIdArr, i)
  291. }
  292. if len(questionIdArr) == 0 {
  293. return
  294. }
  295. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  296. if e != nil {
  297. err = errors.New("获取用户身份失败 Err:" + e.Error())
  298. return
  299. }
  300. if isResearcher {
  301. // 设置分配给研究员的问答已读
  302. e = yb_community_question.UpdateReplierRead(int(userInfo.UserID), questionIdArr)
  303. if e != nil {
  304. err = errors.New("更新问答已读失败 Err:" + e.Error())
  305. return
  306. }
  307. // 设置研究员提问的问答已读
  308. e = yb_community_question.UpdateUserRead(int(userInfo.UserID), questionIdArr)
  309. } else {
  310. // 设置普通用户的问答已读
  311. e = yb_community_question.UpdateUserRead(int(userInfo.UserID), questionIdArr)
  312. }
  313. if e != nil {
  314. err = errors.New("更新问答已读失败 Err:" + e.Error())
  315. }
  316. return
  317. }
  318. // GetQuestionListTotal 获取问答列表数量统计
  319. func GetQuestionListTotal(userInfo user.UserInfo) (resp *response.CommunityQuestionListTotal, err error) {
  320. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  321. if e != nil {
  322. err = errors.New("获取用户身份失败 Err:" + e.Error())
  323. return
  324. }
  325. condition := " is_deleted = 0"
  326. var pars []interface{}
  327. if isResearcher {
  328. condition += " and (replier_user_id=? or user_id=?)"
  329. pars = append(pars, userInfo.UserID, userInfo.UserID)
  330. } else {
  331. condition += " and user_id=?"
  332. pars = append(pars, userInfo.UserID)
  333. }
  334. countList, e := yb_community_question.GetQuestionListCount(condition, pars)
  335. if e != nil {
  336. err = errors.New("获取回复人问题统计失败 Err:" + e.Error())
  337. return
  338. }
  339. resp = new(response.CommunityQuestionListTotal)
  340. var distribute, wait, replied, total int
  341. for _, v := range countList {
  342. total += v.Total
  343. if isResearcher {
  344. if v.UserId == userInfo.UserID {
  345. if v.ReplyStatus == 1 || v.ReplyStatus == 2 { //研究员提问的待分配的问题
  346. wait += v.Total
  347. }
  348. }
  349. if v.ReplierUserId == userInfo.UserID && v.ReplyStatus == 2{ //分配给研究员的未回答的问题
  350. distribute += v.Total
  351. }
  352. if v.ReplyStatus == 3 {
  353. replied += v.Total
  354. }
  355. }else{
  356. if v.ReplyStatus == 1 || v.ReplyStatus == 2 { //未分配和未回答的数量
  357. wait += v.Total
  358. }else if v.ReplyStatus == 3 { //已回答的数量
  359. replied += v.Total
  360. }
  361. }
  362. }
  363. if isResearcher {
  364. resp.Distribute = distribute
  365. }
  366. resp.Wait = wait
  367. resp.Replied = replied
  368. resp.Total = total
  369. return
  370. }
  371. // GetMyQuestionUnread 获取我的未读数
  372. func GetMyQuestionUnread(userInfo user.UserInfo) (total int, err error) {
  373. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  374. if e != nil {
  375. err = errors.New("获取用户身份失败 Err:" + e.Error())
  376. return
  377. }
  378. condition := " is_deleted = 0"
  379. var pars []interface{}
  380. if isResearcher {
  381. condition += " and ((replier_user_id=? and replier_is_read=0) or (user_id=? and is_read=0))"
  382. pars = append(pars, userInfo.UserID, userInfo.UserID)
  383. } else {
  384. condition += " and user_id=? and is_read=0"
  385. pars = append(pars, userInfo.UserID)
  386. }
  387. num, e := yb_community_question.GetUnreadNum(condition, pars)
  388. if e != nil {
  389. err = errors.New("获取我的未读数失败 Err:" + e.Error())
  390. return
  391. }
  392. total = int(num)
  393. return
  394. }
  395. // GetResearchGroupTree 获取研究方向分组
  396. func GetResearchGroupTree() (respList []*response.ResearchGroupItem, err error) {
  397. respList = make([]*response.ResearchGroupItem, 0)
  398. list, e := research_group.GetResearchGroupList()
  399. if e != nil {
  400. err = errors.New("获取研究方向分组失败, Err:" + e.Error())
  401. return
  402. }
  403. listLen := len(list)
  404. if listLen == 0 {
  405. return
  406. }
  407. // 分类
  408. firstList := make([]*response.ResearchGroupItem, 0)
  409. secondList := make([]*response.ResearchGroupItem, 0)
  410. for i := 0; i < listLen; i++ {
  411. item := new(response.ResearchGroupItem)
  412. item.ResearchGroupId = list[i].ResearchGroupID
  413. item.ResearchGroupName = list[i].ResearchGroupName
  414. item.ParentId = list[i].ParentID
  415. item.ChartPermissionId = list[i].ChartPermissionID
  416. item.Sort = list[i].Sort
  417. if list[i].ParentID == 0 {
  418. firstList = append(firstList, item)
  419. } else {
  420. secondList = append(secondList, item)
  421. }
  422. }
  423. if len(firstList) == 0 {
  424. return
  425. }
  426. // 匹配子分类
  427. for _, p := range firstList {
  428. children := make([]*response.ResearchGroupItem, 0)
  429. for _, child := range secondList {
  430. if p.ResearchGroupId == child.ParentId {
  431. children = append(children, child)
  432. }
  433. }
  434. p.Children = children
  435. }
  436. respList = firstList
  437. return
  438. }
  439. // AddAudioListenLog 添加用户点击音频日志
  440. func AddAudioListenLog(userInfo user.UserInfo, audioId int, sourceAgent int) (err error) {
  441. //1. 查询音频是否存在
  442. audio, err := yb_community_question_audio.GetByAudioId(audioId)
  443. if err != nil && err != utils.ErrNoRow {
  444. err = errors.New("查询音频信息失败 Err:" + err.Error())
  445. return
  446. }
  447. if err == utils.ErrNoRow {
  448. err = errors.New("音频不存在")
  449. return
  450. }
  451. if audio == nil {
  452. err = errors.New("音频不存在")
  453. return
  454. }
  455. //3. 添加点击日志
  456. item := &yb_community_audio_listen_log.YbCommunityAudioListenLog{
  457. CommunityQuestionAudioID: audio.CommunityQuestionAudioID,
  458. CommunityQuestionID: audio.CommunityQuestionID,
  459. UserID: int(userInfo.UserID),
  460. SourceAgent: sourceAgent,
  461. }
  462. if err = item.Create(); err != nil {
  463. err = errors.New("新增点击日志失败 Err:" + err.Error())
  464. return
  465. }
  466. return
  467. }