"""字段模型构建器 参考前端 FieLdsModel 和 ConfigModel 结构。 将简洁的字段定义转换为完整的 JNPF 字段模型。 """ import time from typing import Any, Dict, List, Optional # jnpfKey → (tagIcon, tag, placeholder 模板) _JNPF_KEY_MAP = { "input": ("icon-ym icon-ym-generator-input", "JnpfInput", "请输入{label}"), "inputNumber": ("icon-ym icon-ym-generator-number", "JnpfInputNumber", "请输入{label}"), "textarea": ("icon-ym icon-ym-generator-input", "JnpfInput", "请输入{label}"), "select": ("icon-ym icon-ym-generator-select", "JnpfSelect", "请选择{label}"), "datePicker": ("icon-ym icon-ym-generator-date", "JnpfDatePicker", "请选择{label}"), "switch": ("icon-ym icon-ym-generator-switch", "JnpfSwitch", ""), "checkbox": ("icon-ym icon-ym-generator-checkbox", "JnpfCheckbox", ""), "radio": ("icon-ym icon-ym-generator-radio", "JnpfRadio", "请选择{label}"), "numInput": ("icon-ym icon-ym-generator-number", "JnpfInputNumber", "请输入{label}"), "comSelect": ("icon-ym icon-ym-generator-select", "JnpfComSelect", "请选择{label}"), "cascader": ("icon-ym icon-ym-generator-cascader", "JnpfCascader", "请选择{label}"), "treeSelect": ("icon-ym icon-ym-generator-tree", "JnpfTreeSelect", "请选择{label}"), "address": ("icon-ym icon-ym-generator-address", "JnpfAddress", "请选择{label}"), "uploadImg": ("icon-ym icon-ym-generator-upload", "JnpfUploadImg", ""), "uploadFile": ("icon-ym icon-ym-generator-upload", "JnpfUploadFile", ""), "rate": ("icon-ym icon-ym-generator-rate", "JnpfRate", ""), "colorPicker": ("icon-ym icon-ym-generator-color", "JnpfColorPicker", ""), "popupTable": ("icon-ym icon-ym-generator-popup", "JnpfPopupTable", ""), "popupAttr": ("icon-ym icon-ym-generator-popup", "JnpfPopupAttr", ""), "relationForm": ("icon-ym icon-ym-generator-relation", "JnpfRelationForm", ""), "relationFlow": ("icon-ym icon-ym-generator-relation", "JnpfRelationFlow", ""), "sign": ("icon-ym icon-ym-sign", "JnpfSign", ""), "timePicker": ("icon-ym icon-ym-generator-date", "JnpfTime", "请选择{label}"), } def build_field_model( field_name: str, field_vmodel: str, jnpf_key: str = "input", required: bool = False, table_name: str = "base_table", render_key: int = 0, is_sub_table: bool = False, relation_table: str = "", parent_vmodel: str = "", options: List[Dict[str, Any]] = None, dictionary_type: str = "", ) -> Dict[str, Any]: """构建单个字段的 FieLdsModel Args: field_name: 字段中文名,如 "销售商名称" field_vmodel: 数据库字段名,如 "f_full_name" jnpf_key: 控件类型,支持 25+ 种类型,"table" 表示子表控件 required: 是否必填 table_name: 所属表名 render_key: 渲染键(自动生成) is_sub_table: 是否为子表字段(table 控件的 children) relation_table: 子表所属表名(子表字段必填) parent_vmodel: 子表控件的 vModel(子表字段必填) options: 下拉选项列表,仅 select/radio/comSelect 支持。 格式必须为 [{fullName: "显示文本", id: "值"}, ...], 不是 [{label: "显示文本", value: "值"}, ...]。 Returns: 完整的 FieLdsModel 字典 """ # JNPF 系统保留字段验证(只验证 f_version,大小写敏感) if field_vmodel.lower() == "f_version": raise ValueError(f"字段名 '{field_vmodel}' 是 JNPF 系统保留字段,禁止使用。") # 提示字段命名规范(f_xx_xx 格式) import re if not re.match(r'^f_[a-z][a-z0-9]*(_[a-z0-9]+)*$', field_vmodel): print(f"⚠️ 提示:字段名建议使用 f_xx_xx 格式(如 f_user_name),当前:{field_vmodel}") tag_icon, tag, placeholder_tpl = _JNPF_KEY_MAP.get(jnpf_key, _JNPF_KEY_MAP["input"]) model: Dict[str, Any] = { "clearable": True, "suffixIcon": "", "addonAfter": "", "__config__": { "formId": 101, "visibility": ["pc", "app"], "jnpfKey": jnpf_key, "noShow": False, "tipLabel": "", "dragDisabled": False, "className": [], "label": field_name, "trigger": "blur", "showLabel": True, "required": required, "tableName": table_name, "renderKey": render_key, "layout": "colFormItem", "tagIcon": tag_icon, "tag": tag, "regList": [], "dictionaryType": dictionary_type, # 数据字典类型ID }, "readonly": False, "__vModel__": field_vmodel, "showPassword": False, "style": {"width": "100%"}, "disabled": False, "placeholder": placeholder_tpl.format(label=field_name) if placeholder_tpl else "", "prefixIcon": "", "addonBefore": "", "on": {"change": "", "blur": ""}, # columnOptions 额外字段 "id": field_vmodel, "fullName": field_name, "fullNameI18nCode": [""], } # 子表字段特有属性 if is_sub_table: model["__config__"]["isSubTable"] = True model["__config__"]["relationTable"] = relation_table model["__config__"]["parentVModel"] = parent_vmodel model["__config__"]["span"] = 24 # 控件类型特有属性 if jnpf_key == "switch": model["activeValue"] = 1 model["inactiveValue"] = 0 elif jnpf_key == "inputNumber" or jnpf_key == "numInput": model["step"] = 1 model["controls"] = True model["min"] = None model["max"] = None elif jnpf_key == "rate": model["max"] = 5 model["allowHalf"] = False model["showText"] = False elif jnpf_key == "textarea": model["rows"] = 4 model["maxlength"] = None elif jnpf_key == "datePicker": model["format"] = "yyyy-MM-dd" model["valueFormat"] = "yyyy-MM-dd" elif jnpf_key == "timePicker": model["format"] = "HH:mm:ss" model["valueFormat"] = "HH:mm:ss" elif jnpf_key in ("select", "radio", "checkbox", "comSelect"): model["multiple"] = False # 使用字段定义中传入的 options # 注意:options 必须使用 {fullName, id} 格式,不是 {label, value} # 前端通过 props.label="fullName" 和 props.value="id" 读取选项 model["options"] = options if options else [] model["props"] = {"label": "fullName", "value": "id", "disabled": "disabled", "children": ""} # 如果有数据字典类型,设置关联 if dictionary_type: model["__config__"]["dictionaryType"] = dictionary_type elif jnpf_key == "relationForm": model.update({ "modelId": "", "relationField": "", "hasPage": False, "pageSize": 20, "queryType": 0, "propsValue": None, "extraOptions": [], "columnOptions": [], "clearable": True, "popupType": "dialog", "popupTitle": "选择数据", "popupWidth": "800px", "filterable": False, "field": field_vmodel, }) return model def build_table_field_model( label: str, vmodel: str, child_table_name: str, children: List[Dict[str, Any]], render_key: int = 0, main_table_name: str = "", show_summary: int = 0, summary_field: Optional[List[str]] = None, ) -> Dict[str, Any]: """构建 table 控件(子表)的 FieLdsModel Args: label: 子表标签,如 "添加明细" vmodel: 子表控件 vModel,如 "tableField107" child_table_name: 子表数据库表名,如 "base_order_item" children: 子表字段模型列表(通过 build_field_model 生成,需设 is_sub_table=True) render_key: 渲染键 main_table_name: 主表表名(用于 children 的 tableName) show_summary: 是否显示合计行 (0=否, 1=是) summary_field: 合计字段列表(vModel 列表) Returns: 完整的 table 控件 FieLdsModel 字典 """ return { "showSummary": show_summary, "actionText": "添加", "tableConf": {}, "defaultValue": [], "footerBtnsList": [ {"btnIcon": "icon-ym icon-ym-btn-add", "show": True, "label": "添加", "btnType": "primary", "value": "add"}, {"btnIcon": "icon-ym icon-ym-btn-clearn", "showConfirm": True, "show": True, "label": "批量删除", "btnType": "danger", "value": "batchRemove"}, ], "addTableConf": { "popupType": "dialog", "hasPage": True, "popupTitle": "选择数据", "pageSize": 20, "columnOptions": [], "interfaceId": "", "interfaceName": "", "relationOptions": [], "templateJson": [], "popupWidth": "800px", }, "columnBtnsList": [ {"btnIcon": "icon-ym icon-ym-btn-edit", "show": True, "label": "复制", "btnType": "primary", "value": "copy"}, {"btnIcon": "icon-ym icon-ym-btn-clearn", "showConfirm": True, "show": True, "label": "删除", "btnType": "danger", "value": "remove"}, ], "summaryField": summary_field or [], "showDeleteBtn": True, "addType": 0, "__config__": { "formId": 107, "visibility": ["pc", "app"], "jnpfKey": "table", "defaultValue": [], "noShow": False, "tipLabel": "", "dragDisabled": False, "className": [], "label": label, "type": "table", "showLabel": False, "tableName": child_table_name, "renderKey": render_key, "layout": "rowFormItem", "tagIcon": "icon-ym icon-ym-generator-table", "children": children, "showTitle": True, "complexHeaderList": [], "tag": "JnpfInputTable", "componentName": f"table{render_key}", "span": 24, }, "showAddBtn": True, "__vModel__": vmodel, "disabled": False, "layoutType": "table", } def build_field_models( fields: List[Dict[str, Any]], table_name: str = "base_table", base_render_key: int = 0, ) -> List[Dict[str, Any]]: """批量构建字段模型 Args: fields: 字段定义列表,每个元素参考 FieldDef.to_dict() 格式 table_name: 表名 base_render_key: 基础渲染键 Returns: 字段模型列表 """ models = [] for i, f in enumerate(fields): model = build_field_model( field_name=f.get("fieldName", ""), field_vmodel=f.get("field", ""), jnpf_key=f.get("jnpfKey", "input"), required=f.get("required", False), table_name=table_name, render_key=base_render_key + i, options=f.get("options"), dictionary_type=f.get("dictionaryType", ""), ) models.append(model) return models