negotiationDailyReportCache.js
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* 洽谈明细进程内缓存 — 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
}