96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			96 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package system
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"time"
 | |
| 
 | |
| 	"management/internal/db/model/dto"
 | |
| 	db "management/internal/db/sqlc"
 | |
| 	"management/internal/pkg/know"
 | |
| 	"management/internal/pkg/know/pearadmin"
 | |
| 	"management/internal/pkg/redis"
 | |
| )
 | |
| 
 | |
| type ConfigBiz interface {
 | |
| 	Create(ctx context.Context, arg *db.CreateSysConfigParams) error
 | |
| 	Update(ctx context.Context, arg *db.UpdateSysConfigByKeyParams) error
 | |
| 	Get(ctx context.Context, id int32) (*db.SysConfig, error)
 | |
| 	List(ctx context.Context, q dto.SearchDto) ([]*db.SysConfig, int64, error)
 | |
| 
 | |
| 	ConfigExpansion
 | |
| }
 | |
| 
 | |
| type ConfigExpansion interface {
 | |
| 	Pear(ctx context.Context) (*dto.PearConfig, error)
 | |
| }
 | |
| 
 | |
| type configBiz struct {
 | |
| 	store db.Store
 | |
| 	redis redis.IRedis
 | |
| }
 | |
| 
 | |
| var _ ConfigBiz = (*configBiz)(nil)
 | |
| 
 | |
| func NewConfig(store db.Store, redis redis.IRedis) *configBiz {
 | |
| 	return &configBiz{
 | |
| 		store: store,
 | |
| 		redis: redis,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (b *configBiz) Create(ctx context.Context, arg *db.CreateSysConfigParams) error {
 | |
| 	return b.store.CreateSysConfig(ctx, arg)
 | |
| }
 | |
| 
 | |
| func (b *configBiz) Update(ctx context.Context, arg *db.UpdateSysConfigByKeyParams) error {
 | |
| 	return b.store.UpdateSysConfigByKey(ctx, arg)
 | |
| }
 | |
| 
 | |
| func (b *configBiz) Get(ctx context.Context, id int32) (*db.SysConfig, error) {
 | |
| 	return b.store.GetSysConfig(ctx, id)
 | |
| }
 | |
| 
 | |
| func (b *configBiz) List(ctx context.Context, q dto.SearchDto) ([]*db.SysConfig, int64, error) {
 | |
| 	count, err := b.store.CountSysConfigCondition(ctx, q.SearchKey)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	configs, err := b.store.ListSysConfigCondition(ctx, &db.ListSysConfigConditionParams{
 | |
| 		Key:  q.SearchName,
 | |
| 		Skip: (int32(q.Page) - 1) * int32(q.Rows),
 | |
| 		Size: int32(q.Rows),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	return configs, count, nil
 | |
| }
 | |
| 
 | |
| func (b *configBiz) Pear(ctx context.Context) (*dto.PearConfig, error) {
 | |
| 	// 判断redis是否存储
 | |
| 	key := know.GetManageKey(ctx, know.PearAdmin)
 | |
| 	bs, err := b.redis.GetBytes(ctx, key)
 | |
| 	if err == nil {
 | |
| 		var res *dto.PearConfig
 | |
| 		if err := json.Unmarshal(bs, &res); err == nil {
 | |
| 			return res, nil
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	conf, err := b.store.GetSysConfigByKey(ctx, pearadmin.PearKey)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	var pear dto.PearConfig
 | |
| 	if err := json.Unmarshal(conf.Value, &pear); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	_ = b.redis.Set(ctx, key, conf.Value, time.Hour*6)
 | |
| 	return &pear, nil
 | |
| }
 |