wxtplmsgcustom.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package p
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "app.yhyue.com/moapp/jybase/mysql"
  7. "strings"
  8. "sync"
  9. "time"
  10. "app.yhyue.com/moapp/jybase/logger"
  11. )
  12. type WxTplMsgCustom struct {
  13. FirstData string
  14. AdvertCode string
  15. }
  16. //
  17. type WxTplMsgCustoms struct {
  18. All *sync.Map
  19. Mysql *mysql.Mysql
  20. TplId string
  21. SceneCode []string
  22. }
  23. /* 新建结构体
  24. * @Param mysql 数据库实例
  25. * @Param tplId 模板消息id
  26. * @Param sceneCode 场景代码
  27. * @Return error
  28. */
  29. func NewWxTplMsgCustoms(m *mysql.Mysql, tplId string, sceneCode ...string) *WxTplMsgCustoms {
  30. return &WxTplMsgCustoms{
  31. All: &sync.Map{},
  32. Mysql: m,
  33. TplId: tplId,
  34. SceneCode: sceneCode,
  35. }
  36. }
  37. //初始化自定义模板消息,如果有异常,程序终止
  38. func (w *WxTplMsgCustoms) ForceVerify() {
  39. w.init(true)
  40. }
  41. //重新加载自定义模板消息
  42. func (w *WxTplMsgCustoms) Reload() {
  43. w.init(false)
  44. }
  45. //初始化自定义模板消息
  46. func (w *WxTplMsgCustoms) init(isVerify bool) {
  47. param := []interface{}{w.TplId}
  48. wh := []string{}
  49. for _, v := range w.SceneCode {
  50. param = append(param, v)
  51. wh = append(wh, "?")
  52. }
  53. list := w.Mysql.SelectBySql(`select a.sceneCode,c.firstdata,c.advertCode from scene a
  54. inner join template_message b on (a.templateId=b.id and b.templateId=? and a.sceneCode in (`+strings.Join(wh, ",")+`))
  55. inner join template_config c on (a.id=c.sceneId and c.state=1 and c.isTest=1)`, param...)
  56. all := map[string][]*WxTplMsgCustom{}
  57. if list != nil {
  58. for _, v := range *list {
  59. sceneCode, _ := v["sceneCode"].(string)
  60. sceneCode = strings.TrimSpace(sceneCode)
  61. firstdata, _ := v["firstdata"].(string)
  62. firstdata = strings.TrimSpace(firstdata)
  63. advertCode, _ := v["advertCode"].(string)
  64. advertCode = strings.TrimSpace(advertCode)
  65. if sceneCode == "" || firstdata == "" {
  66. continue
  67. }
  68. all[sceneCode] = append(all[sceneCode], &WxTplMsgCustom{
  69. FirstData: firstdata,
  70. AdvertCode: advertCode,
  71. })
  72. }
  73. }
  74. for k, v := range all {
  75. if len(v) > 0 {
  76. w.All.Store(k, v)
  77. }
  78. }
  79. w.All.Range(func(key, value interface{}) bool {
  80. v, _ := value.([]*WxTplMsgCustom)
  81. for _, vv := range v {
  82. logger.Info("微信自定义模板消息 id", w.TplId, "场景代码", key, "广告位代码", vv.AdvertCode, "firstData", vv.FirstData)
  83. }
  84. return true
  85. })
  86. for _, v := range w.SceneCode {
  87. if len(all[v]) == 0 {
  88. msg := fmt.Sprintf("微信自定义模板消息 id %s 场景代码 %s 配置异常", w.TplId, v)
  89. if isVerify {
  90. log.Fatalln(msg)
  91. } else {
  92. logger.Error(msg)
  93. }
  94. }
  95. }
  96. }
  97. /* 随机获取自定义模板消息
  98. * @Param sceneCode 场景代码
  99. */
  100. func (w *WxTplMsgCustoms) Get(sceneCode string) *WxTplMsgCustom {
  101. value, ok := w.All.Load(sceneCode)
  102. if !ok {
  103. return nil
  104. }
  105. values, _ := value.([]*WxTplMsgCustom)
  106. if len(values) == 0 {
  107. return nil
  108. }
  109. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  110. return values[r.Intn(len(values))]
  111. }