Commit 5be43907 authored by 田翔's avatar 田翔

关联表单计算树形

parent d818d58b
{ {
"name": "panda-xform", "name": "panda-xform",
"version": "2.0.9", "version": "2.1.0",
"description": "2.0.9: 命名规范问题", "description": "2.1.0: 关联表单计算树形",
"keywords": [ "keywords": [
"panda-xform" "panda-xform"
], ],
......
import React, { useMemo, useContext, forwardRef, useImperativeHandle, createContext } from 'react' import React, { useMemo, useContext, forwardRef, useImperativeHandle, createContext, useState } from 'react'
import FormRender, { useForm } from 'form-render' import FormRender, { useForm } from 'form-render'
import { ConfigProvider } from 'antd' import { ConfigProvider } from 'antd'
import widgets from '../widgets' import widgets from '../widgets'
...@@ -19,11 +19,12 @@ const XRender = (props, ref) => { ...@@ -19,11 +19,12 @@ const XRender = (props, ref) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext) const { getPrefixCls } = useContext(ConfigProvider.ConfigContext)
const prefixCls = getPrefixCls('pandaXform') const prefixCls = getPrefixCls('pandaXform')
const pandaXform = getPrefixCls() const pandaXform = getPrefixCls()
const [startTime, setStartTime] = useState(new Date().getTime())
const form = useForm() const form = useForm()
const watch = useMemo(() => { const watch = useMemo(() => {
return getWatch(schema, form) return getWatch(schema, form, startTime)
}, [schema]) }, [schema, form, startTime])
const getValues = async () => { const getValues = async () => {
let { data, errors } = await form.submit() let { data, errors } = await form.submit()
......
...@@ -48,6 +48,7 @@ export const jsonToValues = (data, paths) => { ...@@ -48,6 +48,7 @@ export const jsonToValues = (data, paths) => {
} }
const getRules = (json) => { const getRules = (json) => {
let AutoCalculate = []
let parent = json?.properties let parent = json?.properties
if (isObject(parent)) { if (isObject(parent)) {
for (let v in parent) { for (let v in parent) {
...@@ -55,17 +56,17 @@ const getRules = (json) => { ...@@ -55,17 +56,17 @@ const getRules = (json) => {
if (isObject(child)) { if (isObject(child)) {
for (let s in child) { for (let s in child) {
if (child[s].widget === 'AutoCalculate') { if (child[s].widget === 'AutoCalculate') {
return { AutoCalculate.push({
filedName: s, filedName: s,
...child[s], ...child[s],
targetPath: `${v}.${s}`, targetPath: `${v}.${s}`,
} })
} }
} }
} }
} }
} }
return {} return AutoCalculate
} }
const getPath = (json, filed) => { const getPath = (json, filed) => {
...@@ -85,10 +86,22 @@ const getPath = (json, filed) => { ...@@ -85,10 +86,22 @@ const getPath = (json, filed) => {
return '' return ''
} }
const getFieldName = (configs, tableName) => {
let filedName = []
configs.forEach(v => {
if (v['台账名称'] === tableName) {
filedName.push(v.fieldName)
}
})
return filedName
}
const automaticComputation = async (params) => { const automaticComputation = async (params) => {
if ((new Date().getTime() - params.startTime) < 1000) return
let param = { let param = {
tableName: params.tableName, tableName: params.tableName,
filedFormulas: params.filedFormulas, filedFormulas: params.filedFormulas,
relationForm: params.relationForm,
values: jsonToValues(params.form.getValues(), params.paths).formValue values: jsonToValues(params.form.getValues(), params.paths).formValue
} }
const { code, data, msg } = await formAutomaticComputation(param) const { code, data, msg } = await formAutomaticComputation(param)
...@@ -108,35 +121,56 @@ const automaticComputation = async (params) => { ...@@ -108,35 +121,56 @@ const automaticComputation = async (params) => {
const setValue = debounce(automaticComputation) const setValue = debounce(automaticComputation)
export const getWatch = (schema, form) => { export const getWatch = (schema, form, startTime) => {
let watch = {} let watch = {}
let { rules, calculateRule, filedName } = getRules(schema) let { relationForm } = form?.getValues()
let paths = [] const configs = relationForm?.configs || []
if (Array.isArray(rules)) { if (Array.isArray(configs)) {
let item = rules.find(v => v.tableName === schema.tableName) configs.forEach(v => {
if (item) { v['台账名称'] = v.accountName
if (Array.isArray(item.fields)) { v['映射字段'] = v.mappedField
item.fields.forEach(v => { })
let path = getPath(schema, v) }
if (path) { const AutoCalculate = getRules(schema)
paths.push(path) if (Array.isArray(AutoCalculate)) {
AutoCalculate.forEach(item => {
const { filedName, rules, calculateRule } = item
let paths = []
if (Array.isArray(rules)) {
rules.forEach(s => {
let item = { tableName: null, fields: [] }
if (schema.tableName === s.tableName) {
item = s
} else {
item = { fields: getFieldName(configs, s.tableName) }
}
if (item) {
if (Array.isArray(item.fields)) {
item.fields.forEach(v => {
let path = getPath(schema, v)
if (path) {
paths.push(path)
}
})
}
} }
}) })
} }
} if (Array.isArray(paths)) {
} paths.forEach(v => {
watch[v] = () => {
if (Array.isArray(paths)) { let params = {
paths.forEach(v => { tableName: schema.tableName,
watch[v] = () => { filedFormulas: [{ fieldName: filedName, formula: calculateRule }],
let params = { form: form,
tableName: schema.tableName, paths: paths,
filedFormulas: [{ fieldName: filedName, formula: calculateRule }], relationForm,
form: form, schema: schema,
paths: paths, startTime
schema: schema }
} setValue(params)
setValue(params) }
})
} }
}) })
} }
......
...@@ -291,7 +291,6 @@ const RelationForm = (props) => { ...@@ -291,7 +291,6 @@ const RelationForm = (props) => {
const onOk = async () => { const onOk = async () => {
if (addons) { if (addons) {
console.log('addons', addons)
const { formValue, errors } = await baseFormRef.current.getValues() const { formValue, errors } = await baseFormRef.current.getValues()
if (errors.length) { if (errors.length) {
return message.error('请按照提示完善表单内容') return message.error('请按照提示完善表单内容')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment