king
2023-10-30 bca9d545b6b10e1d0120b8edaece22f9acbe12f3
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import React, {Component} from 'react'
import { is, fromJS } from 'immutable'
import { notification, Modal, Table, Input, Tabs } from 'antd'
import moment from 'moment'
import { TableOutlined, CloseCircleFilled } from '@ant-design/icons'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import './index.scss'
 
const { Search } = Input
const { TabPane } = Tabs
 
class PopTable extends Component {
  state = {
    options: [],
    searchKey: '',
    pageIndex: 1,
    pageSize: 10,
    orderBy: '',
    loading: false
  }
 
  timer = null
 
  componentDidMount () {
    const { config } = this.props
    const { options } = this.state
 
    if (config.onload === 'true' && options.length === 0) {
      this.loadData()
    }
  }
 
  loadData () {
    const { BID, ID, config } = this.props
    const { pageIndex, pageSize, searchKey, orderBy } = this.state
 
    this.setState({
      loading: true
    })
 
    let param = {
      func: 'sPC_Get_TableData',
      obj_name: 'data',
      exec_type: 'y',
      arr_field: config.arr_field,
      default_sql: 'true',
      custom_script: '',
      menuname: config.label
    }
 
    let sql = ''
    let DateCount = ''
    let _search = ''
    let _orderBy = orderBy || config.order || ''
    let _datasource = config.dataSource
 
    if (config.searchKey && searchKey) {
      let fields = config.searchKey.split(',').map(field => field + ` like '%${searchKey}%'`)
      _search = 'where ' + fields.join(' OR ')
    }
 
    _datasource = _datasource.replace(/@BID@/ig, `'${BID || ''}'`)
    _datasource = _datasource.replace(/@ID@/ig, `'${ID || ''}'`)
 
    if (config.laypage === 'true') {
      sql = `/*system_query*/select top ${pageSize} ${config.arr_field} from (select ${config.arr_field} ,ROW_NUMBER() over(order by ${_orderBy}) as rows from ${_datasource} ${_search}) tmptable where rows > ${pageSize * (pageIndex - 1)} order by tmptable.rows `
      DateCount = `/*system_query*/select count(1) as total from ${_datasource} ${_search}`
    } else if (_orderBy) {
      sql = `/*system_query*/select ${config.arr_field} from (select ${config.arr_field} ,ROW_NUMBER() over(order by ${_orderBy}) as rows from ${_datasource} ${_search}) tmptable order by tmptable.rows `
    } else {
      sql = `/*system_query*/select ${config.arr_field} from ${_datasource} ${_search}  `
    }
 
    let departmentcode = sessionStorage.getItem('departmentcode') || ''
    let organization = sessionStorage.getItem('organization') || ''
    let mk_user_type = sessionStorage.getItem('mk_user_type') || ''
    
    sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
      Select @mk_departmentcode='${departmentcode}', @mk_organization='${organization}', @mk_user_type='${mk_user_type}'
      ${sql}`
 
    // 测试系统打印查询语句
    if (window.GLOB.debugger === true) {
      console.info(`/*${config.label} 数据源*/\n` + sql.replace(/\n\s{6}/ig, '\n'))
      DateCount && console.info(`/*${config.label} 总数查询*/\n` + DateCount.replace(/\n\s{6}/ig, '\n'))
    }
 
    param.LText = Utils.formatOptions(sql)
    param.DateCount = Utils.formatOptions(DateCount)
 
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt('', param.timestamp)
 
    param.username = sessionStorage.getItem('User_Name') || ''
    param.fullname = sessionStorage.getItem('Full_Name') || ''
 
    Api.getSystemCacheConfig(param, config.cache === 'true').then(result => {
      if (result.status) {
        let options = result.data.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.primaryKey] || ''
 
          if (config.controlField && item[config.controlField] === 'true') {
            item.$disabled = true
          }
 
          return item
        })
  
        this.setState({
          options: options,
          total: result.total || 0,
          loading: false
        })
  
        if (result.message) {
          if (result.ErrCode === 'Y') {
            Modal.success({
              title: result.message
            })
          } else if (result.ErrCode === 'S') {
            notification.success({
              top: 92,
              message: result.message,
              duration: 2
            })
          }
        }
      } else {
        this.setState({
          loading: false
        })
  
        if (!result.message) return
        if (result.ErrCode === 'N') {
          Modal.error({
            title: result.message,
          })
        } else if (result.ErrCode !== '-2') {
          notification.error({
            top: 92,
            message: result.message,
            duration: 10
          })
        }
      }
    })
  }
 
  searchOption = (val) => {
    this.setState({searchKey: val})
 
    if (this.timer) {
      clearTimeout(this.timer)
    }
 
    this.timer = setTimeout(() => {
      this.loadData()
    }, 500)
  }
 
  changeRow = (record) => {
    const { config } = this.props
 
    if (record.$disabled) return
 
    let values = {[config.field]: record.$$uuid}
 
    if (config.linkSubField) {
      config.linkSubField.forEach(m => {
        values[m] = record[m] === undefined ? '' : record[m]
      })
    }
 
    this.props.onChange(values, record.$$uuid)
  }
 
  changeTable = (pagination, filters, sorter) => {
    let orderBy = ''
 
    if (sorter.field && sorter.order) {
      if (sorter.order === 'ascend') {
        orderBy = `${sorter.field} asc`
      } else {
        orderBy = `${sorter.field} desc`
      }
    }
 
    this.setState({
      pageIndex: pagination.current,
      pageSize: pagination.pageSize,
      orderBy: orderBy,
    }, () => {
      this.loadData()
    })
  }
 
  render() {
    const { config, value } = this.props
    const { options, loading, total, pageIndex, pageSize } = this.state
    
    return <>
      {config.searchKey ? <Search placeholder={config.placeholder} onSearch={this.searchOption} enterButton /> : null}
      <Table
        rowKey="$$uuid"
        bordered={true}
        rowSelection={null}
        columns={config.cols}
        dataSource={options}
        loading={loading}
        onRow={(record) => {
          let className = ''
 
          if (record.$disabled) {
            className = ' mk-disable-line '
          } else if (value === record.$$uuid) {
            className = ' ant-table-row-selected '
          }
          
          return {
            className: className,
            onClick: () => {this.changeRow(record)},
          }
        }}
        onChange={this.changeTable}
        pagination={config.laypage === 'true' ? {
          current: pageIndex,
          pageSize: pageSize,
          showSizeChanger: true,
          total: total || 0,
          showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`
        } : false}
      />
    </>
  }
}
 
