'use strict'; const jianSunDatabase = require('../config/jianSunDatabase'); const { PddOpenPlatformError } = require('./pddOpenPlatformClient'); function normalizeText(value, maxLength = 1000) { return String(value ?? '').trim().slice(0, maxLength); } const SHOP_AUTH_BASE_SQL = ` FROM dbo.shop_pdd_auth auth CROSS APPLY ( SELECT TOP 1 candidate.* FROM dbo.sys_PddBill_Service candidate WHERE REPLACE(REPLACE(LTRIM(RTRIM(candidate.name)), N'拼多多-', N''), N' ', N'') = REPLACE(REPLACE(LTRIM(RTRIM(auth.shop_name)), N'拼多多-', N''), N' ', N'') AND candidate.isStop = 0 AND NULLIF(LTRIM(RTRIM(candidate.client_id)), N'') IS NOT NULL AND NULLIF(LTRIM(RTRIM(candidate.secrity)), N'') IS NOT NULL ORDER BY candidate.Id DESC ) service WHERE auth.shop_status = N'正常' AND ISNULL(auth.mark, N'') NOT LIKE N'%已关闭%' AND NULLIF(LTRIM(RTRIM(auth.token)), N'') IS NOT NULL AND auth.experied > DATEADD(MINUTE, 5, GETDATE()) `; function mapPublicShop(row = {}) { return { shopId: normalizeText(row.nick, 160), shopCode: normalizeText(row.mallId, 160), shopName: normalizeText(row.shopName, 255), accountName: normalizeText(row.nick, 160), platform: 'PDD', status: 'active', manager: normalizeText(row.shopManager, 120), director: normalizeText(row.director, 120), tokenExpiresAt: row.tokenExpiredTime || null, credentialSource: 'shop_pdd_auth', }; } async function listPddPublishingShops(options = {}) { const query = options.query || jianSunDatabase.query; const rows = await query(`SELECT auth.shop_name AS shopName, auth.nick, auth.experied AS tokenExpiredTime, auth.shop_manager AS shopManager, auth.director, service.mallID AS mallId ${SHOP_AUTH_BASE_SQL} ORDER BY auth.shop_name`); return { rows: rows.map(mapPublicShop), total: rows.length, source: 'shop_pdd_auth' }; } async function resolvePddPublishingShop(shop = {}, options = {}) { const query = options.query || jianSunDatabase.query; const shopId = normalizeText(shop.shopId || shop.shop_id || shop.nick, 160); const shopName = normalizeText(shop.shopName || shop.shop_name, 255); if (!shopId && !shopName) { throw new PddOpenPlatformError('请选择可上架的拼多多店铺', { code: 'PDD_PUBLISHING_SHOP_REQUIRED', status: 400, }); } const rows = await query(`SELECT TOP 2 auth.shop_name AS shopName, auth.nick, auth.token AS accessToken, auth.experied AS tokenExpiredTime, auth.shop_manager AS shopManager, auth.director, service.Id AS serviceId, service.mallID AS mallId, service.refreshToken, service.client_id AS clientId, service.secrity AS clientSecret ${SHOP_AUTH_BASE_SQL} AND (auth.nick = @shopId OR service.mallID = @shopId OR auth.shop_name = @shopName) ORDER BY CASE WHEN auth.nick = @shopId THEN 0 WHEN service.mallID = @shopId THEN 1 ELSE 2 END`, { shopId, shopName, }); if (rows.length !== 1) { throw new PddOpenPlatformError( rows.length > 1 ? '所选店铺匹配到多条开放平台授权' : '所选店铺不可上架、已关闭或授权已过期', { code: rows.length > 1 ? 'PDD_PUBLISHING_SHOP_AMBIGUOUS' : 'PDD_PUBLISHING_SHOP_UNAVAILABLE', status: 409 } ); } const row = rows[0]; return { shop: mapPublicShop(row), credential: { recordId: normalizeText(row.serviceId, 160), name: normalizeText(row.shopName, 255), mallId: normalizeText(row.mallId, 160), accountName: normalizeText(row.nick, 160), accessToken: normalizeText(row.accessToken, 1000), refreshToken: normalizeText(row.refreshToken, 1000), tokenExpiredTime: row.tokenExpiredTime, clientId: normalizeText(row.clientId, 300), clientSecret: normalizeText(row.clientSecret, 500), }, }; } module.exports = { SHOP_AUTH_BASE_SQL, listPddPublishingShops, mapPublicShop, resolvePddPublishingShop, };