plan-fields.js
9.58 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* 计划书字段配置
*
* @description 统一管理所有计划书字段的配置信息,包括字段类型、验证规则、API 映射等
* @module config/plan-fields
* @author Claude Code
* @created 2026-02-14
* @version 1.1.0 - 添加字段分组功能
*/
/**
* 字段类型枚举
* @enum {string}
*/
export const FIELD_TYPES = {
TEXT: 'text',
NUMBER: 'number',
AMOUNT: 'amount',
PERCENTAGE: 'percentage',
SELECT: 'select',
RADIO: 'radio',
DATE: 'date',
NAME: 'name'
}
/**
* 字段分组枚举
* @enum {string}
*/
export const FIELD_GROUPS = {
BASIC: 'basic', // 基本信息:姓名、性别、生日
COVERAGE: 'coverage', // 保障:保额、缴费年期
WITHDRAWAL: 'withdrawal' // 提取:提取方式、金额等
}
/**
* 数据转换类型枚举
* @enum {string}
*/
export const TRANSFORM_TYPES = {
FEN_TO_YUAN: 'fen_to_yuan', // 分转元
YUAN_TO_FEN: 'yuan_to_fen', // 元转分
NONE: 'none' // 无需转换
}
/**
* 计划书字段定义
* @type {Object<string, FieldDefinition>}
* @property {Object} customer_name - 申请人姓名
* @property {Object} gender - 性别
* @property {Object} birthday - 出生日期
* @property {Object} smoker - 是否吸烟
* @property {Object} coverage - 保额
* @property {Object} payment_period - 缴费年期
* @property {Object} withdrawal_enabled - 是否启用提取
* @property {Object} withdrawal_mode - 提取模式
* @property {Object} withdrawal_start_age - 开始提取年龄
* @property {Object} withdrawal_period - 提取年期
* @property {Object} withdrawal_method - 提取方式
* @property {Object} annual_withdrawal_amount - 年提取金额
* @property {Object} annual_increase_percentage - 年递增比例
* @property {Object} total_amount - 总保费
*/
export const PLAN_FIELD_DEFINITIONS = {
/**
* 申请人姓名
*/
customer_name: {
label: '申请人',
type: FIELD_TYPES.TEXT,
required: true,
api_field: 'customer_name',
placeholder: '请输入申请人姓名',
component: 'PlanFieldName',
group: FIELD_GROUPS.BASIC,
validation: {
required: (value) => value?.trim()?.length >= 2
}
},
/**
* 性别
*/
gender: {
label: '性别',
type: FIELD_TYPES.RADIO,
required: true,
api_field: 'customer_gender',
component: 'PlanFieldRadio',
group: FIELD_GROUPS.BASIC,
options: [
{ label: '男', value: 'male' },
{ label: '女', value: 'female' }
],
default: 'male'
},
/**
* 出生日期
*/
birthday: {
label: '出生年月日',
type: FIELD_TYPES.DATE,
required: true,
api_field: 'customer_birthday',
component: 'PlanFieldDatePicker',
group: FIELD_GROUPS.BASIC,
placeholder: '请选择出生年月日'
},
/**
* 是否吸烟
*/
smoker: {
label: '是否吸烟',
type: FIELD_TYPES.RADIO,
required: true,
api_field: 'smoking_status',
component: 'PlanFieldRadio',
group: FIELD_GROUPS.BASIC,
options: [
{ label: '是', value: 'yes' },
{ label: '否', value: 'no' }
],
default: 'no'
},
/**
* 保额(年缴)
*/
coverage: {
label: '保额',
type: FIELD_TYPES.AMOUNT,
required: true,
api_field: 'annual_premium',
transform: TRANSFORM_TYPES.FEN_TO_YUAN,
component: 'PlanFieldAmount',
group: FIELD_GROUPS.COVERAGE,
placeholder: '请输入保额',
validation: {
required: (value) => value > 0,
min: (value, config) => value >= (config?.min_coverage || 1000),
max: (value, config) => value <= (config?.max_coverage || 10000000)
}
},
/**
* 缴费年期
*/
payment_period: {
label: '缴费年期',
type: FIELD_TYPES.SELECT,
required: true,
api_field: 'payment_years',
component: 'PlanFieldSelect',
group: FIELD_GROUPS.COVERAGE,
options_from: 'payment_periods', // 从模板配置获取选项
placeholder: '请选择缴费年期'
},
/**
* 是否启用提取
*/
withdrawal_enabled: {
label: '启用提取计划',
type: FIELD_TYPES.RADIO,
required: false,
api_field: 'allow_reduce_amount',
component: 'PlanFieldRadio',
group: FIELD_GROUPS.WITHDRAWAL,
options: [
{ label: '是', value: true },
{ label: '否', value: false }
],
default: false,
affects: ['withdrawal_mode', 'withdrawal_start_age', 'withdrawal_period', 'withdrawal_method', 'annual_withdrawal_amount']
},
/**
* 提取模式
*/
withdrawal_mode: {
label: '提取模式',
type: FIELD_TYPES.SELECT,
required: false,
api_field: 'withdrawal_option',
component: 'PlanFieldSelect',
group: FIELD_GROUPS.WITHDRAWAL,
options_from: 'withdrawal_plan.withdrawal_modes',
depends_on: 'withdrawal_enabled',
show_when: { withdrawal_enabled: true }
},
/**
* 开始提取年龄
*/
withdrawal_start_age: {
label: '开始提取年龄',
type: FIELD_TYPES.NUMBER,
required: false,
api_field: 'withdrawal_start_age',
component: 'PlanFieldAgePicker',
group: FIELD_GROUPS.WITHDRAWAL,
depends_on: 'withdrawal_enabled',
show_when: { withdrawal_enabled: true },
default_from: 'age_range.min'
},
/**
* 提取年期
*/
withdrawal_period: {
label: '提取年期',
type: FIELD_TYPES.SELECT,
required: false,
api_field: 'withdrawal_period',
component: 'PlanFieldSelect',
group: FIELD_GROUPS.WITHDRAWAL,
options_from: 'withdrawal_plan.withdrawal_periods',
depends_on: 'withdrawal_enabled',
show_when: { withdrawal_enabled: true }
},
/**
* 提取方式
*/
withdrawal_method: {
label: '提取方式',
type: FIELD_TYPES.SELECT,
required: false,
api_field: 'withdrawal_method',
component: 'PlanFieldSelect',
group: FIELD_GROUPS.WITHDRAWAL,
options: ['现金', '抵缴保费'],
depends_on: 'withdrawal_enabled',
show_when: { withdrawal_enabled: true }
},
/**
* 年提取金额
*/
annual_withdrawal_amount: {
label: '年提取金额',
type: FIELD_TYPES.AMOUNT,
required: false,
api_field: 'annual_withdrawal_amount',
transform: TRANSFORM_TYPES.FEN_TO_YUAN,
component: 'PlanFieldAmount',
group: FIELD_GROUPS.WITHDRAWAL,
depends_on: 'withdrawal_enabled',
show_when: { withdrawal_enabled: true },
placeholder: '请输入年提取金额'
},
/**
* 年递增比例
*/
annual_increase_percentage: {
label: '年递增比例',
type: FIELD_TYPES.PERCENTAGE,
required: false,
api_field: 'annual_increase_percentage',
transform: TRANSFORM_TYPES.NONE,
component: 'PlanFieldAmount',
group: FIELD_GROUPS.WITHDRAWAL,
validation: {
range: (value) => {
const num = parseFloat(value)
return !Number.isNaN(num) && num >= 0 && num <= 100
}
}
},
/**
* 总保费
*/
total_amount: {
label: '总保费',
type: FIELD_TYPES.AMOUNT,
required: false,
api_field: 'total_premium',
transform: TRANSFORM_TYPES.FEN_TO_YUAN,
component: 'PlanFieldAmount',
group: FIELD_GROUPS.COVERAGE,
placeholder: '请输入总保费'
}
}
/**
* 获取字段定义
* @param {string} fieldKey - 字段键名
* @returns {FieldDefinition|null} 字段定义
*/
export function getFieldDefinition(fieldKey) {
return PLAN_FIELD_DEFINITIONS[fieldKey] || null
}
/**
* 获取字段对应的所有依赖字段
* @param {string} fieldKey - 字段键名
* @returns {string[]} 依赖字段的键名数组
*/
export function getFieldDependencies(fieldKey) {
const definition = getFieldDefinition(fieldKey)
return definition?.affects || []
}
/**
* 获取字段的 API 字段名
* @param {string} fieldKey - 字段键名
* @returns {string} API 字段名
*/
export function getFieldApiField(fieldKey) {
const definition = getFieldDefinition(fieldKey)
return definition?.api_field || fieldKey
}
/**
* 检查字段是否需要值转换
* @param {string} fieldKey - 字段键名
* @returns {boolean} 是否需要转换
*/
export function fieldNeedsTransform(fieldKey) {
const definition = getFieldDefinition(fieldKey)
return definition?.transform && definition.transform !== TRANSFORM_TYPES.NONE
}
/**
* 根据分组获取字段列表
*
* @param {string} group - 分组标识(FIELD_GROUPS)
* @returns {Object[]} 字段定义映射
*
* @example
* getFieldsByGroup(FIELD_GROUPS.BASIC) // { customer_name: {...}, gender: {...}, birthday: {...} }
*/
export function getFieldsByGroup(group) {
const result = {}
for (const [key, definition] of Object.entries(PLAN_FIELD_DEFINITIONS)) {
if (definition.group === group) {
result[key] = definition
}
}
return result
}
/**
* 字段定义类型
* @typedef {Object} FieldDefinition
* @property {string} label - 字段显示名称
* @property {string} type - 字段类型(见 FIELD_TYPES)
* @property {boolean} required - 是否必填
* @property {string} api_field - API 字段名
* @property {string} [component] - 对应组件名
* @property {string} [placeholder] - 占位符文本
* @property {Array} [options] - 选项列表(select/radio 类型)
* @property {string} [options_from] - 选项来源(从模板配置获取)
* @property {*} [default] - 默认值
* @property {string} [transform] - 值转换类型(见 TRANSFORM_TYPES)
* @property {Object} [validation] - 验证规则
* @property {Function} [validation.required] - 必填验证函数
* @property {string[]} [affects] - 影响的字段列表
* @property {string} [depends_on] - 依赖的字段
* @property {Object} [show_when] - 显示条件
* @property {string} [default_from] - 默认值来源(从其他字段获取)
*/