question.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package community
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/request"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/admin"
  9. "hongze/hongze_yb/models/tables/company"
  10. "hongze/hongze_yb/models/tables/company_product"
  11. "hongze/hongze_yb/models/tables/research_group"
  12. "hongze/hongze_yb/models/tables/research_group_relation"
  13. "hongze/hongze_yb/models/tables/research_variety_tag_relation"
  14. "hongze/hongze_yb/models/tables/variety_classify"
  15. "hongze/hongze_yb/models/tables/variety_tag"
  16. "hongze/hongze_yb/models/tables/wx_user"
  17. "hongze/hongze_yb/models/tables/yb_community_audio_listen_log"
  18. "hongze/hongze_yb/models/tables/yb_community_question"
  19. "hongze/hongze_yb/models/tables/yb_community_question_audio"
  20. "hongze/hongze_yb/models/tables/yb_community_question_process"
  21. "hongze/hongze_yb/services/alarm_msg"
  22. "hongze/hongze_yb/services/company_approval_message"
  23. "hongze/hongze_yb/services/user"
  24. "hongze/hongze_yb/services/wechat"
  25. "hongze/hongze_yb/utils"
  26. "strconv"
  27. "strings"
  28. "time"
  29. )
  30. // GetQuestionList 获取问答列表
  31. func GetQuestionList(pageIndex, pageSize, onlyMine, varietyTagId, replyStatus, groupId int, userInfo user.UserInfo) (resp []*response.CommunityQuestionItem, err error) {
  32. condition := " is_deleted = 0"
  33. var pars []interface{}
  34. // 用户身份
  35. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  36. if e != nil {
  37. err = errors.New("获取用户身份失败 Err:" + e.Error())
  38. return
  39. }
  40. if onlyMine == 1 {
  41. if isResearcher { //如果是研究员
  42. if replyStatus == 4 { //分配给研究员未回答的问题
  43. condition += " and replier_user_id=? and reply_status = 2"
  44. pars = append(pars, userInfo.UserID)
  45. } else if replyStatus == 2 { //研究员提问的问题未分配或者未回答的问题
  46. condition += " and user_id=? and reply_status >0 and reply_status <3"
  47. pars = append(pars, userInfo.UserID)
  48. } else if replyStatus == 3 { //分配给研究员的已回答和研究员提问的被回答的问题
  49. condition += " and (replier_user_id=? or user_id=?) and reply_status =3"
  50. pars = append(pars, userInfo.UserID, userInfo.UserID)
  51. } else if replyStatus == 0 { //分配给研究员或者研究员提问的所以问题
  52. condition += " and (replier_user_id=? or user_id=?)"
  53. pars = append(pars, userInfo.UserID, userInfo.UserID)
  54. } else if replyStatus == 5 {
  55. // 已终止状态
  56. condition += " and replier_user_id=? and reply_status = 4"
  57. pars = append(pars, userInfo.UserID)
  58. }
  59. } else {
  60. condition += " and user_id=?"
  61. pars = append(pars, userInfo.UserID)
  62. if replyStatus == 2 { // 普通用户未回答为待分配和未回答
  63. condition += " and reply_status >0 and reply_status <3"
  64. } else if replyStatus == 3 {
  65. condition += " and reply_status = 3"
  66. }
  67. }
  68. } else {
  69. if replyStatus == 3 {
  70. condition += " and reply_status = 3"
  71. }
  72. }
  73. if varietyTagId > 0 {
  74. condition += " and variety_tag_id =?"
  75. pars = append(pars, varietyTagId)
  76. }
  77. if groupId > 0 {
  78. condition += " and research_group_second_id =?"
  79. pars = append(pars, groupId)
  80. }
  81. // 问题列表
  82. questionList, e := yb_community_question.GetPageListByCondition(condition, pars, pageIndex, pageSize)
  83. if e != nil {
  84. err = errors.New("获取问题列表失败 Err:" + e.Error())
  85. return
  86. }
  87. listLen := len(questionList)
  88. if listLen == 0 {
  89. return
  90. }
  91. idArr := make([]int, 0)
  92. for i := 0; i < listLen; i++ {
  93. idArr = append(idArr, questionList[i].CommunityQuestionID)
  94. }
  95. // 音频列表
  96. audioList, e := yb_community_question_audio.GetListByQuestrionIds(idArr)
  97. if e != nil {
  98. err = errors.New("获取音频列表失败 Err:" + e.Error())
  99. return
  100. }
  101. // 用户权限
  102. //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
  103. //if e != nil {
  104. // err = errors.New("获取用户权限失败 Err:" + e.Error())
  105. // return
  106. //}
  107. userId := int(userInfo.UserID)
  108. resp = make([]*response.CommunityQuestionItem, 0)
  109. for _, v := range questionList {
  110. audios := make([]*response.CommunityQuestionAudioItem, 0)
  111. for _, a := range audioList {
  112. if a.CommunityQuestionID == v.CommunityQuestionID {
  113. audios = append(audios, &response.CommunityQuestionAudioItem{
  114. CommunityQuestionAudioID: a.CommunityQuestionAudioID,
  115. CommunityQuestionID: a.CommunityQuestionID,
  116. AudioURL: a.AudioURL,
  117. AudioPlaySeconds: a.AudioPlaySeconds,
  118. AudioSize: a.AudioSize,
  119. Sort: a.Sort,
  120. })
  121. }
  122. }
  123. replierRank := fmt.Sprintf("弘则%s研究员", v.ResearchGroupFirstName)
  124. avatar := v.ReplierAvatar
  125. if avatar == "" {
  126. avatar = utils.HZ_DEFAULT_AVATAR
  127. }
  128. item := &response.CommunityQuestionItem{
  129. CommunityQuestionID: v.CommunityQuestionID,
  130. UserId: v.UserID,
  131. QuestionContent: v.QuestionContent,
  132. ReplierRealName: v.ReplierRealName,
  133. ReplierRank: replierRank,
  134. ReplierAvatar: avatar,
  135. VarietyTagId: v.VarietyTagID,
  136. VarietyTagName: v.VarietyTagName,
  137. ResearchGroupSecondId: v.ResearchGroupSecondID,
  138. ResearchGroupSecondName: v.ResearchGroupSecondName,
  139. IsRead: v.IsRead,
  140. ReplierUserID: v.ReplierUserID,
  141. ReplierIsRead: v.ReplierIsRead,
  142. ReplyStatus: v.ReplyStatus,
  143. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  144. ReplyTime: v.ReplyTime.Format(utils.FormatDateTime),
  145. //AuthOk: authOk,
  146. //PermissionInfo: permissionInfo,
  147. AudioList: audios,
  148. StopReason: v.StopReason,
  149. }
  150. if item.IsRead == 0 && item.UserId == userId {
  151. item.IsTop = 1
  152. }
  153. resp = append(resp, item)
  154. }
  155. return
  156. }
  157. // GetQuestionDetail 获取问答详情
  158. func GetQuestionDetail(questionId int, userInfo user.UserInfo) (item *response.CommunityQuestionItem, errMsg string, err error) {
  159. detail, e := yb_community_question.GetItemById(questionId)
  160. errMsg = "获取失败"
  161. if e != nil {
  162. if e == utils.ErrNoRow {
  163. errMsg = "问题已被删除"
  164. }
  165. err = errors.New("获取问题详情失败 Err:" + e.Error())
  166. return
  167. }
  168. audioList, e := yb_community_question_audio.GetListByQuestionId(questionId)
  169. if e != nil {
  170. err = errors.New("获取问题音频失败 Err:" + e.Error())
  171. return
  172. }
  173. audios := make([]*response.CommunityQuestionAudioItem, 0)
  174. for _, a := range audioList {
  175. audios = append(audios, &response.CommunityQuestionAudioItem{
  176. CommunityQuestionAudioID: a.CommunityQuestionAudioID,
  177. CommunityQuestionID: a.CommunityQuestionID,
  178. AudioURL: a.AudioURL,
  179. AudioPlaySeconds: a.AudioPlaySeconds,
  180. AudioSize: a.AudioSize,
  181. Sort: a.Sort,
  182. })
  183. }
  184. replierRank := fmt.Sprintf("弘则%s研究员", detail.ResearchGroupFirstName)
  185. // 用户权限
  186. //authOk, permissionInfo, _, e := company.CheckBaseFiccPermission(userInfo.CompanyID, int(userInfo.UserID))
  187. //if e != nil {
  188. // err = errors.New("获取用户权限失败 Err:" + e.Error())
  189. // return
  190. //}
  191. avatar := detail.ReplierAvatar
  192. if avatar == "" {
  193. avatar = utils.HZ_DEFAULT_AVATAR
  194. }
  195. item = &response.CommunityQuestionItem{
  196. CommunityQuestionID: detail.CommunityQuestionID,
  197. UserId: detail.UserID,
  198. QuestionContent: detail.QuestionContent,
  199. ReplierRealName: detail.ReplierRealName,
  200. ReplierRank: replierRank,
  201. ReplierAvatar: detail.ReplierAvatar,
  202. ReplierUserID: detail.ReplierUserID,
  203. VarietyTagId: detail.VarietyTagID,
  204. VarietyTagName: detail.VarietyTagName,
  205. ResearchGroupSecondId: detail.ResearchGroupSecondID,
  206. ResearchGroupSecondName: detail.ResearchGroupSecondName,
  207. IsRead: detail.IsRead,
  208. ReplierIsRead: detail.ReplierIsRead,
  209. ReplyStatus: detail.ReplyStatus,
  210. CreateTime: detail.CreateTime.Format(utils.FormatDateTime),
  211. ReplyTime: detail.ReplyTime.Format(utils.FormatDateTime),
  212. //AuthOk: authOk,
  213. //PermissionInfo: permissionInfo,
  214. AudioList: audios,
  215. }
  216. errMsg = "获取成功"
  217. return
  218. }
  219. // CreateQuestion 新增问答
  220. func CreateQuestion(userId, varietyClassifyId, varietyTagId int, mobile, realName, content string) (err error) {
  221. // 获取分类
  222. classifyItem, e := variety_classify.GetVarietyClassifyById(varietyClassifyId)
  223. if e != nil {
  224. err = errors.New("获取问答标签分类失败 Err:" + e.Error())
  225. return
  226. }
  227. // 获取标签
  228. tagItem, e := variety_tag.GetVarietyTagById(varietyTagId)
  229. if e != nil {
  230. err = errors.New("获取问答标签失败 Err:" + e.Error())
  231. return
  232. }
  233. // 获取标签对应的研究方向分组-研究员信息
  234. researcher, e := GetFirstResearcherByVarietyTagId(tagItem.VarietyTagID)
  235. if e != nil {
  236. err = errors.New("获取标签对应研究员信息失败 Err:" + e.Error())
  237. return
  238. }
  239. nowTime := time.Now().Local()
  240. item := &yb_community_question.YbCommunityQuestion{
  241. UserID: userId,
  242. Mobile: mobile,
  243. RealName: realName,
  244. QuestionContent: content,
  245. ReplyStatus: 2,
  246. IsRead: 1,
  247. ReplierIsRead: 0,
  248. MsgSendStatus: 1,
  249. ReplierUserID: researcher.UserId,
  250. ReplierAdminID: researcher.AdminId,
  251. ReplierRealName: researcher.AdminName,
  252. ResearchGroupFirstID: classifyItem.VarietyClassifyID,
  253. ResearchGroupFirstName: classifyItem.ClassifyName,
  254. ResearchGroupSecondID: tagItem.VarietyTagID,
  255. ResearchGroupSecondName: tagItem.TagName,
  256. VarietyTagID: tagItem.VarietyTagID,
  257. VarietyTagName: tagItem.TagName,
  258. CreateTime: nowTime,
  259. }
  260. if e := item.Create(); e != nil {
  261. err = errors.New("新增问题失败 Err:" + e.Error())
  262. return
  263. }
  264. // 新增问答流程
  265. go func() {
  266. pro := new(yb_community_question_process.YbCommunityQuestionProcess)
  267. pro.CommunityQuestionID = item.CommunityQuestionID
  268. pro.ReplierUserID = researcher.UserId
  269. pro.ReplierAdminID = researcher.AdminId
  270. pro.ReplierAdminName = researcher.AdminName
  271. pro.VarietyClassifyID = classifyItem.VarietyClassifyID
  272. pro.VarietyTagID = tagItem.VarietyTagID
  273. pro.Remark = fmt.Sprintf("自动分配问题给研究员%s", researcher.AdminName)
  274. pro.ProcessType = yb_community_question_process.ProcessTypeDistribute
  275. pro.CreateTime = nowTime
  276. if e = pro.Create(); e != nil {
  277. return
  278. }
  279. }()
  280. // 推送待回复模板消息给研究员
  281. go func() {
  282. _ = wechat.SendQuestionToResearcher(item.CommunityQuestionID, researcher.OpenId, item.QuestionContent, "")
  283. }()
  284. // 暂不推送消息给管理员了(其实就是沛总=_=!)
  285. //go messageToAdmin(userId, item)
  286. return
  287. }
  288. // ReplyUserQuestion 回复问题
  289. func ReplyUserQuestion(replierId, questionId int, audios []*request.ReplyReqAudioList) (errMsg string, err error) {
  290. item, e := yb_community_question.GetItemById(questionId)
  291. if e != nil {
  292. errMsg = "问答信息有误"
  293. err = errors.New("获取提问信息失败 Err:" + e.Error())
  294. return
  295. }
  296. if item.ReplyStatus < 2 {
  297. errMsg = "回复状态有误"
  298. err = errors.New("回复状态有误")
  299. return
  300. }
  301. if item.ReplyStatus == 3 {
  302. errMsg = "请勿重复提交"
  303. err = errors.New("问题已回复,请勿重复提交")
  304. return
  305. }
  306. if item.ReplierUserID != replierId {
  307. errMsg = "回复人不匹配"
  308. err = errors.New(fmt.Sprintf("回复人与分配人不匹配, 当前回复人ID: %d, 分配的回复人ID: %d", replierId, item.ReplierUserID))
  309. return
  310. }
  311. // 问题
  312. updateCols := make([]string, 0)
  313. updateCols = append(updateCols, "reply_status", "reply_time", "modify_time", "msg_send_status", "is_read")
  314. nowTime := time.Now().Local()
  315. item.ReplyStatus = yb_community_question.ReplyStatusDone
  316. item.ReplyTime = nowTime
  317. item.ModifyTime = nowTime
  318. item.MsgSendStatus = 2
  319. item.IsRead = 0
  320. // 音频
  321. audioList := make([]*yb_community_question_audio.YbCommunityQuestionAudio, 0)
  322. for _, v := range audios {
  323. audioList = append(audioList, &yb_community_question_audio.YbCommunityQuestionAudio{
  324. CommunityQuestionID: questionId,
  325. AudioURL: v.AudioUrl,
  326. AudioPlaySeconds: v.AudioPlaySeconds,
  327. AudioSize: v.AudioSize,
  328. Sort: v.Sort,
  329. CreateTime: nowTime,
  330. })
  331. }
  332. if e := yb_community_question.UpdateQuestionAndAudioList(item, updateCols, audioList); e != nil {
  333. err = errors.New("UpdateQuestionAndAudioList Err:" + e.Error())
  334. return
  335. }
  336. // 推送回复消息给用户
  337. go wechat.SendQuestionReplyWxMsg(item.CommunityQuestionID, item.UserID, item.QuestionContent)
  338. return
  339. }
  340. // ReadQuestionReply 回复已读
  341. func ReadQuestionReply(questionIds string, userInfo user.UserInfo) (err error) {
  342. if questionIds == "" {
  343. return
  344. }
  345. questionIdArr := make([]int, 0)
  346. questionIdStrArr := strings.Split(questionIds, ",")
  347. for _, v := range questionIdStrArr {
  348. i, _ := strconv.Atoi(v)
  349. questionIdArr = append(questionIdArr, i)
  350. }
  351. if len(questionIdArr) == 0 {
  352. return
  353. }
  354. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  355. if e != nil {
  356. err = errors.New("获取用户身份失败 Err:" + e.Error())
  357. return
  358. }
  359. if isResearcher {
  360. // 设置分配给研究员的问答已读
  361. e = yb_community_question.UpdateReplierRead(int(userInfo.UserID), questionIdArr)
  362. if e != nil {
  363. err = errors.New("更新问答已读失败 Err:" + e.Error())
  364. return
  365. }
  366. // 设置研究员提问的问答已读
  367. e = yb_community_question.UpdateUserRead(int(userInfo.UserID), questionIdArr)
  368. } else {
  369. // 设置普通用户的问答已读
  370. e = yb_community_question.UpdateUserRead(int(userInfo.UserID), questionIdArr)
  371. }
  372. if e != nil {
  373. err = errors.New("更新问答已读失败 Err:" + e.Error())
  374. }
  375. return
  376. }
  377. // GetQuestionListTotal 获取问答列表数量统计
  378. func GetQuestionListTotal(userInfo user.UserInfo) (resp *response.CommunityQuestionListTotal, err error) {
  379. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  380. if e != nil {
  381. err = errors.New("获取用户身份失败 Err:" + e.Error())
  382. return
  383. }
  384. condition := " is_deleted = 0"
  385. var pars []interface{}
  386. if isResearcher {
  387. condition += " and (replier_user_id=? or user_id=?)"
  388. pars = append(pars, userInfo.UserID, userInfo.UserID)
  389. } else {
  390. condition += " and user_id=?"
  391. pars = append(pars, userInfo.UserID)
  392. }
  393. countList, e := yb_community_question.GetQuestionListCount(condition, pars)
  394. if e != nil {
  395. err = errors.New("获取回复人问题统计失败 Err:" + e.Error())
  396. return
  397. }
  398. resp = new(response.CommunityQuestionListTotal)
  399. var distribute, wait, replied, stop, total int
  400. for _, v := range countList {
  401. total += v.Total
  402. if isResearcher {
  403. if v.UserId == userInfo.UserID {
  404. if v.ReplyStatus == 1 || v.ReplyStatus == 2 { //研究员提问的待分配的问题
  405. wait += v.Total
  406. }
  407. }
  408. if v.ReplierUserId == userInfo.UserID && v.ReplyStatus == 2 { //分配给研究员的未回答的问题
  409. distribute += v.Total
  410. }
  411. if v.ReplyStatus == 3 {
  412. replied += v.Total
  413. }
  414. if v.ReplyStatus == yb_community_question.ReplyStatusStop {
  415. stop += v.Total
  416. }
  417. } else {
  418. if v.ReplyStatus == 1 || v.ReplyStatus == 2 { //未分配和未回答的数量
  419. wait += v.Total
  420. } else if v.ReplyStatus == 3 { //已回答的数量
  421. replied += v.Total
  422. }
  423. }
  424. }
  425. if isResearcher {
  426. resp.Distribute = distribute
  427. }
  428. resp.Wait = wait
  429. resp.Replied = replied
  430. resp.Total = total
  431. resp.Stop = stop
  432. return
  433. }
  434. // GetMyQuestionUnread 获取我的未读数
  435. func GetMyQuestionUnread(userInfo user.UserInfo) (total int, err error) {
  436. isResearcher, _, e := user.GetResearcherByUserInfo(userInfo)
  437. if e != nil {
  438. err = errors.New("获取用户身份失败 Err:" + e.Error())
  439. return
  440. }
  441. condition := " is_deleted = 0"
  442. var pars []interface{}
  443. if isResearcher {
  444. condition += " and ((replier_user_id=? and replier_is_read=0) or (user_id=? and is_read=0))"
  445. pars = append(pars, userInfo.UserID, userInfo.UserID)
  446. } else {
  447. condition += " and user_id=? and is_read=0"
  448. pars = append(pars, userInfo.UserID)
  449. }
  450. num, e := yb_community_question.GetUnreadNum(condition, pars)
  451. if e != nil {
  452. err = errors.New("获取我的未读数失败 Err:" + e.Error())
  453. return
  454. }
  455. total = int(num)
  456. return
  457. }
  458. // GetResearchGroupTree 获取研究方向分组
  459. func GetResearchGroupTree() (respList []*response.ResearchGroupItem, err error) {
  460. respList = make([]*response.ResearchGroupItem, 0)
  461. list, e := research_group.GetResearchGroupList()
  462. if e != nil {
  463. err = errors.New("获取研究方向分组失败, Err:" + e.Error())
  464. return
  465. }
  466. listLen := len(list)
  467. if listLen == 0 {
  468. return
  469. }
  470. // 分类
  471. firstList := make([]*response.ResearchGroupItem, 0)
  472. secondList := make([]*response.ResearchGroupItem, 0)
  473. for i := 0; i < listLen; i++ {
  474. item := new(response.ResearchGroupItem)
  475. item.ResearchGroupId = list[i].ResearchGroupID
  476. item.ResearchGroupName = list[i].ResearchGroupName
  477. item.ParentId = list[i].ParentID
  478. item.ChartPermissionId = list[i].ChartPermissionID
  479. item.Sort = list[i].Sort
  480. if list[i].ParentID == 0 {
  481. firstList = append(firstList, item)
  482. } else {
  483. secondList = append(secondList, item)
  484. }
  485. }
  486. if len(firstList) == 0 {
  487. return
  488. }
  489. // 匹配成员
  490. relationList, e := research_group_relation.GetResearchGroupRelationList()
  491. if e != nil {
  492. err = errors.New("获取研究方向关系失败, Err:" + e.Error())
  493. return
  494. }
  495. for _, v := range secondList {
  496. members := make([]*response.ResearchGroupMember, 0)
  497. for _, r := range relationList {
  498. if v.ResearchGroupId == r.ResearchGroupId {
  499. members = append(members, &response.ResearchGroupMember{
  500. AdminId: r.AdminId,
  501. AdminName: r.AdminName,
  502. })
  503. }
  504. }
  505. v.Members = members
  506. }
  507. // 匹配子分类
  508. for _, p := range firstList {
  509. children := make([]*response.ResearchGroupItem, 0)
  510. for _, child := range secondList {
  511. if p.ResearchGroupId == child.ParentId {
  512. children = append(children, child)
  513. }
  514. }
  515. p.Children = children
  516. }
  517. respList = firstList
  518. return
  519. }
  520. // AddAudioListenLog 添加用户点击音频日志
  521. func AddAudioListenLog(userInfo user.UserInfo, audioId int, sourceAgent int) (newId int, err error) {
  522. //1. 查询音频是否存在
  523. audio, err := yb_community_question_audio.GetByAudioId(audioId)
  524. if err != nil && err != utils.ErrNoRow {
  525. err = errors.New("查询音频信息失败 Err:" + err.Error())
  526. return
  527. }
  528. if err == utils.ErrNoRow {
  529. err = errors.New("音频不存在")
  530. return
  531. }
  532. if audio == nil {
  533. err = errors.New("音频不存在")
  534. return
  535. }
  536. companyInfo, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  537. if e != nil && e != utils.ErrNoRow {
  538. err = errors.New("获取客户信息失败")
  539. return
  540. }
  541. companyName := "潜在客户"
  542. companyStatus := "潜在"
  543. sellerId := 0
  544. if companyInfo != nil && companyInfo.CompanyID > 0 {
  545. companyName = companyInfo.CompanyName
  546. companyStatus = companyInfo.Status
  547. sellerId = companyInfo.SellerID
  548. }
  549. //3. 添加点击日志
  550. item := &yb_community_audio_listen_log.YbCommunityAudioListenLog{
  551. CommunityQuestionAudioID: audio.CommunityQuestionAudioID,
  552. CommunityQuestionID: audio.CommunityQuestionID,
  553. UserID: int(userInfo.UserID),
  554. RealName: userInfo.RealName,
  555. CompanyID: int(userInfo.CompanyID),
  556. CompanyName: companyName,
  557. CompanyStatus: companyStatus,
  558. SellerID: sellerId,
  559. SourceAgent: sourceAgent,
  560. }
  561. if err = item.Create(); err != nil {
  562. err = errors.New("新增点击日志失败 Err:" + err.Error())
  563. return
  564. }
  565. newId = item.Id
  566. return
  567. }
  568. // messageToAdmin 添加站内消息
  569. func messageToAdmin(userId int, ybCommunityQuestion *yb_community_question.YbCommunityQuestion) {
  570. var err error
  571. defer func() {
  572. if err != nil {
  573. go alarm_msg.SendAlarmMsg("新增社区问答完成后,发送消息给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  574. }
  575. }()
  576. //因为产品说只要给沛总发送信息,那么没办法咯,只去获取沛总的信息 2022-07-19 11:29:16
  577. vWangInfo, err := admin.GetVWangInfo()
  578. if err != nil {
  579. return
  580. }
  581. //站内消息
  582. go systemMessageToAdmin(*vWangInfo, userId, ybCommunityQuestion)
  583. //微信模板消息
  584. go wxMessageToAdmin(*vWangInfo, ybCommunityQuestion)
  585. return
  586. }
  587. // systemMessageToAdmin 系统消息消息通知管理员
  588. func systemMessageToAdmin(adminInfo admin.Admin, userId int, ybCommunityQuestion *yb_community_question.YbCommunityQuestion) {
  589. var err error
  590. defer func() {
  591. if err != nil {
  592. go alarm_msg.SendAlarmMsg("新增社区问答完成后,站内评论信息发送给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  593. }
  594. }()
  595. // 接收人的admin_id
  596. receiveUserId := int(adminInfo.AdminID)
  597. //获取评论人信息
  598. wxUser, err := wx_user.GetByUserId(userId)
  599. if err != nil {
  600. return
  601. }
  602. var msgType, sourceType, approvalStatus int8
  603. msgType = company_approval_message.CompanyApprovalMessageMessageTypeByApply
  604. sourceType = company_approval_message.CompanyApprovalMessageSourceTypeByQuestion
  605. approvalStatus = company_approval_message.CompanyApprovalMessageApprovalStatusByPending
  606. companyInfo, err := company.GetByCompanyId(wxUser.CompanyID)
  607. if err != nil {
  608. return
  609. }
  610. productId := 1
  611. companyProductInfo, err := company_product.GetByCompany2ProductId(wxUser.CompanyID, int64(productId))
  612. if err != nil {
  613. return
  614. }
  615. companyName := companyInfo.CompanyName
  616. remark := `您有新的问答待分配`
  617. content := `您有新的问答待分配`
  618. messageInfo := company_approval_message.MessageInfo{
  619. CompanyName: companyInfo.CompanyName,
  620. ProductId: productId,
  621. CompanyProductStatus: companyProductInfo.Status,
  622. Title: ybCommunityQuestion.QuestionContent,
  623. Content: ybCommunityQuestion.QuestionContent,
  624. UserId: wxUser.UserID,
  625. UserName: ybCommunityQuestion.RealName,
  626. CreateTime: ybCommunityQuestion.CreateTime,
  627. }
  628. //客户添加消息
  629. err = company_approval_message.AddCompanyApprovalMessage(utils.AdminId, receiveUserId, int(wxUser.CompanyID), ybCommunityQuestion.CommunityQuestionID, msgType, sourceType, approvalStatus, companyName, remark, content, messageInfo)
  630. return
  631. }
  632. // wxMessageToAdmin 微信模板消息通知管理员
  633. func wxMessageToAdmin(adminInfo admin.Admin, ybCommunityQuestion *yb_community_question.YbCommunityQuestion) {
  634. var err error
  635. defer func() {
  636. if err != nil {
  637. go alarm_msg.SendAlarmMsg("新增社区问答完成后,微信模板消息发送给管理员失败"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  638. }
  639. }()
  640. if global.CONFIG.Serve.RunMode == "debug" {
  641. adminInfo.Mobile = `18221983795`
  642. }
  643. err = wechat.SendQuestionToAdmin(ybCommunityQuestion.CommunityQuestionID, int(adminInfo.AdminID), adminInfo.OpenId, ybCommunityQuestion.QuestionContent)
  644. return
  645. }
  646. // GetFirstResearcherByVarietyTagId 通过品种标签ID获取首位研究员admin信息及wx_user信息
  647. func GetFirstResearcherByVarietyTagId(tagId int) (researcher *admin.ResearcherAdminAndUser, err error) {
  648. researcher = new(admin.ResearcherAdminAndUser)
  649. // 获取标签研究员关系组
  650. relationList, e := research_variety_tag_relation.GetResearchVarietyTagRelationList()
  651. if e != nil {
  652. err = errors.New("获取研究员分组失败, Err: " + e.Error())
  653. return
  654. }
  655. if len(relationList) == 0 {
  656. err = errors.New("获取研究员分组有误")
  657. return
  658. }
  659. // 从当前组取出有效的研究员信息
  660. for i := range relationList {
  661. if relationList[i].VarietyTagId == tagId {
  662. item, e := admin.GetResearcherAdminAndWxUserByAdminId(relationList[i].AdminId)
  663. if e != nil && e != utils.ErrNoRow {
  664. err = errors.New("获取研究员成员信息失败, Err: " + e.Error())
  665. return
  666. }
  667. if item != nil && item.UserId > 0 && item.AdminId > 0 && item.OpenId != "" {
  668. researcher = item
  669. break
  670. }
  671. }
  672. }
  673. // 未在当前组找到有效的研究员信息, 去其他组找
  674. if researcher.UserId == 0 || researcher.AdminId == 0 || researcher.OpenId == "" {
  675. for i := range relationList {
  676. if relationList[i].VarietyTagId != tagId {
  677. item, e := admin.GetResearcherAdminAndWxUserByAdminId(relationList[i].AdminId)
  678. if e != nil && e != utils.ErrNoRow {
  679. err = errors.New("获取其他组研究员成员信息失败, Err: " + e.Error())
  680. return
  681. }
  682. if item != nil && item.UserId > 0 && item.AdminId > 0 && item.OpenId != "" {
  683. researcher = item
  684. break
  685. }
  686. }
  687. }
  688. }
  689. // 无有效研究员
  690. if researcher.UserId == 0 || researcher.AdminId == 0 || researcher.OpenId == "" {
  691. err = errors.New("无有效研究员可分配")
  692. }
  693. return
  694. }
  695. // TransferQuestion 转移问答
  696. func TransferQuestion(questionId, userId, userAdminId, varietyClassifyId, varietyTagId, adminId int, userAdminName string) (errMsg string, err error) {
  697. errMsg = "操作失败"
  698. item, e := yb_community_question.GetItemById(questionId)
  699. if e != nil {
  700. err = errors.New("获取提问信息失败 Err:" + e.Error())
  701. return
  702. }
  703. if item.ReplyStatus != yb_community_question.ReplyStatusWait {
  704. errMsg = "当前状态不可转移"
  705. err = errors.New("回复状态有误")
  706. return
  707. }
  708. if item.ReplierUserID != userId {
  709. errMsg = "无权操作"
  710. err = errors.New(fmt.Sprintf("回复人与分配人不匹配, 当前回复人ID: %d, 分配的回复人ID: %d", userId, item.ReplierUserID))
  711. return
  712. }
  713. // 获取分类
  714. classifyItem, e := variety_classify.GetVarietyClassifyById(varietyClassifyId)
  715. if e != nil {
  716. err = errors.New("获取问答标签分类失败 Err:" + e.Error())
  717. return
  718. }
  719. // 获取标签
  720. tagItem, e := variety_tag.GetVarietyTagById(varietyTagId)
  721. if e != nil {
  722. err = errors.New("获取问答标签失败 Err:" + e.Error())
  723. return
  724. }
  725. // 获取研究员信息失败
  726. researcher, e := admin.GetResearcherAdminAndWxUserByAdminId(adminId)
  727. if e != nil {
  728. errMsg = "该研究员未关注公众号或无法正确接收模板消息"
  729. err = errors.New("获取研究员信息失败 Err:" + e.Error())
  730. return
  731. }
  732. // 更新问答
  733. nowTime := time.Now().Local()
  734. updateCols := []string{
  735. "ReplierIsRead", "ReplierUserID", "ReplierAdminID", "ReplierRealName", "ResearchGroupFirstID", "ResearchGroupFirstName",
  736. "ResearchGroupSecondID", "ResearchGroupSecondName", "VarietyTagID", "VarietyTagName", "ModifyTime",
  737. }
  738. item.ReplierIsRead = 0
  739. item.ReplierUserID = researcher.UserId
  740. item.ReplierAdminID = researcher.AdminId
  741. item.ReplierRealName = researcher.AdminName
  742. item.ResearchGroupFirstID = classifyItem.VarietyClassifyID
  743. item.ResearchGroupFirstName = classifyItem.ClassifyName
  744. item.ResearchGroupSecondID = tagItem.VarietyTagID
  745. item.ResearchGroupSecondName = tagItem.TagName
  746. item.VarietyTagID = tagItem.VarietyTagID
  747. item.VarietyTagName = tagItem.TagName
  748. item.ModifyTime = nowTime
  749. if e = item.Update(updateCols); e != nil {
  750. err = errors.New("更新问答失败 Err:" + e.Error())
  751. return
  752. }
  753. // 新增问答流程
  754. go func() {
  755. pro := new(yb_community_question_process.YbCommunityQuestionProcess)
  756. pro.CommunityQuestionID = item.CommunityQuestionID
  757. pro.TransferUserID = userId
  758. pro.TransferAdminID = userAdminId
  759. pro.TransferAdminName = userAdminName
  760. pro.ReplierUserID = researcher.UserId
  761. pro.ReplierAdminID = researcher.AdminId
  762. pro.ReplierAdminName = researcher.AdminName
  763. pro.VarietyClassifyID = classifyItem.VarietyClassifyID
  764. pro.VarietyTagID = tagItem.VarietyTagID
  765. pro.Remark = fmt.Sprintf("转至%s", researcher.AdminName)
  766. pro.ProcessType = yb_community_question_process.ProcessTypeTransfer
  767. pro.CreateTime = nowTime
  768. if e = pro.Create(); e != nil {
  769. return
  770. }
  771. }()
  772. // 推送转移模板消息给研究员
  773. go func() {
  774. remark := fmt.Sprintf("【%s】移交了新的问答给您, 请点击详情尽快处理", userAdminName)
  775. _ = wechat.SendQuestionToResearcher(item.CommunityQuestionID, researcher.OpenId, item.QuestionContent, remark)
  776. }()
  777. return
  778. }
  779. // StopQuestion 终止问答
  780. func StopQuestion(questionId, userId int, reason string) (errMsg string, err error) {
  781. errMsg = "操作失败"
  782. item, e := yb_community_question.GetItemById(questionId)
  783. if e != nil {
  784. err = errors.New("获取提问信息失败 Err:" + e.Error())
  785. return
  786. }
  787. if item.ReplyStatus != yb_community_question.ReplyStatusWait {
  788. errMsg = "当前状态不可终止"
  789. err = errors.New("回复状态有误")
  790. return
  791. }
  792. if item.ReplierUserID != userId {
  793. errMsg = "无权操作"
  794. err = errors.New(fmt.Sprintf("回复人与分配人不匹配, 当前回复人ID: %d, 分配的回复人ID: %d", userId, item.ReplierUserID))
  795. return
  796. }
  797. // 更新问答
  798. nowTime := time.Now().Local()
  799. updateCols := []string{
  800. "ReplyStatus", "StopReason", "ModifyTime",
  801. }
  802. item.ReplyStatus = yb_community_question.ReplyStatusStop
  803. item.StopReason = reason
  804. item.ModifyTime = nowTime
  805. if e = item.Update(updateCols); e != nil {
  806. err = errors.New("更新问答失败 Err:" + e.Error())
  807. return
  808. }
  809. return
  810. }