king
2020-11-05 ee50d5424a093209d1c5c549f4578107893b22f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { Icon, Modal } from 'antd'
 
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import VerifyCard from './verifycard'
import './index.scss'
 
class DataSource extends Component {
  static propTpyes = {
    config: PropTypes.any,
    updateConfig: PropTypes.func
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    sourcelist: [],
    mainSearch: [],
    visible: false,
    loading: false,
    setting: null
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    this.setState({setting: fromJS(config.setting).toJS()})
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  editDataSource = () => {
    const { config, menu } = this.props
 
    let search = []
    let parents = []
    let _conf = config
    let getParents = (box) => {
      box.components.forEach(item => {
        if (item.type !== 'tabs') return
 
        item.subtabs.forEach(tab => {
          if (_conf.parentId === tab.parentId && _conf.tabId === tab.uuid) {
            parents.unshift(tab)
            _conf = item
 
            if (_conf.parentId && _conf.tabId) {
              getParents(menu)
            }
          } else {
            getParents(tab)
          }
        })
      })
    }
 
    if (config.parentId && config.tabId) {
      getParents(menu)
    }
 
    parents.unshift(menu)
 
    parents.forEach(parent => {
      parent.components.forEach(item => {
        if (item.type === 'search') {
          search = item.search
        }
      })
    })
 
    this.setState({
      visible: true,
      mainSearch: search
    })
  }
 
  verifySubmit = () => {
    const { config } = this.props
 
    this.setState({loading: true})
    this.verifyRef.submitDataSource().then(res => {
 
      if (res.columns) {
        res.columns = res.columns.map(item => {
    
          if (/int/ig.test(item.datatype)) {
            item.type = 'number'
            item.decimal = 0
          } else if (/Decimal/ig.test(item.datatype)) {
            item.type = 'number'
            item.decimal = +item.datatype.replace(/^Decimal\(18,/ig, '').replace(/\)/ig, '')
          } else {
            item.type = 'text'
            item.fieldlength = +item.datatype.replace(/^Nvarchar\(/ig, '').replace(/\)/ig, '')
          }
          return item
        })
      }
      
      this.setState({loading: false, visible: false})
      this.props.updateConfig({...config, ...res})
    }, () => {
      this.setState({loading: false})
    })
  }
 
  render () {
    const { config, menu } = this.props
    const { visible, dict, loading, mainSearch } = this.state
 
    return (
      <div className="model-datasource">
        <Icon type="setting" onClick={() => this.editDataSource()} />
        <Modal
          wrapClassName="popview-modal"
          title={'数据源配置'}
          visible={visible}
          width={'75vw'}
          maskClosable={false}
          style={{minWidth: '900px', maxWidth: '1200px'}}
          okText={dict['model.submit']}
          onOk={this.verifySubmit}
          confirmLoading={loading}
          onCancel={() => { this.setState({ visible: false }) }}
          destroyOnClose
        >
          <VerifyCard
            dict={dict}
            menu={menu}
            mainSearch={mainSearch}
            config={config}
            wrappedComponentRef={(inst) => this.verifyRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
const mapStateToProps = (state) => {
  return {
    menu: state.customMenu
  }
}
 
const mapDispatchToProps = () => {
  return {}
}
 
export default connect(mapStateToProps, mapDispatchToProps)(DataSource)