class MKPopSelect extends Component {
  constructor(props) {
    super(props)
    
    this.state = {
      value: props.defaultValue,
      visible: false
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  selectChange = (values, val) => {
    this.props.onChange(values)
    this.setState({value: val, visible: false})
  }
 
  trigger = (e) => {
    e && e.stopPropagation()
 
    this.setState({visible: true})
  }
 
  clear = (e) => {
    const { config } = this.props
 
    e.stopPropagation()
    
    let values = {[config.field]: ''}
 
    if (config.linkSubField) {
      config.linkSubField.forEach(m => {
        values[m] = ''
      })
    }
 
    this.props.onChange(values)
    this.setState({value: ''})
  }
 
  cancel = () => {
    const { mask } = this.props
 
    this.setState({visible: false})
 
    if (mask) {
      this.props.blur()
    }
  }
 
  render() {
    const { mask, BID, ID, config } = this.props
    const { value, visible } = this.state
    
    return <>
      {mask ? <div className="mk-pop-select-mask" onClick={this.trigger}></div> : null}
      <div className="mk-pop-select-wrap" onClick={this.trigger}>
        {value}
        {value && !mask ? <CloseCircleFilled onClick={this.clear} /> : null}
        <TableOutlined onClick={this.trigger}/>
      </div>
      <Modal
        wrapClassName='mk-table-pop-select-modal'
        title={config.label}
        visible={visible}
        closable={true}
        centered={true}
        maskClosable={false}
        cancelText="关闭"
        width={config.popWidth < 100 ? config.popWidth + 'vw' : config.popWidth}
        onCancel={this.cancel}
        destroyOnClose
      >
        {config.pops ? <Tabs>
          {config.pops.map(tab => (
            <TabPane tab={tab.tabName} key={tab.uuid}>
              <PopTable config={tab} BID={BID} ID={ID} value={value} onChange={this.selectChange}/>
            </TabPane>
          ))}
        </Tabs> :
        <PopTable config={config} BID={BID} ID={ID} value={value} onChange={this.selectChange}/>}
      </Modal>
    </>
  }
}
 
export default MKPopSelect