fieldOptionsService.js 1.12 KB
const { fetchFieldOptionsAdapter } = require('./fieldOptionsAdapters.js')

const optionsCache = new Map()

function buildCacheKey(params = {}) {
	const brandKey = params.carBrandTypeFlag != null && params.carBrandTypeFlag !== ''
		? String(params.carBrandTypeFlag)
		: Array.isArray(params.groupBrandIds)
			? params.groupBrandIds.join(',')
			: ''
	return [
		params.optionsKey || '',
		params.merchantId || '',
		params.yqMerchantId || '',
		params.staffUuid || '',
		params.reportDate || '',
		params.saleDate || '',
		params.reportType || 'store',
		brandKey,
		params.name || '',
		JSON.stringify(params.sceneList || [])
	].join('|')
}

async function fetchFieldOptions(params = {}, options = {}) {
	const { useCache = true } = options
	const cacheKey = buildCacheKey(params)
	if (useCache && optionsCache.has(cacheKey)) {
		return optionsCache.get(cacheKey)
	}

	const result = await fetchFieldOptionsAdapter(params)
	if (result?.code === 0 && useCache) {
		optionsCache.set(cacheKey, result)
	}
	return result
}

function clearFieldOptionsCache() {
	optionsCache.clear()
}

module.exports = {
	fetchFieldOptions,
	clearFieldOptionsCache
}