From 2aa5ab63b4bbce5c36dbb3511b205b3b5f6af9bd Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 07 五月 2024 11:34:20 +0800
Subject: [PATCH] 2024-05-07

---
 src/templates/sharecomponent/fieldscomponent/index.jsx |  203 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 173 insertions(+), 30 deletions(-)

diff --git a/src/templates/sharecomponent/fieldscomponent/index.jsx b/src/templates/sharecomponent/fieldscomponent/index.jsx
index c7630d7..52d7eb1 100644
--- a/src/templates/sharecomponent/fieldscomponent/index.jsx
+++ b/src/templates/sharecomponent/fieldscomponent/index.jsx
@@ -3,9 +3,8 @@
 import { is, fromJS } from 'immutable'
 import { Button, Modal, Empty, notification } from 'antd'
 
+import Api from '@/api'
 import Utils from '@/utils/utils.js'
-import zhCN from '@/locales/zh-CN/model.js'
-import enUS from '@/locales/en-US/model.js'
 import EditCard from './editcard'
 
 import MKEmitter from '@/utils/events.js'
@@ -13,23 +12,20 @@
 
 class FieldsComponent extends Component {
   static propTpyes = {
-    type: PropTypes.string,          // 鎼滅储鏉′欢娣诲姞銆佹樉绀哄垪娣诲姞
-    config: PropTypes.object,        // 瀹瑰櫒Id
+    type: PropTypes.string,
+    config: PropTypes.object,
     updatefield: PropTypes.func
   }
 
   state = {
-    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
     appType: sessionStorage.getItem('appType'),
     fields: [],             // 瀛楁闆�
     visible: false,         // 妯℃�佹鎺у埗
     selectCards: []
   }
 
-  queryField = () => {
-    const { type, config } = this.props
-    // 鍒ゆ柇鏄惁宸查�夋嫨琛ㄥ悕
-    if (!window.GLOB.tableFields || window.GLOB.tableFields.length === 0) {
+  checkField = () => {
+    if (!window.GLOB.publicTables || window.GLOB.publicTables.length === 0) {
       notification.warning({
         top: 92,
         message: '璇烽�夋嫨琛ㄥ悕锛�',
@@ -38,9 +34,110 @@
       return
     }
 
+    window.GLOB.tableFields = window.GLOB.tableFields || []
+
+    let index = 0
+
+    let deffers = window.GLOB.publicTables.map(item => {
+      let tb = window.GLOB.tableFields.filter(tab => tab.tableName === item.TbName)[0]
+
+      if (tb) {
+        return Promise.resolve(fromJS(tb).toJS())
+      }
+
+      return new Promise(resolve => {
+        setTimeout(() => {
+          Api.getCloudConfig({func: 'sPC_Get_FieldName', TBName: item.TbName}).then(res => {
+            let tabmsg = {
+              status: res.status,
+              message: res.message,
+              tableName: item.TbName,
+              columns: []
+            }
+
+            if (res.FDName && res.FDName.length > 0) {
+              tabmsg.columns = res.FDName.map(item => {
+                let _type = item.FieldType.toLowerCase()
+                let _datatype = item.FieldType.toLowerCase()
+                let _decimal = 0
+                let _length = 50
+                if (/^nvarchar/.test(_type)) {
+                  try { // 瀛樺湪max
+                    _length = +_type.match(/\d+/)[0] || 50
+                  } catch (e) {
+                    _length = 4000
+                  }
+                  _type = 'text'
+                } else if (/^int/.test(_type)) {
+                  _type = 'number'
+                } else if (/^decimal/.test(_type)) {
+                  _decimal = _type.split(',')[1]
+                  _decimal = parseInt(_decimal)
+                  _type = 'number'
+                } else if (/^datetime/.test(_type)) {
+                  _type = 'datetime'
+                } else if (/^date/.test(_type)) {
+                  _type = 'date'
+                } else {
+                  _type = 'text'
+                }
+
+                if (/^nvarchar/.test(_datatype)) {
+                  _datatype = _datatype.replace(/^nvarchar/, 'Nvarchar')
+                } else if (/^decimal/.test(_datatype)) {
+                  _datatype = _datatype.replace(/^decimal/, 'Decimal')
+                } else if (/^int/.test(_datatype)) {
+                  _datatype = _datatype.replace(/^int/, 'Int')
+                }
+    
+                return {
+                  field: item.FieldName || '',
+                  label: item.FieldDec,
+                  type: _type,
+                  datatype: _type,
+                  decimal: _decimal,
+                  length: _length,
+                  $datatype: _datatype
+                }
+              })
+            }
+
+            resolve(tabmsg)
+          })
+        }, index * 50)
+
+        index++
+      })
+    })
+    Promise.all(deffers).then(response => {
+      let error = false
+      let _columns = response
+
+      response.forEach(item => {
+        if (!item.status) {
+          error = item.message || '瀛楁鏌ヨ澶辫触锛�'
+        }
+      })
+
+      if (error) {
+        notification.warning({
+          top: 92,
+          message: error,
+          duration: 5
+        })
+      } else {
+        window.GLOB.tableFields = _columns
+        this.queryField(_columns)
+      }
+    })
+  }
+
+  queryField = (tableFields) => {
+    const { type, config } = this.props
+
     // 琛ㄥ瓧娈甸泦杞负map鏁版嵁
     let columns = new Map()
-    window.GLOB.tableFields.forEach(table => {
+    tableFields.forEach(table => {
       table.columns.forEach(column => {
         columns.set(column.field.toLowerCase(), column)
       })
@@ -109,13 +206,13 @@
       selectCards.forEach(item => {
         let _match = ''
         let initval = ''
-        if (item.type === 'select') {
+        let _type = item.type
+        if (item.type === 'date') {
+          _type = 'daterange'
+        } else if (item.type === 'select') {
           _match = '='
-        } else if (item.type === 'daterange') {
-          initval = '[30, 0]'
-          _match = 'between'
         } else {
-          item.type = 'text'
+          _type = 'text'
           _match = 'like'
         }
 
@@ -124,9 +221,8 @@
           label: item.label,
           field: item.field,
           initval: initval,
-          type: item.type,
+          type: _type,
           resourceType: '0',
-          setAll: 'false',
           options: [],
           orderType: 'asc',
           match: _match,
@@ -144,15 +240,21 @@
           label: item.label,
           field: item.field,
           Hide: 'false',
-          IsSort: item.type === 'picture' ? 'false' : 'true',
-          type: item.type,
-          Width: 120
+          IsSort: 'true',
+          type: item.type === 'number' ? 'number' : 'text',
+          Width: item.type === 'number' ? 80 : 120
         }
 
         if (item.type === 'number') {
           newcard.decimal = item.decimal
         } else {
           newcard.fieldlength = item.length || 50
+        }
+
+        if (item.type === 'date') {
+          newcard.textFormat = 'YYYY-MM-DD'
+        } else if (item.type === 'datetime') {
+          newcard.textFormat = 'YYYY-MM-DD HH:mm:ss'
         }
 
         items.push(newcard)
@@ -170,16 +272,19 @@
           field: item.field,
           datatype: _t
         }
-        items.push(newcard)
+
+        items.unshift(newcard)
         keys.push(item.field.toLowerCase())
       })
 
       this.props.updatefield(items)
     } else if (type === 'form') {
-      let lastItem = config.fields[config.fields.length - 1]
+      let firstItem = config.fields[0]
       let span = this.state.appType === 'mob' ? 24 : 12
-      if (lastItem && lastItem.span) {
-        span = lastItem.span
+      let labelwidth = 33.3
+      if (firstItem && firstItem.span) {
+        span = firstItem.span
+        labelwidth = firstItem.labelwidth || 33.3
       }
       selectCards.forEach(item => { // 寰幆娣诲姞鏂板瀛楁
         let newcard = {
@@ -189,15 +294,46 @@
           initval: item.type === 'number' ? 0 : '',
           type: item.type,
           resourceType: '0',
-          setAll: 'false',
           span: span,
-          labelwidth: 33.3,
+          labelwidth: labelwidth,
           options: [],
           dataSource: '',
           decimal: item.decimal,
           orderType: 'asc',
           readonly: 'false',
           required: 'true'
+        }
+
+        if (/^icon|images?$/ig.test(item.field)) {
+          newcard.type = 'fileupload'
+          newcard.fileType = 'picture-card'
+          newcard.fieldlength = item.length || 512
+          newcard.maxSize = 1
+          newcard.maxfile = 1
+        } else if (item.type === 'text' && item.length >= 256) {
+          newcard.type = 'textarea'
+          newcard.required = 'false'
+          newcard.fieldlength = item.length
+          if (firstItem) {
+            if (firstItem.type === newcard.type) {
+              newcard.span = firstItem.span
+              newcard.labelwidth = firstItem.labelwidth
+            } else {
+              newcard.span = 24
+              if (firstItem.span === 12) {
+                newcard.labelwidth = 16.2
+              } else if (firstItem.span === 8) {
+                newcard.labelwidth = 10.5
+              } else if (firstItem.span === 6) {
+                newcard.labelwidth = 7.7
+              }
+            }
+          } else {
+            newcard.span = 24
+            newcard.labelwidth = 16.2
+          }
+        } else if (item.type === 'text') {
+          newcard.fieldlength = item.length || 50
         }
 
         items.push(newcard)
@@ -243,19 +379,26 @@
 
   render() {
     const { type } = this.props
-    const { dict, fields } = this.state
+    const { fields } = this.state
+
+    let label = '鎵归噺娣诲姞'
+    if (type === 'search') {
+      label = '娣诲姞鎼滅储'
+    } else if (type === 'columns') {
+      label = '娣诲姞鏄剧ず鍒�'
+    }
 
     return (
       <div className="quickly-add">
-        <Button type="primary" block onClick={this.queryField}>{dict['model.batchAdd']}</Button>
+        <Button type="primary" block onClick={this.checkField}>{label}</Button>
         {/* 鏍规嵁瀛楁鍚嶆坊鍔犳樉绀哄垪鍙婃悳绱㈡潯浠� */}
         <Modal
           wrapClassName="model-table-fieldmanage-modal"
-          title={dict['model.edit']}
+          title="缂栬緫"
           visible={this.state.visible}
           width={'65vw'}
           maskClosable={false}
-          cancelText={dict['model.close']}
+          cancelText="鍏抽棴"
           onOk={this.addFieldSubmit}
           onCancel={() => this.setState({ visible: false })}
           destroyOnClose

--
Gitblit v1.8.0