chore(init): 初始化解忧商行项目并完成 Phase 0 基座
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package mohong
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
const (
|
||||
configKeyDefaultQrcodeURL = "mohong.default_qrcode_url"
|
||||
configKeyOrderCopyTemplate = "mohong.order_copy_template"
|
||||
|
||||
defaultOrderCopyTemplate = "【撞车订单】\n订单号:{{order_no}}\n下单时间:{{created_at}}\n商品明细:\n{{items}}\n数量合计:{{quantity}}\n金额:{{amount}}\n下单人:{{buyer_name}}({{buyer_phone}})"
|
||||
)
|
||||
|
||||
func (r *Repository) GetConfig(ctx context.Context) (*ConfigDTO, error) {
|
||||
if r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
keys := []string{configKeyDefaultQrcodeURL, configKeyOrderCopyTemplate}
|
||||
var rows []model.SystemConfig
|
||||
if err := r.db.WithContext(ctx).Where("`key` IN ?", keys).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values := map[string]string{}
|
||||
for _, row := range rows {
|
||||
values[row.Key] = row.Value
|
||||
}
|
||||
return &ConfigDTO{
|
||||
DefaultQrcodeURL: strings.TrimSpace(values[configKeyDefaultQrcodeURL]),
|
||||
OrderCopyTemplate: firstNonEmpty(values[configKeyOrderCopyTemplate], defaultOrderCopyTemplate),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateConfig(ctx context.Context, req UpdateConfigRequest) (*ConfigDTO, error) {
|
||||
if r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if req.DefaultQrcodeURL != nil {
|
||||
if err := upsertConfig(tx, configKeyDefaultQrcodeURL, strings.TrimSpace(*req.DefaultQrcodeURL), "撞车全局默认固定二维码图片URL"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if req.OrderCopyTemplate != nil {
|
||||
if err := upsertConfig(tx, configKeyOrderCopyTemplate, strings.TrimSpace(*req.OrderCopyTemplate), "撞车订单一键复制文案模板"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.GetConfig(ctx)
|
||||
}
|
||||
|
||||
func upsertConfig(tx *gorm.DB, key, value, description string) error {
|
||||
row := model.SystemConfig{
|
||||
Key: key,
|
||||
Value: value,
|
||||
Description: description,
|
||||
}
|
||||
return tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "key"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"value", "description"}),
|
||||
}).Create(&row).Error
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, v := range values {
|
||||
if strings.TrimSpace(v) != "" {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Repository) resolveQrcodeURL(tx *gorm.DB, productQrcode string) string {
|
||||
productQrcode = strings.TrimSpace(productQrcode)
|
||||
if productQrcode != "" {
|
||||
return productQrcode
|
||||
}
|
||||
var cfg model.SystemConfig
|
||||
if err := tx.Where("`key` = ?", configKeyDefaultQrcodeURL).First(&cfg).Error; err == nil {
|
||||
return strings.TrimSpace(cfg.Value)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Repository) loadCopyTemplate(tx *gorm.DB) string {
|
||||
var cfg model.SystemConfig
|
||||
if err := tx.Where("`key` = ?", configKeyOrderCopyTemplate).First(&cfg).Error; err == nil && strings.TrimSpace(cfg.Value) != "" {
|
||||
return strings.TrimSpace(cfg.Value)
|
||||
}
|
||||
return defaultOrderCopyTemplate
|
||||
}
|
||||
Reference in New Issue
Block a user