Files
hfb_sys/backend/internal/modules/mohong/config.go
T
yml2213 183351a297 摸大红支持购物车多选一起结账
下单支持多商品行快照与一次支付;支付后整单复制文案,前台提供购物车加购与结账。
2026-07-16 20:58:39 +08:00

102 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}