123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package service
- import (
- "encoding/json"
- "fmt"
- "sort"
- "strconv"
- "strings"
- "crazy-fox-backend-api/config"
- "crazy-fox-backend-api/model"
- "crazy-fox-backend-api/repo"
- "crazy-fox-backend-api/storage/redis"
- "crazy-fox-backend-api/utils"
- jsoniter "github.com/json-iterator/go"
- "github.com/pkg/errors"
- "github.com/xuri/excelize/v2"
- )
- // pushService 推送服务
- type pushService struct{}
- func (This *pushService) PushConfig() []model.PushRepoConfig {
- pushKey := config.FirebasePushConf
- pushKey2 := config.FirebasePushConfByKey
- var (
- pushList []model.PushRepoConfig
- err error
- )
- // 缓存存在 且未过期
- //if cache, exist := global.LocalCache.Get(pushKey); exist {
- // return cache.([]model.PushRepoConfig)
- //}
- pushList, _ = repo.Push.GetConfigs(10000, 0)
- langConf := config.LangLower
- for _, pushItem := range pushList {
- for lang, _ := range langConf {
- if _, ok := pushItem.Conf[lang]; !ok {
- pushItem.Conf[lang] = model.PushContentConf{}
- }
- }
- }
- cache1 := make(map[string]any)
- cache2 := make(map[string]any)
- for _, row := range pushList {
- idKey := strconv.Itoa(int(row.Id))
- if cache1[idKey], err = jsoniter.Marshal(row); err != nil {
- return pushList
- }
- if cache2[row.Name], err = jsoniter.Marshal(row.Conf); err != nil {
- return pushList
- }
- }
- redisCli := redis.SelectConn(redis.Games)
- expireAt := utils.RandomExpire(utils.CurTimestamp()+config.OneWeek, 600)
- _ = redisCli.HMSet(pushKey, cache1)
- _ = redisCli.ExpireAt(pushKey, expireAt)
- _ = redisCli.HMSet(pushKey2, cache2)
- _ = redisCli.ExpireAt(pushKey2, expireAt)
- return pushList
- }
- func (This *pushService) PushLangConfig() []model.EnCnTag {
- langConf := config.LangLower
- langList := make([]model.EnCnTag, 0, len(langConf))
- for en, cn := range langConf {
- langList = append(langList, model.EnCnTag{
- En: en,
- Cn: cn,
- })
- }
- sort.Slice(langList, func(i int, j int) bool { return langList[i].En < langList[j].En })
- return langList
- }
- func (This *pushService) SaveConfig(pushConfig model.PushRepoConfig) error {
- var err error
- if pushConfig.Id > 0 {
- err = repo.Update(&pushConfig, "id")
- } else {
- _, err = repo.Insert(&pushConfig)
- }
- if err != nil {
- return errors.Wrap(err, "更新推送配置失败")
- }
- pushKey := config.FirebasePushConf
- pushKey2 := config.FirebasePushConfByKey
- repo.DelCache(pushKey, pushKey2)
- return nil
- }
- func (This *pushService) Export(xlsx *excelize.File, fileName *string) error {
- var (
- Ok bool
- err error
- langConf model.PushContentConf
- langKeys []string
- )
- confList := This.PushConfig()
- langs := config.LangLower
- langKeys = utils.MapKeys(langs, true)
- title := make([]string, 0, (len(langKeys)+1)*2)
- title = append(title, "id", "推送点名称")
- for _, langK := range langKeys {
- title = append(title, fmt.Sprintf("%v-title", langK), fmt.Sprintf("%v-content", langK))
- }
- // 工作表1
- sheetName := "推送点配置"
- if err = xlsx.SetSheetName("Sheet1", sheetName); err != nil {
- return errors.WithStack(err)
- }
- var rowIdx = 1
- if err = xlsx.SetSheetRow(sheetName, fmt.Sprintf("A%v", rowIdx), &title); err != nil {
- return errors.WithStack(err)
- }
- for _, item := range confList {
- rowIdx++
- row := make([]string, 0, (len(langKeys)+1)*2)
- row = append(row, strconv.Itoa(int(item.Id)), item.Name)
- for _, langK := range langKeys {
- if langConf, Ok = item.Conf[langK]; !Ok {
- langConf = model.PushContentConf{
- Title: "",
- Content: "",
- }
- }
- row = append(row, langConf.Title, langConf.Content)
- }
- if err = xlsx.SetSheetRow(sheetName, fmt.Sprintf("A%v", rowIdx), &row); err != nil {
- return errors.WithStack(err)
- }
- }
- return nil
- }
- func (This *pushService) Import(excelInfo [][][]string) error {
- if len(excelInfo) < 1 { // no sheet
- return errors.New("导入数据为空")
- }
- if len(excelInfo[0]) < 1 { // sheet no data
- return errors.New("导入数据为空")
- }
- var (
- contentConf model.PushContentConf
- Ok bool
- err error
- cellContent string
- )
- for i := 1; i < len(excelInfo[0]); i++ {
- updateConf := make(map[string]any)
- row := excelInfo[0][i]
- if len(row) < 2 {
- return errors.New(fmt.Sprintf("第%v行,数据异常%v", i+1, row))
- }
- updateConf["id"] = row[0]
- updateConf["name"] = row[1]
- langConf := make(map[string]model.PushContentConf)
- for j := 2; j < len(excelInfo[0][0]); j++ {
- titleArr := strings.Split(excelInfo[0][0][j], "-")
- if len(titleArr) != 2 {
- return errors.New("标题格式异常:" + excelInfo[0][0][j])
- }
- if contentConf, Ok = langConf[titleArr[0]]; !Ok {
- contentConf = model.PushContentConf{}
- }
- if len(row) <= j {
- cellContent = ""
- } else {
- cellContent = row[j]
- }
- switch titleArr[1] {
- case "title":
- contentConf.Title = cellContent
- case "content":
- contentConf.Content = cellContent
- default:
- return errors.New("标题异常:" + excelInfo[0][0][j])
- }
- langConf[titleArr[0]] = contentConf
- }
- if updateConf["conf"], err = json.Marshal(langConf); err != nil {
- return err
- }
- if err = repo.UpdateByMap[model.PushRepoConfig](updateConf, "id"); err != nil {
- return err
- }
- }
- return nil
- }
|