+18
-63
@@ -16,15 +16,11 @@ var (
|
||||
ErrShopHasReferences = errors.New("店铺仍有关联数据")
|
||||
)
|
||||
|
||||
// ListShops 返回管理页的一店一行汇总;首版每个渠道维护一个主别名。
|
||||
// ListShops 返回管理页的一店一行汇总。
|
||||
func ListShops(q Execer) ([]model.Shop, error) {
|
||||
rows, err := q.Query(`SELECT s.shop_id,s.display_name,s.normalized_name,s.enabled,
|
||||
COALESCE(MAX(CASE WHEN a.channel='syb' THEN a.alias_name END),''),
|
||||
COALESCE(MAX(CASE WHEN a.channel='syb' THEN a.enabled END),0),
|
||||
COALESCE(MAX(CASE WHEN a.channel='shopee' THEN a.alias_name END),''),
|
||||
COUNT(DISTINCT sp.goods_id),s.created_by_user_id,s.created_at,s.updated_at
|
||||
FROM shops s
|
||||
LEFT JOIN shop_channel_aliases a ON a.shop_id=s.shop_id
|
||||
LEFT JOIN shopee_products sp ON sp.shop_id=s.shop_id AND sp.deleted_at IS NULL
|
||||
GROUP BY s.shop_id ORDER BY s.enabled DESC,s.normalized_name`)
|
||||
if err != nil {
|
||||
@@ -32,34 +28,11 @@ func ListShops(q Execer) ([]model.Shop, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []model.Shop
|
||||
for rows.Next() {
|
||||
var item model.Shop
|
||||
var enabled, sybEnabled int
|
||||
if err := rows.Scan(&item.ShopID, &item.DisplayName, &item.NormalizedName, &enabled,
|
||||
&item.SybAlias, &sybEnabled, &item.ShopeeAlias, &item.ShopeeProductCount,
|
||||
&item.CreatedByUserID, &item.CreatedAt, &item.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
item.Enabled = enabled == 1
|
||||
item.SybSyncEnabled = sybEnabled == 1
|
||||
result = append(result, item)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// ListShopOptions 返回筛选器使用的全部业务店铺,停用项仍保留以便查历史数据。
|
||||
func ListShopOptions(q Execer) ([]model.Shop, error) {
|
||||
rows, err := q.Query(`SELECT shop_id,display_name,normalized_name,enabled,created_by_user_id,created_at,updated_at
|
||||
FROM shops ORDER BY enabled DESC,normalized_name`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询店铺选项失败: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []model.Shop
|
||||
for rows.Next() {
|
||||
var item model.Shop
|
||||
var enabled int
|
||||
if err := rows.Scan(&item.ShopID, &item.DisplayName, &item.NormalizedName, &enabled,
|
||||
&item.ShopeeProductCount,
|
||||
&item.CreatedByUserID, &item.CreatedAt, &item.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,24 +58,9 @@ func GetShop(q Execer, shopID string) (model.Shop, bool, error) {
|
||||
return item, true, nil
|
||||
}
|
||||
|
||||
func GetShopAliasEnabled(q Execer, shopID, channel string) (bool, bool, error) {
|
||||
var enabled int
|
||||
err := q.QueryRow(`SELECT enabled FROM shop_channel_aliases WHERE shop_id=? AND channel=? LIMIT 1`,
|
||||
shopID, channel).Scan(&enabled)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, false, fmt.Errorf("查询渠道店铺状态失败: %w", err)
|
||||
}
|
||||
return enabled == 1, true, nil
|
||||
}
|
||||
|
||||
// ListEnabledSybShopMappings 返回“SYB 原始精确名称 → 业务店铺 ID”。
|
||||
// ListEnabledSybShopMappings 返回“店铺精确名称 → 店铺 ID”。
|
||||
func ListEnabledSybShopMappings(q Execer) (map[string]string, error) {
|
||||
rows, err := q.Query(`SELECT a.normalized_alias,a.shop_id FROM shop_channel_aliases a
|
||||
JOIN shops s ON s.shop_id=a.shop_id WHERE a.channel='syb' AND a.enabled=1 AND s.enabled=1
|
||||
ORDER BY a.normalized_alias`)
|
||||
rows, err := q.Query(`SELECT normalized_name,shop_id FROM shops WHERE enabled=1 ORDER BY normalized_name`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询启用的 SYB 店铺配置失败: %w", err)
|
||||
}
|
||||
@@ -118,19 +76,18 @@ func ListEnabledSybShopMappings(q Execer) (map[string]string, error) {
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func FindShopIDByAlias(q Execer, channel, rawName string) (string, error) {
|
||||
func FindShopIDByName(q Execer, rawName string) (string, error) {
|
||||
name := strings.TrimSpace(rawName)
|
||||
if name == "" {
|
||||
return "", nil
|
||||
}
|
||||
var shopID string
|
||||
err := q.QueryRow(`SELECT shop_id FROM shop_channel_aliases
|
||||
WHERE channel=? AND normalized_alias=?`, channel, name).Scan(&shopID)
|
||||
err := q.QueryRow(`SELECT shop_id FROM shops WHERE normalized_name=?`, name).Scan(&shopID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("按 %s 店铺名称查业务店铺失败: %w", channel, err)
|
||||
return "", fmt.Errorf("按店铺名称查询店铺失败: %w", err)
|
||||
}
|
||||
return shopID, nil
|
||||
}
|
||||
@@ -186,14 +143,12 @@ func SetShopEnabled(q Execer, shopID string, enabled bool, updatedAt string) (bo
|
||||
return n == 1, err
|
||||
}
|
||||
|
||||
func SetShopSybEnabled(q Execer, shopID string, enabled bool, updatedAt string) (bool, error) {
|
||||
result, err := q.Exec(`UPDATE shop_channel_aliases SET enabled=?,updated_at=?
|
||||
WHERE shop_id=? AND channel='syb'`, enabled, updatedAt, shopID)
|
||||
func SetShopCompatibilityAliasesEnabled(q Execer, shopID string, enabled bool, updatedAt string) error {
|
||||
_, err := q.Exec(`UPDATE shop_channel_aliases SET enabled=?,updated_at=? WHERE shop_id=?`, enabled, updatedAt, shopID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("更新 SYB 同步状态失败: %w", err)
|
||||
return fmt.Errorf("同步店铺兼容数据状态失败: %w", err)
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
return n > 0, err
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReplaceShopAlias(q Execer, aliasID, shopID, channel, aliasName, updatedAt string, enabled bool) error {
|
||||
@@ -208,7 +163,7 @@ func ReplaceShopAlias(q Execer, aliasID, shopID, channel, aliasName, updatedAt s
|
||||
CreatedAt: updatedAt, UpdatedAt: updatedAt})
|
||||
}
|
||||
|
||||
// RebuildShopAssociations 按渠道原始名称精确重建一个店铺的派生关联。
|
||||
// RebuildShopAssociations 按唯一店铺名称精确重建一个店铺的派生关联。
|
||||
func RebuildShopAssociations(q Execer, shopID string) error {
|
||||
if _, err := q.Exec(`UPDATE syb_orders SET shop_id=NULL WHERE shop_id=?`, shopID); err != nil {
|
||||
return err
|
||||
@@ -216,14 +171,14 @@ func RebuildShopAssociations(q Execer, shopID string) error {
|
||||
if _, err := q.Exec(`UPDATE shopee_products SET shop_id=NULL WHERE shop_id=?`, shopID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := q.Exec(`UPDATE syb_orders so JOIN shop_channel_aliases a
|
||||
ON a.shop_id=? AND a.channel='syb' AND a.normalized_alias=TRIM(so.shop_name) COLLATE utf8mb4_bin
|
||||
SET so.shop_id=a.shop_id`, shopID); err != nil {
|
||||
if _, err := q.Exec(`UPDATE syb_orders so JOIN shops s
|
||||
ON s.shop_id=? AND s.normalized_name=TRIM(so.shop_name) COLLATE utf8mb4_bin
|
||||
SET so.shop_id=s.shop_id`, shopID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := q.Exec(`UPDATE shopee_products sp JOIN shop_channel_aliases a
|
||||
ON a.shop_id=? AND a.channel='shopee' AND a.normalized_alias=TRIM(sp.shopee_shop_name) COLLATE utf8mb4_bin
|
||||
SET sp.shop_id=a.shop_id`, shopID); err != nil {
|
||||
if _, err := q.Exec(`UPDATE shopee_products sp JOIN shops s
|
||||
ON s.shop_id=? AND s.normalized_name=TRIM(sp.shopee_shop_name) COLLATE utf8mb4_bin
|
||||
SET sp.shop_id=s.shop_id`, shopID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user