|
|
@@ -0,0 +1,396 @@
|
|
|
+import React from 'react'
|
|
|
+import {
|
|
|
+ isObjEmpty,
|
|
|
+ isObjNull,
|
|
|
+} from '../utils/common'
|
|
|
+import FormInput from '../components/common/formNew/FormInput'
|
|
|
+import FormAdd from '../components/common/formNew/FormAdd'
|
|
|
+import { message } from 'antd'
|
|
|
+import BillModel, {
|
|
|
+ TYPE_ADD,
|
|
|
+ TYPE_TAB,
|
|
|
+ TYPE_TITLE,
|
|
|
+} from '../model/common/BillModel'
|
|
|
+import FormTitle from '../components/common/formNew/FormTitle'
|
|
|
+import BillGroupModel from '../model/common/BillGroupModel'
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by RaoMeng on 2020/11/6
|
|
|
+ * Desc: 通用表单生成组件工具类
|
|
|
+ */
|
|
|
+
|
|
|
+let mBaseUrl = window.location.origin
|
|
|
+ + (process.env.REACT_APP_ROUTER_BASE_NAME || '')
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通过json数据获取表单组件列表
|
|
|
+ * @param billGroupModelList
|
|
|
+ * @returns {[]}
|
|
|
+ */
|
|
|
+export function getFormItems (billGroupModelList) {
|
|
|
+ let formItems = []
|
|
|
+ if (!isObjEmpty(billGroupModelList)) {
|
|
|
+ //从表的起始Groupindex
|
|
|
+ let gridStartIndex = -1
|
|
|
+ for (let g = 0; g < billGroupModelList.length; g++) {
|
|
|
+ let billGroup = billGroupModelList[g]
|
|
|
+ if (!isObjNull(billGroup)) {
|
|
|
+ let showBillFields = billGroup.showBillFields
|
|
|
+ if (!isObjNull(showBillFields) &&
|
|
|
+ showBillFields.length > 0) {
|
|
|
+ //添加组标题
|
|
|
+ gridStartIndex = addGroupTitle(billGroupModelList,
|
|
|
+ g, billGroup, gridStartIndex, formItems)
|
|
|
+ //添加显示字段
|
|
|
+ for (let i = 0; i < showBillFields.length; i++) {
|
|
|
+ let billModel = showBillFields[i]
|
|
|
+ if (!isObjNull(billModel)) {
|
|
|
+ let itemViewType = getItemViewType(billModel.type)
|
|
|
+ if (billModel.renderer == 'detailAttach') {
|
|
|
+ itemViewType = 2
|
|
|
+ }
|
|
|
+ switch (itemViewType) {
|
|
|
+ case 1:
|
|
|
+ formItems.push(
|
|
|
+ <FormInput billModel={billModel} groupIndex={g}
|
|
|
+ childIndex={i}
|
|
|
+ baseUrl={mBaseUrl}
|
|
|
+ onTextChange={onTextChange.bind(this,
|
|
|
+ billGroupModelList)}
|
|
|
+ onInputClick={onInputClick.bind(this,
|
|
|
+ billGroupModelList)}
|
|
|
+ />,
|
|
|
+ )
|
|
|
+ break
|
|
|
+ case 2:
|
|
|
+ //附件
|
|
|
+ /*formItems.push(
|
|
|
+ <FormEnclosure billModel={billModel} groupIndex={g}
|
|
|
+ baseUrl={mBaseUrl}
|
|
|
+ childIndex={i}/>,
|
|
|
+ )*/
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (billGroup.isForm == false && billGroup.lastInType == true) {
|
|
|
+ //如果是最后一个从表,则添加【新增】按钮
|
|
|
+ formItems.push(
|
|
|
+ <FormAdd billModel={getAddModel(g)} groupIndex={g}
|
|
|
+ onAddClick={onAddClick.bind(this, billGroupModelList)}
|
|
|
+ />,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return formItems
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 添加明细组
|
|
|
+ * @param groupIndex
|
|
|
+ */
|
|
|
+function onAddClick (billGroupModelList, groupIndex) {
|
|
|
+ billGroupModelList[groupIndex].lastInType = false
|
|
|
+
|
|
|
+ let newBillGroup = newGridBillGroup(groupIndex,
|
|
|
+ billGroupModelList[groupIndex])
|
|
|
+ billGroupModelList.push(newBillGroup)
|
|
|
+}
|
|
|
+
|
|
|
+function newGridBillGroup (groupIndex, oldBillGroup) {
|
|
|
+ let oldGridIndex = oldBillGroup.gridIndex
|
|
|
+ let isForm = oldBillGroup.isForm
|
|
|
+
|
|
|
+ let newBillGroup = new BillGroupModel()
|
|
|
+ newBillGroup.isForm = isForm
|
|
|
+ if (isForm == true) {
|
|
|
+ newBillGroup.group = oldBillGroup.group
|
|
|
+ } else {
|
|
|
+ newBillGroup.group = `明细${oldGridIndex + 1}`
|
|
|
+ }
|
|
|
+ newBillGroup.billCaller = oldBillGroup.billCaller
|
|
|
+ newBillGroup.gridIndex = oldGridIndex + 1
|
|
|
+ newBillGroup.isDeleteAble = true
|
|
|
+ newBillGroup.lastInType = true
|
|
|
+ newBillGroup.groupIndex = groupIndex + 1
|
|
|
+
|
|
|
+ let showBillFields = oldBillGroup.showBillFields
|
|
|
+ let hideBillFields = oldBillGroup.hideBillFields
|
|
|
+
|
|
|
+ if (!isObjEmpty(showBillFields)) {
|
|
|
+ for (let i = 0; i < showBillFields.length; i++) {
|
|
|
+ let billModel = showBillFields[i]
|
|
|
+ newBillGroup.addShow(new BillModel(billModel))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isObjEmpty(hideBillFields)) {
|
|
|
+ for (let i = 0; i < hideBillFields.length; i++) {
|
|
|
+ let billModel = hideBillFields[i]
|
|
|
+ newBillGroup.addHide(new BillModel(billModel))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newBillGroup
|
|
|
+}
|
|
|
+
|
|
|
+function getAddModel (index) {
|
|
|
+ let addBillModel = new BillModel()
|
|
|
+ addBillModel.groupIndex = index
|
|
|
+ addBillModel.type = TYPE_ADD
|
|
|
+ addBillModel.caption = '添加单据'
|
|
|
+ return addBillModel
|
|
|
+}
|
|
|
+
|
|
|
+function getItemViewType (dfType) {
|
|
|
+ if (isObjEmpty(dfType)) {
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+ switch (dfType.toUpperCase()) {
|
|
|
+ case TYPE_TITLE:
|
|
|
+ return 0
|
|
|
+ case TYPE_ADD:
|
|
|
+ return 110
|
|
|
+ case TYPE_TAB:
|
|
|
+ return 111
|
|
|
+ case 'C':
|
|
|
+ case 'SF':
|
|
|
+ case 'DF':
|
|
|
+ case 'S':
|
|
|
+ case 'SS':
|
|
|
+ return 1
|
|
|
+ case 'FF':
|
|
|
+ return 2
|
|
|
+ default:
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 添加分组头部
|
|
|
+ * @param g
|
|
|
+ * @param billGroup
|
|
|
+ * @param gridStartIndex
|
|
|
+ * @param formItems
|
|
|
+ */
|
|
|
+function addGroupTitle (
|
|
|
+ billGroupModelList, g, billGroup, gridStartIndex, formItems) {
|
|
|
+ let titleBillModel = new BillModel()
|
|
|
+ titleBillModel.groupIndex = g
|
|
|
+ titleBillModel.type = TYPE_TITLE
|
|
|
+ if (billGroup.isForm == true) {
|
|
|
+ titleBillModel.caption = billGroup.group
|
|
|
+ titleBillModel.allowBlank = 'F'
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if (gridStartIndex === -1) {
|
|
|
+ gridStartIndex = g
|
|
|
+ }
|
|
|
+ titleBillModel.caption = `明细${g - gridStartIndex + 1}`
|
|
|
+ titleBillModel.allowBlank = billGroup.isDeleteAble == true
|
|
|
+ ? 'T'
|
|
|
+ : 'F'
|
|
|
+ }
|
|
|
+ if (!isObjEmpty(titleBillModel.caption)) {
|
|
|
+ formItems.push(
|
|
|
+ <FormTitle billModel={titleBillModel} groupIndex={g}
|
|
|
+ onDeleteClick={onDeleteClick.bind(this, billGroupModelList)}
|
|
|
+ />,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ return gridStartIndex
|
|
|
+}
|
|
|
+
|
|
|
+function onDeleteClick (billGroupModelList, groupIndex) {
|
|
|
+ this.deleteAlert = alert('提示', '确认删除该明细?', [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ onPress: () => {},
|
|
|
+ style: 'default',
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '确定', onPress: () => {
|
|
|
+ deleteGroup(billGroupModelList, groupIndex)
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ])
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 删除分组
|
|
|
+ * @param groupIndex
|
|
|
+ */
|
|
|
+function deleteGroup (billGroupModelList, groupIndex) {
|
|
|
+ let deleteGroup = billGroupModelList[groupIndex]
|
|
|
+ let isLastGroup = deleteGroup.lastInType
|
|
|
+ if (isLastGroup == true && groupIndex - 1 >= 0) {
|
|
|
+ let billGroup = billGroupModelList[groupIndex - 1]
|
|
|
+ if (deleteGroup.isForm == false && (billGroup.isForm == true)) {
|
|
|
+ //明细删除光了,要默认添加一个空明细表
|
|
|
+ deleteGroup.groupIndex = groupIndex - 1
|
|
|
+ deleteGroup.gridIndex = 0
|
|
|
+ let newGridGroup = newGridBillGroup(groupIndex - 1, deleteGroup)
|
|
|
+ newGridGroup.isDeleteAble = false
|
|
|
+ billGroupModelList.splice(groupIndex, 1)
|
|
|
+ billGroupModelList.push(newGridGroup)
|
|
|
+ } else {
|
|
|
+ billGroup.lastInType = true
|
|
|
+ billGroupModelList.splice(groupIndex, 1)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ billGroupModelList.splice(groupIndex, 1)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function onTextChange (
|
|
|
+ billGroupModelList, groupIndex, childIndex, value, display) {
|
|
|
+ if (!isObjNull(billGroupModelList) &&
|
|
|
+ !isObjNull(billGroupModelList[groupIndex])) {
|
|
|
+ let billGroup = billGroupModelList[groupIndex]
|
|
|
+ if (!isObjNull(billGroup.showBillFields) &&
|
|
|
+ !isObjNull(billGroup.showBillFields[childIndex])) {
|
|
|
+ billGroup.showBillFields[childIndex].value = value
|
|
|
+ if (!isObjNull(display)) {
|
|
|
+ billGroup.showBillFields[childIndex].display = display
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function onInputClick (billGroupModelList, groupIndex, childIndex) {
|
|
|
+ /*if (!isObjNull(billGroupModelList) &&
|
|
|
+ !isObjNull(billGroupModelList[groupIndex])) {
|
|
|
+ let billGroup = billGroupModelList[groupIndex]
|
|
|
+ if (!isObjNull(billGroup.showBillFields) &&
|
|
|
+ !isObjNull(billGroup.showBillFields[childIndex])) {
|
|
|
+ let billModel = billGroup.showBillFields[childIndex]
|
|
|
+ if (isObjNull(billModel)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ billModel.childIndex = childIndex
|
|
|
+ this.setState({
|
|
|
+ selectModel: billModel,
|
|
|
+ })
|
|
|
+ Toast.loading('数据请求中', 0)
|
|
|
+ let type = billModel.type
|
|
|
+ if (type === 'C') {
|
|
|
+ //单项选择
|
|
|
+ this.getComboValue(billModel)
|
|
|
+ } else if (type === 'SF' || type === 'DF') {
|
|
|
+ //DBFind选择
|
|
|
+ this.getDbfindList(billModel, billGroup)
|
|
|
+ } else if (type === 'MF') {
|
|
|
+ //多项选择
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+}
|
|
|
+
|
|
|
+export function getFormAndGrid (billGroupModelList) {
|
|
|
+ if (!isObjEmpty(billGroupModelList)) {
|
|
|
+ let formFields = [], gridGroupFields = []
|
|
|
+ for (let i = 0; i < billGroupModelList.length; i++) {
|
|
|
+ let billGroup = billGroupModelList[i]
|
|
|
+ if (isObjNull(billGroup)) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if (billGroup.isForm) {
|
|
|
+ if (!isObjEmpty(billGroup.showBillFields)) {
|
|
|
+ formFields = formFields.concat(billGroup.showBillFields)
|
|
|
+ }
|
|
|
+ if (!isObjEmpty(billGroup.hideBillFields)) {
|
|
|
+ formFields = formFields.concat(billGroup.hideBillFields)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ let gridFields = []
|
|
|
+ if (!isObjEmpty(billGroup.showBillFields)) {
|
|
|
+ gridFields = gridFields.concat(billGroup.showBillFields)
|
|
|
+ }
|
|
|
+ if (!isObjEmpty(billGroup.hideBillFields)) {
|
|
|
+ gridFields = gridFields.concat(billGroup.hideBillFields)
|
|
|
+ }
|
|
|
+ gridGroupFields.push(gridFields)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let formStore = analysisForm(formFields)
|
|
|
+ let gridStoreList = analysisGrid(gridGroupFields)
|
|
|
+
|
|
|
+ if (isObjNull(formStore) || isObjNull(gridStoreList)) {
|
|
|
+ return null
|
|
|
+ } else {
|
|
|
+ return {
|
|
|
+ form: formStore,
|
|
|
+ grid: gridStoreList,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 判断主表字段必填情况,拼接主表字段
|
|
|
+ * @param formFields
|
|
|
+ * @returns {null}
|
|
|
+ */
|
|
|
+function analysisForm (formFields) {
|
|
|
+ if (!isObjNull(formFields)) {
|
|
|
+ let formStore = {}
|
|
|
+ for (let i = 0; i < formFields.length; i++) {
|
|
|
+ let billModel = formFields[i]
|
|
|
+ if (isObjNull(billModel)) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if (isObjEmpty(getValue(billModel)) &&
|
|
|
+ (billModel.allowBlank === 'necessaryField'
|
|
|
+ || billModel.allowBlank === 'F')) {
|
|
|
+ message.error(`${billModel.caption}为必填项`)
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ //Todo 附件上传
|
|
|
+
|
|
|
+ formStore[billModel.field] = getDisplay(billModel)
|
|
|
+ }
|
|
|
+ return formStore
|
|
|
+ } else {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function analysisGrid (gridBillMap) {
|
|
|
+ let gridStoreList = []
|
|
|
+ if (!isObjNull(gridBillMap)) {
|
|
|
+ for (let i = 0; i < gridBillMap.length; i++) {
|
|
|
+ let gridFields = gridBillMap[i]
|
|
|
+ if (!isObjEmpty(gridFields)) {
|
|
|
+ let gridStore = {}
|
|
|
+ for (let j = 0; j < gridFields.length; j++) {
|
|
|
+ let billModel = gridFields[j]
|
|
|
+ if (isObjEmpty(getValue(billModel))
|
|
|
+ && (billModel.allowBlank === 'necessaryField'
|
|
|
+ || billModel.allowBlank === 'F')) {
|
|
|
+ message.error(`${billModel.caption}为必填项`)
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ //Todo 附件上传
|
|
|
+
|
|
|
+ gridStore[billModel.field] = getDisplay(billModel)
|
|
|
+ }
|
|
|
+ gridStoreList.push(gridStore)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return gridStoreList
|
|
|
+}
|
|
|
+
|
|
|
+function getValue (billModel) {
|
|
|
+ return billModel.value || billModel.defValue || ''
|
|
|
+}
|
|
|
+
|
|
|
+function getDisplay (billModel) {
|
|
|
+ return billModel.display || getValue(billModel)
|
|
|
+}
|
|
|
+
|
|
|
+
|