178 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			178 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package config
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 
 | |
| 	"management/internal/erpserver/model/dto"
 | |
| 	"management/internal/erpserver/model/system"
 | |
| 	"management/internal/erpserver/repository"
 | |
| 	"management/internal/pkg/database"
 | |
| 	"management/internal/pkg/know/pearadmin"
 | |
| 	"management/internal/pkg/sqldb"
 | |
| 
 | |
| 	"github.com/drhin/logger"
 | |
| 	"gorm.io/datatypes"
 | |
| )
 | |
| 
 | |
| type store struct {
 | |
| 	db  *repository.Store
 | |
| 	log *logger.Logger
 | |
| }
 | |
| 
 | |
| func NewStore(db *repository.Store, log *logger.Logger) system.ConfigRepository {
 | |
| 	return &store{
 | |
| 		db:  db,
 | |
| 		log: log,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (s *store) Initialize(ctx context.Context) error {
 | |
| 	_, err := s.GetByKey(ctx, pearadmin.PearKey)
 | |
| 	if err != nil {
 | |
| 		if database.IsNoRows(err) {
 | |
| 			b, e := json.Marshal(pearadmin.PearJson)
 | |
| 			if e != nil {
 | |
| 				return e
 | |
| 			}
 | |
| 
 | |
| 			return s.Create(ctx, &system.Config{
 | |
| 				Key:   pearadmin.PearKey,
 | |
| 				Value: datatypes.JSON(b),
 | |
| 			})
 | |
| 		}
 | |
| 		return err
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (s *store) Create(ctx context.Context, obj *system.Config) error {
 | |
| 	//goland:noinspection ALL
 | |
| 	const q = `
 | |
| 	INSERT INTO sys_config (
 | |
| 		key, value
 | |
| 	) VALUES (
 | |
| 		:key, :value
 | |
| 	);`
 | |
| 
 | |
| 	return sqldb.NamedExecContext(ctx, s.log, s.db.DB(ctx), q, obj)
 | |
| }
 | |
| 
 | |
| func (s *store) Update(ctx context.Context, obj *system.Config) error {
 | |
| 	const q = `
 | |
| 	UPDATE sys_config
 | |
| 	SET key = :key,
 | |
| 		value = :value,
 | |
| 		updated_at = :updated_at
 | |
| 	WHERE id = :id;`
 | |
| 
 | |
| 	return sqldb.NamedExecContext(ctx, s.log, s.db.DB(ctx), q, obj)
 | |
| }
 | |
| 
 | |
| func (s *store) Get(ctx context.Context, id int32) (*system.Config, error) {
 | |
| 	//goland:noinspection ALL
 | |
| 	const q = `
 | |
| 	SELECT
 | |
| 		id, key, value, created_at, updated_at
 | |
| 	FROM 
 | |
| 		sys_config
 | |
| 	WHERE 
 | |
| 		id = :id;`
 | |
| 
 | |
| 	data := map[string]any{
 | |
| 		"id": id,
 | |
| 	}
 | |
| 
 | |
| 	var config system.Config
 | |
| 	err := sqldb.NamedQueryStruct(ctx, s.log, s.db.DB(ctx), q, data, &config)
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("select id config: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	return &config, nil
 | |
| }
 | |
| 
 | |
| func (s *store) GetByKey(ctx context.Context, key string) (*system.Config, error) {
 | |
| 	//goland:noinspection ALL
 | |
| 	const q = `
 | |
| 	SELECT
 | |
| 		id, key, value, created_at, updated_at
 | |
| 	FROM 
 | |
| 		sys_config
 | |
| 	WHERE 
 | |
| 		key = :key;`
 | |
| 
 | |
| 	data := map[string]any{
 | |
| 		"key": key,
 | |
| 	}
 | |
| 
 | |
| 	var config system.Config
 | |
| 	err := sqldb.NamedQueryStruct(ctx, s.log, s.db.DB(ctx), q, data, &config)
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("select key config: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	return &config, nil
 | |
| }
 | |
| 
 | |
| func (s *store) Count(ctx context.Context, filter dto.SearchDto) (int64, error) {
 | |
| 	//goland:noinspection ALL
 | |
| 	const q = `
 | |
| 	SELECT 
 | |
| 		COUNT(1)
 | |
| 	FROM 
 | |
| 		sys_config`
 | |
| 
 | |
| 	data := map[string]any{}
 | |
| 
 | |
| 	buf := bytes.NewBufferString(q)
 | |
| 	applyFilter(filter, data, buf)
 | |
| 
 | |
| 	var count struct {
 | |
| 		Count int64 `db:"count"`
 | |
| 	}
 | |
| 
 | |
| 	err := sqldb.NamedQueryStruct(ctx, s.log, s.db.DB(ctx), buf.String(), data, &count)
 | |
| 	if err != nil {
 | |
| 		return 0, fmt.Errorf("select count config: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	return count.Count, nil
 | |
| }
 | |
| func (s *store) List(ctx context.Context, filter dto.SearchDto) ([]*system.Config, error) {
 | |
| 	//goland:noinspection ALL
 | |
| 	const q = `
 | |
| 	SELECT
 | |
| 		id, key, value, created_at, updated_at
 | |
| 	FROM 
 | |
| 		sys_config`
 | |
| 
 | |
| 	data := map[string]any{
 | |
| 		"offset":        (filter.Page - 1) * filter.Rows,
 | |
| 		"rows_per_page": filter.Rows,
 | |
| 	}
 | |
| 
 | |
| 	buf := bytes.NewBufferString(q)
 | |
| 	applyFilter(filter, data, buf)
 | |
| 
 | |
| 	buf.WriteString(" ORDER BY id DESC")
 | |
| 	buf.WriteString(" LIMIT :rows_per_page OFFSET :offset")
 | |
| 
 | |
| 	var configs []system.Config
 | |
| 	err := sqldb.NamedQuerySlice(ctx, s.log, s.db.DB(ctx), buf.String(), data, &configs)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return toPointer(configs), nil
 | |
| }
 | |
| 
 | |
| func toPointer(data []system.Config) []*system.Config {
 | |
| 	var res []*system.Config
 | |
| 	for _, v := range data {
 | |
| 		res = append(res, &v)
 | |
| 	}
 | |
| 	return res
 | |
| }
 |