negotiationDailyReportCache.js 2.76 KB
/**
 * 洽谈明细进程内缓存 — scene / schema / brand
 * key:scene=staffUuid:merchantId:sceneType;brand=merchantId;schema=reportTemplateId:baseSceneCode
 */
const sceneCache = new Map()
const schemaCache = new Map()
const brandCache = new Map()

function getSceneCacheKey(staffUuid, merchantId, sceneType) {
	return `${staffUuid || ''}:${merchantId || ''}:${sceneType ?? 0}`
}

/**
 * 生成 schema 缓存键
 * @param {number|string} reportTemplateId 模板 ID
 * @param {string} baseSceneCode 门店场景编码
 * @returns {string}
 */
function getSchemaCacheKey(reportTemplateId, baseSceneCode) {
	return `${reportTemplateId || ''}:${baseSceneCode || ''}`
}

/**
 * 读取门店场景缓存
 * @param {string|number} merchantId 门店 ID
 * @returns {Object|null} 缓存的 API 响应
 */
function getCachedScene(staffUuid, merchantId, sceneType) {
	const key = getSceneCacheKey(staffUuid, merchantId, sceneType)
	if (!key || key === '::0') return null
	return sceneCache.get(key) || null
}

function setCachedScene(staffUuid, merchantId, sceneType, value) {
	const key = getSceneCacheKey(staffUuid, merchantId, sceneType)
	if (!key || key === '::0') return
	sceneCache.set(key, value)
}

/**
 * 读取 schema 根节点缓存
 * @param {number|string} reportTemplateId 模板 ID(或 moduleUrl)
 * @param {string} baseSceneCode 门店场景编码
 * @returns {Object|null} 缓存的 schema root 响应
 */
function getCachedSchema(reportTemplateId, baseSceneCode) {
	return schemaCache.get(getSchemaCacheKey(reportTemplateId, baseSceneCode)) || null
}

/**
 * 写入 schema 根节点缓存
 * @param {number|string} reportTemplateId 模板 ID(或 moduleUrl)
 * @param {string} baseSceneCode 门店场景编码
 * @param {Object} value schema root 响应
 * @returns {void}
 */
function setCachedSchema(reportTemplateId, baseSceneCode, value) {
	schemaCache.set(getSchemaCacheKey(reportTemplateId, baseSceneCode), value)
}

/**
 * 读取经营品牌缓存
 * @param {string|number} merchantId 门店 ID
 * @returns {Object|null} 缓存的品牌 API 响应
 */
function getCachedBrand(merchantId) {
	if (!merchantId) return null
	return brandCache.get(merchantId) || null
}

/**
 * 写入经营品牌缓存
 * @param {string|number} merchantId 门店 ID
 * @param {Object} value 品牌 API 响应
 * @returns {void}
 */
function setCachedBrand(merchantId, value) {
	if (!merchantId) return
	brandCache.set(merchantId, value)
}

/**
 * 清空洽谈明细全部进程内缓存
 * @returns {void}
 */
function clearNegotiationDailyReportCache() {
	sceneCache.clear()
	schemaCache.clear()
	brandCache.clear()
}

module.exports = {
	getSceneCacheKey,
	getSchemaCacheKey,
	getCachedScene,
	setCachedScene,
	getCachedSchema,
	setCachedSchema,
	getCachedBrand,
	setCachedBrand,
	clearNegotiationDailyReportCache
}