import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Modal, notification, Switch } from 'antd'
|
|
import Api from '@/api'
|
import { getSearchForm } from '@/templates/zshare/formconfig'
|
import asyncComponent from '@/utils/asyncComponent'
|
import MKEmitter from '@/utils/events.js'
|
import DragElement from './dragsearch'
|
import SearchForm from './searchform'
|
import SettingForm from './settingform'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
const FieldsComponent = asyncComponent(() => import('@/templates/sharecomponent/fieldscomponent'))
|
|
class SearchComponent extends Component {
|
static propTpyes = {
|
config: PropTypes.object, // 配置信息
|
updatesearch: PropTypes.func // 更新
|
}
|
|
state = {
|
searchlist: null,
|
sqlVerifing: false,
|
visible: false,
|
showField: false,
|
setVisible: false,
|
card: null
|
}
|
|
/**
|
* @description 搜索条件初始化
|
*/
|
UNSAFE_componentWillMount () {
|
this.setState({
|
searchlist: fromJS(this.props.config.search).toJS()
|
})
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('plusSearch', this.plusSearch)
|
MKEmitter.addListener('revert', this.revert)
|
}
|
|
// dms-trans
|
revert = (id, type) => {
|
if (id !== this.props.config.uuid || type !== 'search') return
|
|
this.setState({searchlist: fromJS(this.props.config.search).toJS()})
|
}
|
|
plusSearch = (MenuId, item, type) => {
|
const { config } = this.props
|
const { searchlist } = this.state
|
|
if (MenuId !== config.uuid) return
|
|
if (type === 'simple') {
|
this.setState({
|
searchlist: [...searchlist, item],
|
}, () => {
|
this.handleSearch(item)
|
})
|
} else if (type === 'replace') {
|
delete item.focus
|
this.setState({
|
searchlist: searchlist.map(cell => {
|
if (cell.field && cell.field.toLowerCase() === item.field.toLowerCase()) {
|
return item
|
}
|
return cell
|
}),
|
}, () => {
|
this.handleSearch(item)
|
})
|
} else if (type === 'multil') {
|
let list = [...searchlist, ...item]
|
list = list.filter(item => !item.origin) // 去除系统项
|
|
this.setState({
|
searchlist: list
|
}, () => {
|
this.props.updatesearch({...config, search: list})
|
})
|
}
|
}
|
|
/**
|
* @description 搜索条件顺序调整,或拖拽添加
|
*/
|
handleList = (list, card) => {
|
const { config } = this.props
|
|
if (card) {
|
this.setState({searchlist: list})
|
this.handleSearch(card)
|
} else {
|
this.setState({searchlist: list}, ()=> {
|
this.props.updatesearch({...config, search: list})
|
})
|
}
|
}
|
|
/**
|
* @description 搜索条件编辑,获取搜索条件表单信息
|
*/
|
handleSearch = (card) => {
|
const { config } = this.props
|
const { searchlist } = this.state
|
let linkableFields = []
|
|
searchlist.forEach(item => {
|
if (item.uuid === card.uuid) return
|
if (!['select', 'link', 'checkcard'].includes(item.type)) return
|
if (item.type === 'checkcard' && item.multiple === 'true') return
|
|
linkableFields.push({
|
value: item.field,
|
text: item.label
|
})
|
})
|
|
let columns = null
|
if (config.columns && config.columns.length) {
|
columns = config.columns.map(item => {
|
return {key: item.uuid, text: item.field, value: item.field, label: item.label}
|
})
|
}
|
|
this.setState({
|
visible: true,
|
card: card,
|
formlist: getSearchForm(card, linkableFields, columns)
|
})
|
}
|
|
/**
|
* @description 取消保存,如果元素为新添元素,则从序列中删除
|
*/
|
editModalCancel = () => {
|
const { config } = this.props
|
const { card, searchlist } = this.state
|
|
if (card.focus) {
|
let _searchlist = searchlist.filter(item => item.uuid !== card.uuid)
|
|
this.setState({
|
card: null,
|
searchlist: _searchlist,
|
visible: false
|
})
|
this.props.updatesearch({...config, search: _searchlist})
|
} else {
|
this.setState({
|
card: null,
|
visible: false
|
})
|
}
|
}
|
|
/**
|
* @description 搜索修改后提交保存
|
* 1、去除系统默认搜索条件
|
* 2、字段及提示文字重复校验
|
* 3、更新下拉菜单可选集合
|
* 4、下拉菜单数据源语法验证
|
*/
|
handleSubmit = () => {
|
const { config } = this.props
|
let _searchlist = fromJS(this.state.searchlist).toJS()
|
|
this.searchFormRef.handleConfirm().then(res => {
|
let fieldrepet = false // 字段重复
|
|
_searchlist = _searchlist.filter(item => !item.origin || item.uuid === res.uuid) // 去除系统项
|
|
_searchlist = _searchlist.map(item => { // 数据更新及重复检测
|
if (item.uuid !== res.uuid && res.field && item.field) {
|
let itemFields = []
|
if (item.type === 'text') {
|
itemFields = item.field.split(',')
|
} else if (item.type === 'group') {
|
itemFields = [item.field, item.datefield]
|
} else {
|
itemFields = [item.field]
|
}
|
|
let resFields = []
|
if (res.type === 'text') {
|
resFields = res.field.split(',')
|
} else if (res.type === 'group') {
|
resFields = [res.field, res.datefield]
|
} else {
|
resFields = [res.field]
|
}
|
|
let setFields = Array.from(new Set([...itemFields, ...resFields]))
|
|
if (setFields.length < itemFields.length + resFields.length && (res.type !== 'date' || item.type !== 'date')) {
|
fieldrepet = true
|
}
|
}
|
|
if (item.uuid === res.uuid) {
|
return res
|
} else {
|
return item
|
}
|
})
|
|
if (fieldrepet) {
|
notification.warning({
|
top: 92,
|
message: '字段已存在!',
|
duration: 5
|
})
|
return
|
}
|
|
if (['checkcard', 'select', 'multiselect', 'link'].includes(res.type) && res.resourceType === '1' && res.database !== 'sso' && /\s/.test(res.dataSource)) {
|
this.setState({
|
sqlVerifing: true
|
})
|
|
let sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
|
${res.dataSource}`
|
sql = sql.replace(/@\$|\$@/ig, '')
|
|
Api.sDebug(sql).then(result => {
|
if (result.status || result.ErrCode === '-2') {
|
this.setState({
|
sqlVerifing: false,
|
searchlist: _searchlist,
|
visible: false
|
}, ()=> {
|
this.props.updatesearch({...config, search: _searchlist})
|
})
|
} else {
|
this.setState({sqlVerifing: false})
|
|
Modal.error({
|
title: result.message
|
})
|
}
|
})
|
} else {
|
this.setState({
|
searchlist: _searchlist,
|
visible: false
|
}, ()=> {
|
this.props.updatesearch({...config, search: _searchlist})
|
})
|
}
|
})
|
}
|
|
/**
|
* @description 搜索条件删除
|
*/
|
deleteElement = (card) => {
|
const { config } = this.props
|
let that = this
|
|
confirm({
|
content: `确定删除 - ${card.label} ?`,
|
onOk() {
|
let _searchlist = fromJS(that.state.searchlist).toJS()
|
|
_searchlist = _searchlist.filter(item => item.uuid !== card.uuid)
|
|
that.setState({
|
searchlist: _searchlist
|
}, () => {
|
that.props.updatesearch({...config, search: _searchlist})
|
})
|
},
|
onCancel() {}
|
})
|
}
|
|
onFieldChange = () => {
|
const { showField } = this.state
|
|
this.setState({
|
showField: !showField
|
})
|
}
|
|
handleSetting = () => {
|
this.setState({
|
setVisible: true
|
})
|
}
|
|
settingSubmit = () => {
|
const { config } = this.props
|
|
this.settingFormRef.handleConfirm().then(res => {
|
this.setState({
|
setVisible: false
|
})
|
|
let _config = null
|
|
if (config.wrap) { // 自定义页面table
|
_config = {...config, wrap: {...config.wrap, ...res}}
|
} else {
|
_config = {...config, setting: {...config.setting, ...res}}
|
}
|
|
this.props.updatesearch(_config)
|
})
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('revert', this.revert)
|
MKEmitter.removeListener('plusSearch', this.plusSearch)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
if (!is(fromJS(this.state), fromJS(nextState))) {
|
return true
|
} else if (this.props.config.wrap) {
|
return !is(fromJS(nextProps.config.wrap), fromJS(this.props.config.wrap))
|
} else {
|
return !is(fromJS(nextProps.config.setting), fromJS(this.props.config.setting))
|
}
|
}
|
|
render() {
|
const { config } = this.props
|
const { searchlist, visible, sqlVerifing, card, showField, setVisible } = this.state
|
|
let size = ''
|
if (config.wrap && config.wrap.searchSize) {
|
size = ' mk-order-' + config.wrap.searchSize
|
}
|
|
return (
|
<div className={'model-table-search-list length' + searchlist.length + (showField ? ' show-field' : '') + size}>
|
<FieldsComponent config={{uuid: config.uuid, search: searchlist}} type="search" />
|
<Switch className="switch-field-show" checkedChildren="开" unCheckedChildren="关" defaultChecked={showField} onChange={this.onFieldChange} />
|
<DragElement
|
list={searchlist}
|
setting={config.wrap || config.setting}
|
handleList={this.handleList}
|
handleMenu={this.handleSearch}
|
deleteMenu={this.deleteElement}
|
handleSetting={this.handleSetting}
|
/>
|
{/* 编辑搜索条件 */}
|
<Modal
|
title={card && card.copyType === 'search' ? '搜索条件-复制' : '搜索条件-编辑'}
|
wrapClassName="mk-scroll-modal"
|
visible={visible}
|
width={950}
|
maskClosable={false}
|
onOk={this.handleSubmit}
|
confirmLoading={sqlVerifing}
|
onCancel={this.editModalCancel}
|
destroyOnClose
|
>
|
<SearchForm
|
card={this.state.card}
|
formlist={this.state.formlist}
|
inputSubmit={this.handleSubmit}
|
wrappedComponentRef={(inst) => this.searchFormRef = inst}
|
/>
|
</Modal>
|
<Modal
|
title={'搜索设置'}
|
visible={setVisible}
|
width={800}
|
maskClosable={false}
|
onOk={this.settingSubmit}
|
onCancel={() => this.setState({setVisible: false})}
|
destroyOnClose
|
>
|
<SettingForm
|
setting={config.wrap || config.setting}
|
inputSubmit={this.settingSubmit}
|
wrappedComponentRef={(inst) => this.settingFormRef = inst}
|
/>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
export default SearchComponent
|