voice_broadcast.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_task/models/yb"
  7. "hongze/hongze_task/services/alarm_msg"
  8. "time"
  9. )
  10. // PublishVoiceBroadcast 定时发布语音播报-每分钟
  11. func PublishVoiceBroadcast(cont context.Context) (err error) {
  12. defer func() {
  13. if err != nil {
  14. go alarm_msg.SendAlarmMsg("PublishVoiceBroadcast-定时发布语音播报失败, ErrMsg:\n"+err.Error(), 3)
  15. }
  16. }()
  17. // 获取所有未发布的语音播报
  18. list, e := yb.GetUnpublishedVoiceBroadcastList()
  19. if e != nil {
  20. return
  21. }
  22. listLen := len(list)
  23. if listLen == 0 {
  24. return
  25. }
  26. // 比对时间(分钟),时间相等则发布并推送
  27. format := "200601021504"
  28. nowTime := time.Now().Local().Format(format)
  29. for i := 0; i < listLen; i++ {
  30. item := list[i]
  31. t := item.PrePublishTime.Format(format)
  32. if t == nowTime && item.MsgState == 0 {
  33. if e := VoiceBroadcastUpdateAndPush(item); e != nil {
  34. err = e
  35. return
  36. }
  37. }
  38. }
  39. return
  40. }
  41. // VoiceBroadcastUpdateAndPush 更新发布语音播报
  42. func VoiceBroadcastUpdateAndPush(item *yb.VoiceBroadcast) (err error) {
  43. if item == nil {
  44. return
  45. }
  46. if e := yb.PublishVoiceBroadcast(item.BroadcastId); e != nil {
  47. err = errors.New(fmt.Sprintf("Id: %d, 更新发布状态失败, Err: %s", item.BroadcastId, e.Error()))
  48. return
  49. }
  50. go func() {
  51. fmt.Println("开始发送模板消息")
  52. _ = SendYbVoiceBroadcastWxMsg(item.BroadcastId, item.SectionName, item.BroadcastName)
  53. }()
  54. go func() {
  55. fmt.Println("开始发送客群消息")
  56. _ = SendYbVoiceBroadcastToThs(item.BroadcastId)
  57. }()
  58. return
  59. }