push.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "crazy-fox-backend-api/config"
  9. "crazy-fox-backend-api/model"
  10. "crazy-fox-backend-api/repo"
  11. "crazy-fox-backend-api/storage/redis"
  12. "crazy-fox-backend-api/utils"
  13. jsoniter "github.com/json-iterator/go"
  14. "github.com/pkg/errors"
  15. "github.com/xuri/excelize/v2"
  16. )
  17. // pushService 推送服务
  18. type pushService struct{}
  19. func (This *pushService) PushConfig() []model.PushRepoConfig {
  20. pushKey := config.FirebasePushConf
  21. pushKey2 := config.FirebasePushConfByKey
  22. var (
  23. pushList []model.PushRepoConfig
  24. err error
  25. )
  26. // 缓存存在 且未过期
  27. //if cache, exist := global.LocalCache.Get(pushKey); exist {
  28. // return cache.([]model.PushRepoConfig)
  29. //}
  30. pushList, _ = repo.Push.GetConfigs(10000, 0)
  31. langConf := config.LangLower
  32. for _, pushItem := range pushList {
  33. for lang, _ := range langConf {
  34. if _, ok := pushItem.Conf[lang]; !ok {
  35. pushItem.Conf[lang] = model.PushContentConf{}
  36. }
  37. }
  38. }
  39. cache1 := make(map[string]any)
  40. cache2 := make(map[string]any)
  41. for _, row := range pushList {
  42. idKey := strconv.Itoa(int(row.Id))
  43. if cache1[idKey], err = jsoniter.Marshal(row); err != nil {
  44. return pushList
  45. }
  46. if cache2[row.Name], err = jsoniter.Marshal(row.Conf); err != nil {
  47. return pushList
  48. }
  49. }
  50. redisCli := redis.SelectConn(redis.Games)
  51. expireAt := utils.RandomExpire(utils.CurTimestamp()+config.OneWeek, 600)
  52. _ = redisCli.HMSet(pushKey, cache1)
  53. _ = redisCli.ExpireAt(pushKey, expireAt)
  54. _ = redisCli.HMSet(pushKey2, cache2)
  55. _ = redisCli.ExpireAt(pushKey2, expireAt)
  56. return pushList
  57. }
  58. func (This *pushService) PushLangConfig() []model.EnCnTag {
  59. langConf := config.LangLower
  60. langList := make([]model.EnCnTag, 0, len(langConf))
  61. for en, cn := range langConf {
  62. langList = append(langList, model.EnCnTag{
  63. En: en,
  64. Cn: cn,
  65. })
  66. }
  67. sort.Slice(langList, func(i int, j int) bool { return langList[i].En < langList[j].En })
  68. return langList
  69. }
  70. func (This *pushService) SaveConfig(pushConfig model.PushRepoConfig) error {
  71. var err error
  72. if pushConfig.Id > 0 {
  73. err = repo.Update(&pushConfig, "id")
  74. } else {
  75. _, err = repo.Insert(&pushConfig)
  76. }
  77. if err != nil {
  78. return errors.Wrap(err, "更新推送配置失败")
  79. }
  80. pushKey := config.FirebasePushConf
  81. pushKey2 := config.FirebasePushConfByKey
  82. repo.DelCache(pushKey, pushKey2)
  83. return nil
  84. }
  85. func (This *pushService) Export(xlsx *excelize.File, fileName *string) error {
  86. var (
  87. Ok bool
  88. err error
  89. langConf model.PushContentConf
  90. langKeys []string
  91. )
  92. confList := This.PushConfig()
  93. langs := config.LangLower
  94. langKeys = utils.MapKeys(langs, true)
  95. title := make([]string, 0, (len(langKeys)+1)*2)
  96. title = append(title, "id", "推送点名称")
  97. for _, langK := range langKeys {
  98. title = append(title, fmt.Sprintf("%v-title", langK), fmt.Sprintf("%v-content", langK))
  99. }
  100. // 工作表1
  101. sheetName := "推送点配置"
  102. if err = xlsx.SetSheetName("Sheet1", sheetName); err != nil {
  103. return errors.WithStack(err)
  104. }
  105. var rowIdx = 1
  106. if err = xlsx.SetSheetRow(sheetName, fmt.Sprintf("A%v", rowIdx), &title); err != nil {
  107. return errors.WithStack(err)
  108. }
  109. for _, item := range confList {
  110. rowIdx++
  111. row := make([]string, 0, (len(langKeys)+1)*2)
  112. row = append(row, strconv.Itoa(int(item.Id)), item.Name)
  113. for _, langK := range langKeys {
  114. if langConf, Ok = item.Conf[langK]; !Ok {
  115. langConf = model.PushContentConf{
  116. Title: "",
  117. Content: "",
  118. }
  119. }
  120. row = append(row, langConf.Title, langConf.Content)
  121. }
  122. if err = xlsx.SetSheetRow(sheetName, fmt.Sprintf("A%v", rowIdx), &row); err != nil {
  123. return errors.WithStack(err)
  124. }
  125. }
  126. return nil
  127. }
  128. func (This *pushService) Import(excelInfo [][][]string) error {
  129. if len(excelInfo) < 1 { // no sheet
  130. return errors.New("导入数据为空")
  131. }
  132. if len(excelInfo[0]) < 1 { // sheet no data
  133. return errors.New("导入数据为空")
  134. }
  135. var (
  136. contentConf model.PushContentConf
  137. Ok bool
  138. err error
  139. cellContent string
  140. )
  141. for i := 1; i < len(excelInfo[0]); i++ {
  142. updateConf := make(map[string]any)
  143. row := excelInfo[0][i]
  144. if len(row) < 2 {
  145. return errors.New(fmt.Sprintf("第%v行,数据异常%v", i+1, row))
  146. }
  147. updateConf["id"] = row[0]
  148. updateConf["name"] = row[1]
  149. langConf := make(map[string]model.PushContentConf)
  150. for j := 2; j < len(excelInfo[0][0]); j++ {
  151. titleArr := strings.Split(excelInfo[0][0][j], "-")
  152. if len(titleArr) != 2 {
  153. return errors.New("标题格式异常:" + excelInfo[0][0][j])
  154. }
  155. if contentConf, Ok = langConf[titleArr[0]]; !Ok {
  156. contentConf = model.PushContentConf{}
  157. }
  158. if len(row) <= j {
  159. cellContent = ""
  160. } else {
  161. cellContent = row[j]
  162. }
  163. switch titleArr[1] {
  164. case "title":
  165. contentConf.Title = cellContent
  166. case "content":
  167. contentConf.Content = cellContent
  168. default:
  169. return errors.New("标题异常:" + excelInfo[0][0][j])
  170. }
  171. langConf[titleArr[0]] = contentConf
  172. }
  173. if updateConf["conf"], err = json.Marshal(langConf); err != nil {
  174. return err
  175. }
  176. if err = repo.UpdateByMap[model.PushRepoConfig](updateConf, "id"); err != nil {
  177. return err
  178. }
  179. }
  180. return nil
  181. }