community_question.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. package yb
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/models/yb/response"
  10. "hongze/hz_crm_api/services"
  11. "hongze/hz_crm_api/services/alarm_msg"
  12. "hongze/hz_crm_api/utils"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // GetQuestionList 问题列表
  18. func GetQuestionList(condition string, pars []interface{}, orderStr string, startSize, pageSize int) (total int, resp *response.CommunityQuestionListResp, err error) {
  19. resp = new(response.CommunityQuestionListResp)
  20. total, list, e := yb.GetCommunityQuestionList(condition, pars, orderStr, startSize, pageSize)
  21. if e != nil {
  22. err = errors.New("获取问题列表失败 Err:" + e.Error())
  23. return
  24. }
  25. if len(list) == 0 {
  26. return
  27. }
  28. // 判断是否存在已禁用的回复人, 需要重新分配回复人
  29. adminIdArr := make([]string, 0)
  30. questionLen := len(list)
  31. userIdArr := make([]string, 0)
  32. for i := 0; i < questionLen; i++ {
  33. if list[i].ReplierAdminId > 0 {
  34. adminIdArr = append(adminIdArr, strconv.Itoa(list[i].ReplierAdminId))
  35. }
  36. userIdArr = append(userIdArr, strconv.Itoa(list[i].UserId))
  37. }
  38. adminIds := strings.Join(adminIdArr, ",")
  39. adminList, e := system.GetSysAdminByIds(adminIds)
  40. adminLen := len(adminList)
  41. adminEnableMap := make(map[int]int, 0)
  42. if adminLen > 0 {
  43. for i := 0; i < adminLen; i++ {
  44. adminEnableMap[adminList[i].AdminId] = adminList[i].Enabled
  45. }
  46. }
  47. // 是否有提问人详情(可能存在提问人被删除的情况,无详情)
  48. userDetailMap := make(map[int]bool, 0)
  49. if len(userIdArr) > 0 {
  50. userIdStr := strings.Join(userIdArr, ",")
  51. userList, e := company.GetFiccCompanyUserByUserIds(userIdStr)
  52. if e != nil {
  53. err = errors.New("获取提问人列表信息失败, Err:" + e.Error())
  54. return
  55. }
  56. for i := range userList {
  57. userDetailMap[int(userList[i].UserId)] = true
  58. }
  59. }
  60. respList := make([]*response.CommunityQuestionItem, 0)
  61. for _, v := range list {
  62. // 根据回复人被禁用/待回答/已推送消息判断是否需要重新分配
  63. needReset := false
  64. adminEnable := 1
  65. if v.ReplierAdminId > 0 {
  66. adminEnable = adminEnableMap[v.ReplierAdminId]
  67. }
  68. if adminEnable == 0 && v.ReplyStatus == 2 && v.MsgSendStatus >= 1 {
  69. needReset = true
  70. }
  71. item := &response.CommunityQuestionItem{
  72. CommunityQuestionId: v.CommunityQuestionId,
  73. UserId: v.UserId,
  74. Mobile: v.Mobile,
  75. RealName: v.RealName,
  76. QuestionContent: v.QuestionContent,
  77. ReplierUserId: v.ReplierUserId,
  78. ReplierAdminId: v.ReplierAdminId,
  79. ReplierRealName: v.ReplierRealName,
  80. ReplierAvatar: v.ReplierAvatar,
  81. ResearchGroupFirstId: v.ResearchGroupFirstId,
  82. ResearchGroupSecondId: v.ResearchGroupSecondId,
  83. ResearchGroupFirstName: v.ResearchGroupFirstName,
  84. ResearchGroupSecondName: v.ResearchGroupSecondName,
  85. ReplyStatus: v.ReplyStatus,
  86. MsgSendStatus: v.MsgSendStatus,
  87. NeedRedistribute: needReset,
  88. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  89. ReplyTime: v.ReplyTime.Format(utils.FormatDateTime),
  90. ClickNum: v.ClickNum,
  91. UserQaCount: v.UserQaCount,
  92. UserHasDetail: userDetailMap[v.UserId],
  93. StopReason: v.StopReason,
  94. }
  95. respList = append(respList, item)
  96. }
  97. // 数量统计
  98. countList, e := yb.GetCommunityQuestionCount()
  99. if e != nil {
  100. err = errors.New("获取问题数量统计失败 Err:" + e.Error())
  101. return
  102. }
  103. respCount := new(response.CommunityQuestionCount)
  104. for _, v := range countList {
  105. if v.ReplyStatus == 1 {
  106. respCount.Free = v.Total
  107. continue
  108. }
  109. if v.ReplyStatus == 2 {
  110. respCount.Wait = v.Total
  111. continue
  112. }
  113. if v.ReplyStatus == 3 {
  114. respCount.Replied = v.Total
  115. continue
  116. }
  117. }
  118. respCount.Total = respCount.Free + respCount.Wait + respCount.Replied
  119. resp.List = respList
  120. resp.Count = respCount
  121. return
  122. }
  123. func GetQuestionClickLogList(questionId, startSize, pageSize int) (pageTotal int64, resp *response.QuestionClickLogListResp, err error) {
  124. questionInfo, e := yb.GetQuestionById(questionId)
  125. if e != nil {
  126. err = errors.New("提问信息有误 Err:" + e.Error())
  127. return
  128. }
  129. resp = new(response.QuestionClickLogListResp)
  130. list, e := yb.GetAudioLogListByQuestionId(questionId, startSize, pageSize)
  131. if e != nil {
  132. err = errors.New("获取问题列表失败 Err:" + e.Error())
  133. return
  134. }
  135. pageTotal, err = yb.GetAudioLogListTotalByQuestionId(questionId)
  136. if err != nil {
  137. err = errors.New("查询该提问列表总点击量失败 Err:" + err.Error())
  138. return
  139. }
  140. count, err := yb.GetAudioLogTotalByQuestionId(questionId)
  141. if err != nil {
  142. err = errors.New("查询该提问总点击量失败 Err:" + err.Error())
  143. return
  144. }
  145. resp.Count = int(count)
  146. resp.QuestionContent = questionInfo.QuestionContent
  147. if len(list) == 0 {
  148. return
  149. }
  150. userIds := ""
  151. for _, v := range list {
  152. userIds += ", " + (strconv.Itoa(v.UserId))
  153. }
  154. companyUsersMap := make(map[int]*company.CompanyUser)
  155. if userIds != "" {
  156. userIds = strings.Trim(userIds, ",")
  157. //查询用户相关的客户信息
  158. companyUserList, tErr := company.GetFiccCompanyUserByUserIds(userIds)
  159. if tErr != nil {
  160. err = errors.New("获取客户信息失败 Err:" + tErr.Error())
  161. return
  162. }
  163. for _, v := range companyUserList {
  164. companyUsersMap[int(v.UserId)] = v
  165. }
  166. }
  167. respList := make([]*response.QuestionClickLogListItem, 0)
  168. for _, v := range list {
  169. tmp := new(response.QuestionClickLogListItem)
  170. tmp.UserId = v.UserId
  171. tmp.ClickNum = v.ClickNum
  172. tmp.LastCreateTime = v.LastCreateTime
  173. tmp.SourceAgent = v.NewSourceAgent
  174. if u, ok := companyUsersMap[v.UserId]; ok {
  175. tmp.RealName = u.RealName
  176. tmp.CompanyName = u.CompanyName
  177. tmp.CompanyStatus = u.Status
  178. tmp.CompanyId = u.CompanyId
  179. }
  180. respList = append(respList, tmp)
  181. }
  182. resp.List = respList
  183. return
  184. }
  185. // GetQuestionCompanyUser 获取提问者详情
  186. func GetQuestionCompanyUser(questionId int) (ret *response.QuestionCompanyUser, err error) {
  187. questionInfo, e := yb.GetQuestionById(questionId)
  188. if e != nil {
  189. err = errors.New("提问信息有误 Err:" + e.Error())
  190. return
  191. }
  192. companyUserList, err := company.GetFiccCompanyUserByUserIds(strconv.Itoa(questionInfo.UserId))
  193. if err != nil {
  194. err = errors.New("获取客户信息失败 Err:" + err.Error())
  195. return
  196. }
  197. if len(companyUserList) > 0 {
  198. item := companyUserList[0]
  199. ret = &response.QuestionCompanyUser{
  200. UserId: int(item.UserId),
  201. CompanyStatus: item.Status,
  202. CompanyName: item.CompanyName,
  203. RealName: item.RealName,
  204. CompanyId: item.CompanyId,
  205. QuestionContent: questionInfo.QuestionContent,
  206. CommunityQuestionId: questionInfo.CommunityQuestionId,
  207. SellerName: item.SellerName,
  208. }
  209. } else {
  210. err = errors.New("用户不存在")
  211. return
  212. }
  213. return
  214. }
  215. // DistributeQuestion 分配回复人
  216. func DistributeQuestion(questionId, adminId, researchGroupFirstId, researchGroupSecondId, distributeId int) (errMsg string, err error) {
  217. errMsg = "操作成功"
  218. // 校验提问信息
  219. item, e := yb.GetQuestionById(questionId)
  220. if e != nil {
  221. errMsg = "分配失败, 提问信息有误"
  222. err = errors.New("提问信息有误 Err:" + e.Error())
  223. return
  224. }
  225. contentRune := []rune(item.QuestionContent)
  226. if len(contentRune) > 28 {
  227. errMsg = "问题字数不可超过28个字符"
  228. err = errors.New("问题字数不可超过28个字符")
  229. return
  230. }
  231. if item.ReplyStatus == 3 {
  232. errMsg = "分配失败, 回复状态有误"
  233. err = errors.New(fmt.Sprintf("分配失败, 回复状态有误, ReplyStatus:%d", item.ReplyStatus))
  234. return
  235. }
  236. // 获取研究员信息
  237. adminInfo, e := system.GetSysAdminById(adminId)
  238. if e != nil {
  239. errMsg = "分配失败, 研究员信息有误"
  240. err = errors.New("研究员信息有误 Err:" + e.Error())
  241. return
  242. }
  243. if adminInfo.Mobile == "" {
  244. errMsg = "分配失败, 研究员手机号有误"
  245. err = errors.New("研究员手机号为空")
  246. return
  247. }
  248. if adminInfo.OpenId == "" {
  249. errMsg = "该研究员未关注公众号,无法收到消息通知"
  250. err = errors.New("该研究员未关注公众号,无法收到消息通知")
  251. return
  252. }
  253. // 获取研究员关联的联系人信息
  254. companyId := 16
  255. userInfo, e := models.GetWxUserByCompanyIdAndMobile(companyId, adminInfo.Mobile)
  256. if e != nil {
  257. if e.Error() == utils.ErrNoRow() {
  258. errMsg = "分配失败, 未找到研究员手机号对应的联系人信息"
  259. err = errors.New("未获取到相关联系人信息")
  260. return
  261. }
  262. errMsg = "分配失败"
  263. err = errors.New("获取手机号所属联系人失败, Err:" + e.Error())
  264. return
  265. }
  266. // 回复人openid, 此处可能会出现取不到openid的情况, 可直接忽略掉
  267. replierUserId := int(userInfo.UserId)
  268. replierOpenid := ""
  269. // 研究员研究方向分组-研报10.0替换成标签库
  270. //firstGroup, e := system.GetResearchGroupById(researchGroupFirstId)
  271. firstGroup, e := models.GetVarietyClassifyById(researchGroupFirstId)
  272. if e != nil {
  273. errMsg = "分配失败, 一级分组信息有误"
  274. err = errors.New("获取一级分组信息失败, Err:" + e.Error())
  275. return
  276. }
  277. //secondGroup, e := system.GetResearchGroupById(researchGroupSecondId)
  278. secondGroup, e := models.GetVarietyTagById(researchGroupSecondId)
  279. if e != nil {
  280. errMsg = "分配失败, 二级分组信息有误"
  281. err = errors.New("获取二级分组信息失败, Err:" + e.Error())
  282. return
  283. }
  284. // 更新提问信息
  285. updateCols := make([]string, 0)
  286. updateCols = append(updateCols, "ReplierUserId", "ReplierOpenid", "ReplierAdminId", "ReplierRealName", "ReplierAvatar",
  287. "ResearchGroupFirstId", "ResearchGroupSecondId", "ResearchGroupFirstName", "ResearchGroupSecondName", "VarietyTagId", "VarietyTagName",
  288. "DistributeAdminId", "DistributeTime", "ReplyStatus", "ReplierIsRead", "MsgSendStatus", "ModifyTime")
  289. item.ReplierUserId = replierUserId
  290. item.ReplierOpenid = replierOpenid
  291. item.ReplierAdminId = adminId
  292. item.ReplierRealName = adminInfo.RealName
  293. item.ReplierAvatar = adminInfo.AdminAvatar
  294. item.ResearchGroupFirstId = researchGroupFirstId
  295. item.ResearchGroupSecondId = researchGroupSecondId
  296. item.ResearchGroupFirstName = firstGroup.ClassifyName
  297. item.ResearchGroupSecondName = secondGroup.TagName
  298. item.VarietyTagId = secondGroup.VarietyTagId
  299. item.VarietyTagName = secondGroup.TagName
  300. item.DistributeAdminId = distributeId
  301. item.DistributeTime = time.Now().Local()
  302. item.ReplyStatus = 2
  303. item.ReplierIsRead = 0
  304. item.MsgSendStatus = 0
  305. item.ModifyTime = time.Now().Local()
  306. if e := item.Update(updateCols); e != nil {
  307. errMsg = "分配失败,更新提问信息失败"
  308. err = errors.New("更新提问信息失败, Err:" + e.Error())
  309. }
  310. return
  311. }
  312. // SoftDeleteQuestion 删除问题
  313. func SoftDeleteQuestion(questionId int) (err error) {
  314. if _, e := yb.GetQuestionById(questionId); e != nil {
  315. err = errors.New("提问信息有误 Err:" + e.Error())
  316. return
  317. }
  318. if e := yb.DeleteQuestion(questionId); e != nil {
  319. err = errors.New("删除提问失败 Err:" + e.Error())
  320. }
  321. // 删除问答后的逻辑处理
  322. go afterDeleteQuestion(questionId)
  323. return
  324. }
  325. // SendMsgToReplier 推送消息给回复人
  326. func SendMsgToReplier(questionId int) (errMsg string, err error) {
  327. errMsg = "推送成功"
  328. item, e := yb.GetQuestionById(questionId)
  329. if e != nil {
  330. errMsg = "问答信息有误"
  331. err = errors.New("提问信息有误, Err:" + e.Error())
  332. return
  333. }
  334. if item.MsgSendStatus >= 1 {
  335. return
  336. }
  337. if item.ReplierUserId == 0 {
  338. errMsg = "请先分配回复人"
  339. err = errors.New("未分配回复人, 不可推送")
  340. return
  341. }
  342. // 回复人openid为空时查询当前是否已有openid
  343. updateCols := make([]string, 0)
  344. // 找到研究员的手机号,并查到管理员绑定的账号,找到对应的openid
  345. adminInfo, e := system.GetSysAdminById(item.ReplierAdminId)
  346. if e != nil {
  347. errMsg = "发送失败, 研究员账号查询失败"
  348. err = errors.New("发送失败, 研究员账号查询失败, Err:" + e.Error())
  349. return
  350. }
  351. if adminInfo.OpenId == "" {
  352. errMsg = "该研究员未关注公众号,无法发送消息通知"
  353. err = errors.New("回复人openid为空, 不可推送")
  354. return
  355. }
  356. // 推送消息
  357. if e := services.SendYbQuestionDistributeWxMsg(item.CommunityQuestionId, adminInfo.AdminId, adminInfo.OpenId, item.QuestionContent, ""); e != nil {
  358. err = errors.New("推送模板消息失败, Err:" + e.Error())
  359. return
  360. }
  361. // 更新问答信息
  362. updateCols = append(updateCols, "ModifyTime", "MsgSendStatus")
  363. item.ModifyTime = time.Now().Local()
  364. item.MsgSendStatus = 1
  365. if e := item.Update(updateCols); e != nil {
  366. err = errors.New("更新问答信息失败, Err:" + e.Error())
  367. }
  368. return
  369. }
  370. // EditQuestion 编辑问题
  371. func EditQuestion(questionId int, content string) (errMsg string, err error) {
  372. item, e := yb.GetQuestionById(questionId)
  373. if e != nil {
  374. errMsg = "提问信息有误"
  375. err = errors.New("提问信息有误 Err:" + e.Error())
  376. return
  377. }
  378. if item.MsgSendStatus >= 1 {
  379. errMsg = "问题状态有误,不可编辑"
  380. err = errors.New(fmt.Sprintf("问题状态不可编辑, MsgSendStatus:%d", item.MsgSendStatus))
  381. return
  382. }
  383. updateCols := make([]string, 0)
  384. updateCols = append(updateCols, "QuestionContent", "ModifyTime")
  385. item.QuestionContent = content
  386. item.ModifyTime = time.Now().Local()
  387. if e := item.Update(updateCols); e != nil {
  388. errMsg = "更新提问信息失败"
  389. err = errors.New("更新提问信息失败, Err:" + e.Error())
  390. }
  391. return
  392. }
  393. // CheckCommunityQuestionPermission 查询当前操作用户是否运营管理员权限
  394. func CheckCommunityQuestionPermission(adminId int) (errMsg string, err error) {
  395. roleInfo, e := system.GetSysRoleByRoleTypeCode("yb_question_admin")
  396. if e != nil {
  397. errMsg = "角色信息有误"
  398. if e.Error() == utils.ErrNoRow() {
  399. err = errors.New("角色不存在, Err: " + e.Error())
  400. return
  401. }
  402. err = errors.New("角色信息有误, Err: " + e.Error())
  403. return
  404. }
  405. _, e = system.GetRoleIdsByAdminIdRoleId(adminId, roleInfo.RoleId)
  406. if e != nil {
  407. errMsg = "无权操作"
  408. err = errors.New("无权操作, Err: " + e.Error())
  409. return
  410. }
  411. return
  412. }
  413. // afterDeleteQuestion 删除问答后的逻辑处理
  414. func afterDeleteQuestion(questionId int) {
  415. var err error
  416. defer func() {
  417. if err != nil {
  418. go alarm_msg.SendAlarmMsg("问答信息删除后,标记站内消息失败"+time.Now().Format(utils.FormatDateTime)+";Err:"+err.Error(), 3)
  419. }
  420. }()
  421. err = company.CancelCompanyApprovalMessage(questionId, services.CompanyApprovalMessageSourceTypeByQuestion)
  422. return
  423. }
  424. // TransferQuestion 转移问题并通知
  425. func TransferQuestion(questionId, adminId, researchGroupFirstId, researchGroupSecondId, distributeId int, distributeName string) (errMsg string, err error) {
  426. errMsg = "操作失败"
  427. // 校验提问信息
  428. item, e := yb.GetQuestionById(questionId)
  429. if e != nil {
  430. errMsg = "转移失败, 提问信息有误"
  431. err = errors.New("提问信息有误 Err:" + e.Error())
  432. return
  433. }
  434. if item.ReplyStatus != yb.ReplyStatusWait {
  435. errMsg = "当前状态不可转移"
  436. err = errors.New("回复状态有误")
  437. return
  438. }
  439. // 获取分类
  440. classifyItem, e := models.GetVarietyClassifyById(researchGroupFirstId)
  441. if e != nil {
  442. err = errors.New("获取问答标签分类失败 Err:" + e.Error())
  443. return
  444. }
  445. // 获取标签
  446. tagItem, e := models.GetVarietyTagById(researchGroupSecondId)
  447. if e != nil {
  448. err = errors.New("获取问答标签失败 Err:" + e.Error())
  449. return
  450. }
  451. // 获取研究员信息失败
  452. researcher, e := system.GetResearcherAdminAndWxUserByAdminId(adminId)
  453. if e != nil {
  454. errMsg = "该研究员未关注公众号或无法正确接收模板消息"
  455. err = errors.New("获取研究员信息失败 Err:" + e.Error())
  456. return
  457. }
  458. // 更新问答
  459. nowTime := time.Now().Local()
  460. updateCols := []string{
  461. "ReplierIsRead", "ReplierUserId", "ReplierAdminId", "ReplierRealName", "ResearchGroupFirstId", "ResearchGroupFirstName",
  462. "ResearchGroupSecondId", "ResearchGroupSecondName", "VarietyTagId", "VarietyTagName", "DistributeAdminId", "DistributeTime", "ModifyTime",
  463. }
  464. item.ReplierIsRead = 0
  465. item.ReplierUserId = researcher.UserId
  466. item.ReplierAdminId = researcher.AdminId
  467. item.ReplierRealName = researcher.AdminName
  468. item.ResearchGroupFirstId = classifyItem.VarietyClassifyId
  469. item.ResearchGroupFirstName = classifyItem.ClassifyName
  470. item.ResearchGroupSecondId = tagItem.VarietyTagId
  471. item.ResearchGroupSecondName = tagItem.TagName
  472. item.VarietyTagId = tagItem.VarietyTagId
  473. item.VarietyTagName = tagItem.TagName
  474. item.DistributeAdminId = distributeId
  475. item.DistributeTime = nowTime
  476. item.ModifyTime = nowTime
  477. if e = item.Update(updateCols); e != nil {
  478. err = errors.New("更新问答失败 Err:" + e.Error())
  479. return
  480. }
  481. // 新增问答流程
  482. go func() {
  483. pro := new(yb.CommunityQuestionProcess)
  484. pro.CommunityQuestionId = item.CommunityQuestionId
  485. pro.TransferUserId = 0
  486. pro.TransferAdminId = distributeId
  487. pro.TransferAdminName = distributeName
  488. pro.ReplierUserId = researcher.UserId
  489. pro.ReplierAdminId = researcher.AdminId
  490. pro.ReplierAdminName = researcher.AdminName
  491. pro.VarietyClassifyId = classifyItem.VarietyClassifyId
  492. pro.VarietyTagId = tagItem.VarietyTagId
  493. pro.Remark = fmt.Sprintf("管理员%s转至%s", distributeName, researcher.AdminName)
  494. pro.ProcessType = yb.ProcessTypeTransfer
  495. pro.CreateTime = nowTime
  496. if e = pro.Create(); e != nil {
  497. return
  498. }
  499. }()
  500. // 推送转移模板消息给研究员
  501. go func() {
  502. remark := fmt.Sprintf("管理员%s移交了新的问答给您, 请点击详情尽快处理", distributeName)
  503. _ = services.SendYbQuestionDistributeWxMsg(item.CommunityQuestionId, researcher.AdminId, researcher.OpenId, item.QuestionContent, remark)
  504. }()
  505. return
  506. }