import React, { ReactNode } from "react"; import { Button, FormInstance, Space, Card } from "antd"; import { DtmplBaseProps, DtmplConfig, DtmplData, } from "../tmpl/interface"; import DtmplForm from "../form/dtmpl-form"; import HCDataSource from "../tmpl/hc-data-source"; import Units from "../units"; import HcserviceV3 from "../tmpl/hcservice-v3"; import Action from "../controls/action"; import { LocaleContext } from "../locale/LocaleProvider"; import TmplConfigAnalysis from "../tmpl/tmpl-config-analysis"; import { relValueFieldExtControlType } from "../controls/entry-control"; // 缓存 getSubDtmplData 的请求结果(防止多次请求) const subDtmplDataCache: Map = new Map(); interface DtmplEditCardProps extends DtmplBaseProps { onOk?: (code: string) => void; style?: {}; title?: ReactNode; isReset?: boolean; // 是否重置表单 bodyStyle?: {}; headerStyle?: {}; isCopy?: boolean; } interface DtmplEditCardState { id?: string; queryingCode?: string; dtmplConfig?: DtmplConfig; loading?: boolean; serial?: number; dtmplData?: DtmplData; valueFieldMap?: { [key: string]: any }; } export default class DtmplEditCard extends React.PureComponent< DtmplEditCardProps, DtmplEditCardState > { formRef = React.createRef(); autoSaveTimer = -1000000000000000; autoSaveInterval = undefined; static contextType = LocaleContext; context: React.ContextType; onClickBody = (e) => { //console.log("点击了我" + this.autoSaveTimer); this.autoSaveTimer = 0; }; static defaultProps = { showConfirm: false, okText: "确定", bodyStyle: {}, headerStyle: {}, isReset: true, }; state = { id: "dtmplEditCard" + Units.getRandomNum(6), queryingCode: undefined, dtmplConfig: undefined, loading: true, serial: 1, dtmplData: undefined, valueFieldMap: {}, isCopy: false }; async componentDidMount() { await this.loadConfigData(); } async componentDidUpdate(prevProps) { const { dtmplConfig, dtmplData, queryingCode } = this.state; if (prevProps && prevProps != this.props) { let code = this.props.code ? this.props.code : queryingCode; if (prevProps.sourceId != this.props.sourceId || dtmplConfig == null || (!this.props.code && prevProps.code)) { await this.loadConfigData(); } else if (prevProps.sourceId != this.props.sourceId || queryingCode != code || !dtmplData) { //防止反复请求 code if (code) { await this.loadCodeData(dtmplConfig, code); } else { await this.loadData(dtmplConfig); } } } } componentWillUnmount(): void { if (this.autoSaveInterval) { clearInterval(this.autoSaveInterval); } } loadConfigData = async () => { const { sourceId, code, serverKey, addTmplId, isCopy } = this.props; this.setState({ loading: true, }); let dtmplConfig: DtmplConfig = undefined; if (code) { dtmplConfig = await HCDataSource.requestDtmplConfig( serverKey, sourceId, code ); } else { dtmplConfig = await HCDataSource.requestAddDtmplConfig( serverKey, sourceId, addTmplId ); } this.handleRelValueField(dtmplConfig); if (dtmplConfig.entity) { let dtmplData = dtmplConfig.entity; dtmplConfig.entity = undefined; if (isCopy) { this.setState({ dtmplData: { ...dtmplData, code: undefined }, dtmplConfig, loading: false }); } else { this.setState({ queryingCode: code, dtmplData, dtmplConfig, loading: false }); } setTimeout(() => this.handleBuildInFuncFields(dtmplConfig), 300); } else { await this.loadData(dtmplConfig); this.setState({ dtmplConfig, loading: false }); } }; loadData = async (dtmplConfig: DtmplConfig) => { const { sourceId, serverKey, code: propsCode, codeSource } = this.props; let code = propsCode; if (!code && codeSource) { code = await HcserviceV3.queryCode(sourceId, codeSource, serverKey); } await this.loadCodeData(dtmplConfig, code) }; reloadData = async (param?: { code: string }) => { const { queryingCode, dtmplConfig } = this.state; const { sourceId, serverKey, code: propsCode, codeSource } = this.props; let code = param?.code || propsCode || queryingCode; if (!code && codeSource) { code = await HcserviceV3.queryCode(sourceId, codeSource, serverKey); } await this.loadCodeData(dtmplConfig, code); }; loadCodeData = async (dtmplConfig: DtmplConfig, code: string) => { const { sourceId, defaultDtmplData, serverKey, isCopy } = this.props; //重设定时器 if (dtmplConfig?.autoSaveInterval && dtmplConfig?.autoSaveInterval > 0) { if (this.autoSaveInterval) { clearInterval(this.autoSaveInterval); } this.autoSaveInterval = setInterval(() => { if (this.autoSaveTimer >= dtmplConfig.autoSaveInterval) { // console.log('执行自动保存'); this.autoSaveTimer = -1000000000000000; this.doSubmit(undefined); } else { this.autoSaveTimer = this.autoSaveTimer + 1; } }, 1000); } if (code) { this.setState({ loading: true, queryingCode: code, dtmplData: undefined, }); const dtmplData_: DtmplData = await HcserviceV3.getDtmplData( dtmplConfig, serverKey, sourceId, code ); if ((!this.state.queryingCode || code == dtmplData_.code) && (!dtmplData_.sourceId || (this.props.sourceId == dtmplData_.sourceId))) { if (isCopy) { this.setState({ dtmplData: { ...dtmplData_, code: undefined }, id: undefined }); } else { this.setState({ dtmplData: dtmplData_, id: code }); } } this.setState({ loading: false, }); } else if (defaultDtmplData) { this.setState({ dtmplData: { ...defaultDtmplData }, loading: false, }); } else { this.setState({ dtmplData: { code: undefined, title: undefined, fieldMap: {}, arrayMap: {}, }, loading: false, }); } setTimeout(() => this.onValuesChange(), 300); }; // 表单关联项secondCriterion处理start getSubDtmplData = async (valueField, code) => { const { serverKey } = this.props; let dtmplData = null; const cacheKey = `${valueField.extRefPageId || valueField.id}_${code.split("@R@")[0]}`; //缓存Key const cacheData = subDtmplDataCache.get(cacheKey); if (cacheData !== undefined) { // 存在缓存使用缓存,减少请求次数 dtmplData = cacheData; } else { if (valueField.extRefPageId) { // 存在extRefPageId(扩展关联页面),则优先使用extRefPageId(例如:枚举值无指向页面,使用扩展关联页面) const { entities } = await HcserviceV3.requestLtmplQueryTop(null, valueField.extRefPageId, { top: 1, secondCriterion: code.split("@R@")[0] }) || {}; dtmplData = entities?.[0]; } else { // 查询字段指向页面的数据 dtmplData = await HcserviceV3.requestDtmplData(serverKey, valueField.id, code.split("@R@")[0], null); } subDtmplDataCache.set(cacheKey, dtmplData || false); } if (!dtmplData) return null; const subField = valueField.subRelValueField; const subData = dtmplData.fieldMap[subField.id]; if (subField.subRelValueField && subData) { // 存在下级关联项 return this.getSubDtmplData(subField, subData); } return subData; } getSecondCriteria = async (field) => { const { parentFormInstance } = this.props; const { relValueField, relValueField3, baseCriteria = {}, relMinValueField, relMaxValueField } = field; const temForm = parentFormInstance || this.formRef.current; if (relValueField && temForm) { // 设置默认值或查询次条件 let relValueData = temForm.getFieldValue(relValueField.id); if (relValueField.subRelValueField && relValueData) { relValueData = await this.getSubDtmplData(relValueField, relValueData); } if (!relValueFieldExtControlType.includes(field.extControlType)) { field.defaultValue = relValueData; // 非关系型字段填充默认值 } else { field.baseCriteria = { ...(baseCriteria || {}), "secondCriterion": relValueData }; } field.configChanged = true; } if (relValueField3 && temForm) { // 设置查询三条件 let relValueData = temForm.getFieldValue(relValueField3.id); if (relValueField3.subRelValueField && relValueData) { relValueData = await this.getSubDtmplData(relValueField3, relValueData); } field.baseCriteria = { ...(field.baseCriteria || {}), "thirdCriterion": relValueData }; field.configChanged = true; } if (relMinValueField && temForm) { // 设置最小值 let relValueData = temForm.getFieldValue(relMinValueField.id); if (relMinValueField.subRelValueField && relValueData) { relValueData = await this.getSubDtmplData(relMinValueField, relValueData); } field.min = relValueData; field.configChanged = true; } if (relMaxValueField && temForm) { // 设置最大值 let relValueData = temForm.getFieldValue(relMaxValueField.id); if (relMaxValueField.subRelValueField && relValueData) { relValueData = await this.getSubDtmplData(relMaxValueField, relValueData); } field.max = relValueData; field.configChanged = true; } return field; } // 表单关联项secondCriterion处理end /** * 处理表单关联项 * 匹配关联项的字段,实现值更新关联字段值变化 */ handleRelValueField = (dtmplConfig: DtmplConfig) => { const { groups } = dtmplConfig; const valueFieldMap = groups.reduce((total, item) => { if (item.fields) { item.fields.forEach(field => { if (field.relValueField) { const temArr = total[field.relValueField.id] || []; const isSameGroup = item.fields.some(f => f.id === field.relValueField.id); const temField = isSameGroup ? field : item; const isItem = temArr.find(r => r.id === temField.id); if (!isItem) { total[field.relValueField.id] = [...temArr, temField]; } } if (field.relValueField3) { const temArr = total[field.relValueField3.id] || []; const isSameGroup = item.fields.some(f => f.id === field.relValueField3.id); const temField = isSameGroup ? field : item; const isItem = temArr.find(r => r.id === temField.id); if (!isItem) { total[field.relValueField3.id] = [...temArr, temField]; } } }) } return total; }, {}); this.setState({ valueFieldMap }); }; //关联取值:根据 parentFormInstance / this.formRef 完善 dtmplConfig handleBuildInFuncFields = async (tDtmplConfig?: DtmplConfig) => { const dtmplConfig = tDtmplConfig || this.state.dtmplConfig; if (dtmplConfig) { if (!dtmplConfig.buildInFuncFields) { dtmplConfig.buildInFuncFields = TmplConfigAnalysis.buildInFuncFields(dtmplConfig); } dtmplConfig.buildInFuncFields = await Promise.all(dtmplConfig.buildInFuncFields.map(field => this.getSecondCriteria(field))); } } onValuesChange = (changedValues?: any, allValues?: any) => { const { dtmplData, valueFieldMap, dtmplConfig } = this.state; this.handleBuildInFuncFields(); if (dtmplConfig) { const { groups } = dtmplConfig; const changeValKey = Object.keys(changedValues || {})[0]; const clearValues = {}; // 清空关联字段值 if (changeValKey && valueFieldMap[changeValKey]) { // 处理关联字段值变化, 表单值变化,处理关联字段值为空 const temArrayMap = valueFieldMap[changeValKey].reduce((total, item) => { if (groups.some(g => g.id === item.id)) { total[item.id] = []; // 清空关联字段组 } else { clearValues[item.id] = undefined; } return total; }, {}); this.setState({ dtmplData: { ...dtmplData, fieldMap: { ...allValues, ...clearValues }, arrayMap: { ...dtmplData.arrayMap, ...temArrayMap } } }); } } this.setState({ serial: this.state.serial + 1 }); }; // doSubmit = async (actionId?: number, codes?: string[], params?: object) => { // await this.doSubmit1(actionId, codes, params); // //await this.reloadData(); // } doSubmit = async (actionId?: number, codes?: string[], params?: object) => { const { mainCode } = this.props; //if (actionId) { this.formRef.current.setFieldsValue({ actionId: actionId }); // } if (mainCode) { this.formRef.current.setFieldsValue({ mainCode: mainCode }); } if (params) { this.formRef.current.setFieldsValue(params); } this.formRef.current.submit(); //this.reloadData(); }; getFooter = () => { const { serverKey } = this.props; const { dtmplData, serial, dtmplConfig } = this.state; const { translate } = this.context; const footerButtons = []; footerButtons.unshift( ); //处理action if (dtmplConfig) { if (dtmplConfig.buttons?.includes("dtmplSave")) { footerButtons.unshift( ); } dtmplConfig.actions.forEach((ac, index) => { footerButtons.unshift( ); }); } return footerButtons; }; onFinish = async (code: any) => { const { isReset } = this.props; this.setState({ loading: false }); if (code) { this.props.onOk(code); if (isReset) { await this.reloadData({ code }); } } } render() { const { sourceId, mainCode, style, maxColsOnRow, title, serverKey, bodyStyle, headerStyle } = this.props; const { dtmplConfig, id, dtmplData } = this.state; return ( <> {this.getFooter()}} className="dtmpl-edit-card" > ); } }