103 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package system
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"errors"
 | |
| 	"time"
 | |
| 
 | |
| 	"management/internal/erpserver/model/dto"
 | |
| 	"management/internal/erpserver/model/system"
 | |
| 	"management/internal/erpserver/service/util"
 | |
| 	"management/internal/erpserver/service/v1"
 | |
| 	"management/internal/pkg/know"
 | |
| 	"management/internal/pkg/know/pearadmin"
 | |
| 
 | |
| 	"gorm.io/gorm"
 | |
| )
 | |
| 
 | |
| type configService struct {
 | |
| 	*v1.Service
 | |
| 	repo system.ConfigRepository
 | |
| }
 | |
| 
 | |
| func NewConfigService(service *v1.Service, repo system.ConfigRepository) v1.ConfigService {
 | |
| 	return &configService{
 | |
| 		Service: service,
 | |
| 		repo:    repo,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (s *configService) Create(ctx context.Context, obj *system.Config) error {
 | |
| 	return s.repo.Create(ctx, obj)
 | |
| }
 | |
| 
 | |
| func (s *configService) Update(ctx context.Context, obj *system.Config) error {
 | |
| 	return s.repo.Update(ctx, obj)
 | |
| }
 | |
| 
 | |
| func (s *configService) Get(ctx context.Context, id int32) (*system.Config, error) {
 | |
| 	return s.repo.Get(ctx, id)
 | |
| }
 | |
| 
 | |
| func (s *configService) List(ctx context.Context, q dto.SearchDto) ([]*system.Config, int64, error) {
 | |
| 	return s.repo.List(ctx, q)
 | |
| }
 | |
| 
 | |
| func (s *configService) Pear(ctx context.Context) (*dto.PearConfig, error) {
 | |
| 	var res *dto.PearConfig
 | |
| 	key := know.GetManageKey(ctx, know.PearAdmin)
 | |
| 	err := util.GetOrSetCache(ctx, s.Redis, key, util.GetCacheExpire(), func() (any, error) {
 | |
| 		conf, err := s.repo.GetByKey(ctx, pearadmin.PearKey)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 
 | |
| 		var pc dto.PearConfig
 | |
| 		err = json.Unmarshal(conf.Value, &pc)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 		return &pc, nil
 | |
| 	}, &res)
 | |
| 	return res, err
 | |
| }
 | |
| 
 | |
| func (s *configService) RefreshCache(ctx context.Context, key string) error {
 | |
| 	return s.Redis.Del(ctx, key)
 | |
| }
 | |
| 
 | |
| func (s *configService) ResetPear(ctx context.Context) error {
 | |
| 	b, err := json.Marshal(pearadmin.PearJson)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	conf, err := s.repo.GetByKey(ctx, pearadmin.PearKey)
 | |
| 	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if errors.Is(err, gorm.ErrRecordNotFound) {
 | |
| 		// create
 | |
| 		conf = &system.Config{
 | |
| 			Key:   pearadmin.PearKey,
 | |
| 			Value: b,
 | |
| 		}
 | |
| 		err = s.Create(ctx, conf)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	} else {
 | |
| 		// update
 | |
| 		conf.Value = b
 | |
| 		conf.UpdatedAt = time.Now()
 | |
| 		err = s.Update(ctx, conf)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 |