store-monthly-pending.js
3.26 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 门店日报本月待邀约 — 兼容 V2.3 F2-102 与旧模板 F2-24
*/
const {
STORE_MONTHLY_PENDING_FIELD_KEY,
STORE_MONTHLY_PENDING_FIELD_KEYS
} = require('../config/dailyReportConstants.js')
function isLeafField(field) {
return (
field &&
field.type !== 'group' &&
field.type !== 'subform' &&
field.type !== 'divider'
)
}
function collectViewLeafFields(viewState = {}) {
const leaves = (viewState.fixedFields || []).filter(isLeafField)
for (const group of viewState.sectionGroups || []) {
const children = group.children || []
leaves.push(...children.filter(isLeafField))
}
return leaves
}
/**
* 从当前视图解析实际展示的本月待邀约字段,新模板优先。
* @param {Object} [viewState={}] engine.getViewState 或等价结构
* @returns {string}
*/
function resolveMonthlyPendingFieldKey(viewState = {}) {
const visibleKeys = new Set(collectViewLeafFields(viewState).map(field => field.key))
return STORE_MONTHLY_PENDING_FIELD_KEYS.find(key => visibleKeys.has(key)) || ''
}
/**
* 当前视图是否展示本月待邀约字段(schema 下发且已通过显隐)
* @param {Object} [viewState={}] engine.getViewState 或等价结构
* @returns {boolean}
*/
function isMonthlyPendingFieldVisible(viewState = {}) {
return !!resolveMonthlyPendingFieldKey(viewState)
}
/**
* 目标字段是否视为无值(空串 / null / undefined);0 视为已填
* @param {Object} [values={}]
* @param {string} [fieldKey=STORE_MONTHLY_PENDING_FIELD_KEY]
* @returns {boolean}
*/
function isMonthlyPendingValueEmpty(
values = {},
fieldKey = STORE_MONTHLY_PENDING_FIELD_KEY
) {
const value = values[fieldKey]
return value === '' || value == null
}
/**
* 是否应请求 monthly.pending.count(当天 + 可见 + 无值)
* @param {Object} [options={}]
* @param {boolean} [options.isTodayReport]
* @param {Object} [options.viewState]
* @param {Object} [options.values]
* @returns {boolean}
*/
function shouldFetchMonthlyPendingCount({
isTodayReport = false,
viewState = {},
values = {}
} = {}) {
if (!isTodayReport) {
return false
}
const fieldKey = resolveMonthlyPendingFieldKey(viewState)
if (!fieldKey) {
return false
}
return isMonthlyPendingValueEmpty(values, fieldKey)
}
/**
* 解析本月待邀约字段初始化动作:月首日写 0,非首日请求最近一次提交值。
* @param {Object} [options={}]
* @param {boolean} [options.isTodayReport]
* @param {string} [options.reportDate]
* @param {Object} [options.viewState]
* @param {Object} [options.values]
* @returns {{type: 'set'|'fetch', fieldKey: string, value?: number}|null}
*/
function resolveMonthlyPendingInitialization({
isTodayReport = false,
reportDate = '',
viewState = {},
values = {}
} = {}) {
if (!isTodayReport) {
return null
}
const fieldKey = resolveMonthlyPendingFieldKey(viewState)
if (!fieldKey || !isMonthlyPendingValueEmpty(values, fieldKey)) {
return null
}
if (/^\d{4}-\d{2}-01$/.test(String(reportDate))) {
return { type: 'set', fieldKey, value: 0 }
}
return { type: 'fetch', fieldKey }
}
module.exports = {
STORE_MONTHLY_PENDING_FIELD_KEY,
STORE_MONTHLY_PENDING_FIELD_KEYS,
resolveMonthlyPendingFieldKey,
isMonthlyPendingFieldVisible,
isMonthlyPendingValueEmpty,
shouldFetchMonthlyPendingCount,
resolveMonthlyPendingInitialization
}