feat: 按允许店铺筛选顺运宝同步 (#196)
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cmautobuy/admin/model"
|
||||
"cmautobuy/admin/repository"
|
||||
)
|
||||
|
||||
const maxSybShopNameLength = 500
|
||||
|
||||
func ListSybAllowedShops(db *sql.DB, actor *model.User) ([]model.SybAllowedShop, error) {
|
||||
if actor == nil || !actor.IsAdmin() {
|
||||
return nil, ErrAdminRequired
|
||||
}
|
||||
return repository.ListSybAllowedShops(db)
|
||||
}
|
||||
|
||||
func CountEnabledSybAllowedShops(db *sql.DB) (int, error) {
|
||||
names, err := repository.ListEnabledSybShopNames(db)
|
||||
return len(names), err
|
||||
}
|
||||
|
||||
func EnsureEnabledSybAllowedShops(db *sql.DB) error {
|
||||
count, err := CountEnabledSybAllowedShops(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return &validationError{field: "shop_name", message: "没有启用的顺运宝同步店铺,请先由管理员配置并启用至少一个店铺"}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateSybAllowedShop(db *sql.DB, actor *model.User, rawName string, now time.Time) error {
|
||||
if actor == nil || !actor.IsAdmin() {
|
||||
return ErrAdminRequired
|
||||
}
|
||||
name := strings.TrimSpace(rawName)
|
||||
if name == "" {
|
||||
return &validationError{field: "shop_name", message: "店铺名称不能为空"}
|
||||
}
|
||||
if len([]rune(name)) > maxSybShopNameLength {
|
||||
return &validationError{field: "shop_name", message: "店铺名称最多 500 个字符"}
|
||||
}
|
||||
id, err := randomID("SHOP-", 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("生成店铺编号失败: %w", err)
|
||||
}
|
||||
at := now.UTC().Format(model.TimeLayout)
|
||||
err = repository.InsertSybAllowedShop(db, model.SybAllowedShop{
|
||||
ShopID: id, ShopName: name, NormalizedName: name, Enabled: true,
|
||||
CreatedByUserID: actor.UserID, CreatedAt: at, UpdatedAt: at,
|
||||
})
|
||||
if errors.Is(err, repository.ErrSybAllowedShopExists) {
|
||||
return &validationError{field: "shop_name", message: "该店铺已经在允许列表中,可直接重新启用"}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func SetSybAllowedShopEnabled(db *sql.DB, actor *model.User, shopID string, enabled bool, now time.Time) error {
|
||||
if actor == nil || !actor.IsAdmin() {
|
||||
return ErrAdminRequired
|
||||
}
|
||||
if strings.TrimSpace(shopID) == "" {
|
||||
return &validationError{field: "shop_id", message: "店铺编号不能为空"}
|
||||
}
|
||||
found, err := repository.SetSybAllowedShopEnabled(db, shopID, enabled, now.UTC().Format(model.TimeLayout))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
return &validationError{field: "shop_id", message: "店铺不存在,请刷新页面后重试"}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user