Files
yml2213 a4189db4ca 业务更名为撞车并切换crash路由
用户可见文案与页面/API路径改为撞车与crash,保留mohong表与权限码兼容;移动端入口去掉租/撞图标。
2026-07-16 21:19:35 +08:00

102 lines
3.0 KiB
Go
Raw Permalink 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
}