SearchInfo.js 13.4 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3
import fetch from 'dva/fetch';
import React from 'react';
import ReactDOM from 'react-dom';
4 5 6 7 8 9 10 11 12 13 14 15
import {
	Select,
	Button,
	Modal,
	Input,
	Transfer,
	Row,
	Col,
	Form,
	message,
	notification,
} from 'antd';
徐立's avatar
徐立 committed
16
import { connect } from 'dva';
17
import { getToken } from '../utils/token';
chscls@163.com's avatar
chscls@163.com committed
18
import config from '@/webPublic/one_stop_public/config';
徐立's avatar
徐立 committed
19 20 21 22 23 24 25 26 27 28

import QueryItem from './QueryItem';
import OrderItem from './OrderItem';

import FormdataWrapper from '../utils/object-to-formdata-custom';
import ButtonDiy from './ButtonDiy/ButtonDiy';
const Option = Select.Option;
var keyX = 1;

function swapArray(arr, index1, index2) {
29 30
	arr[index1] = arr.splice(index2, 1, arr[index1])[0];
	return arr;
徐立's avatar
徐立 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

/**
 *
 * 2019/02/21  修改导出方式为fetch
 * 增加 props.fileName 设置导出文件名称
 * mustQuerys 设置搜索条件 demo :
 *  mustQuerys={[{
                hql: 'bean.adminStatus',
                x: '=',
                v: "pass",
                c: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
                notes: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
              }]}
 */

const FormItem = Form.Item;
@connect(({ DataObj, loading }) => ({
49 50
	DataObj,
	loading: loading.models.DataObj,
徐立's avatar
徐立 committed
51 52 53
}))
@Form.create()
export default class SearchInfo extends React.Component {
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	constructor(props) {
		super(props);
		this.state = {
			visiable: false,

			queryVisiable: false,
			groupVisiable: false,
			sortVisiable: false,
			currentQueryKey: null,
			currentGroupKey: null,
			orderVisiable: false,
			currentOrderKey: null,
			querys: [],
			orders: [],
			groups: [],
			gs: [],
			os: [],
			qs: [],
			mockData: [],
			targetKeys: [],
			sourceSelectedKeys: [],
			targetSelectedKeys: [],
			infos: {},
			confirmLoading: false,
		};
	}

	onSelectChange = (sourceSelectedKeys, targetSelectedKeys) => {
		this.setState({ sourceSelectedKeys, targetSelectedKeys });
	};
	deleteQuery = (i) => {
		const querys = this.state.querys;
		querys.splice(i, 1);
		this.setState({ querys });
	};
	deleteOrder = (i) => {
		const orders = this.state.orders;
		orders.splice(i, 1);
		this.setState({ orders });
	};
	deleteGroup = (i) => {
		const groups = this.state.groups;
		groups.splice(i, 1);
		this.setState({ groups });
	};
	open = () => {
		const { dispatch } = this.props;
		dispatch({
			type: 'DataObj/getExportInfo',
			payload: {
				objId: this.props.objId,
			},
			callback: (infos) => {
				const mockData = [];
				const qs = [];
				const gs = [];
				const os = [];
				for (var key in infos) {
					const x = infos[key];
					mockData.push({
						key: x.field,
						title: x.name,
						chosen: false,
					});
					if (x.canQuery) {
						qs.push(x);
					}
					if (x.canGroup) {
						gs.push(x);
					}
					if (x.canOrder) {
						os.push(x);
					}
				}

				this.setState({
					mockData,
					infos,
					visiable: true,
					qs,
					gs,
					os,
				});
			},
		});
	};
	onCancle = () => {
		this.setState({ visiable: false });
	};
	handleChange = (targetKeys, direction, moveKeys) => {
		this.setState({ targetKeys });
	};

	up = () => {
		keyX = keyX + 1;
		var { targetKeys, targetSelectedKeys } = this.state;
		for (var i = 0; i < targetKeys.length; i++) {
			if (targetKeys[i] === targetSelectedKeys[0]) {
				if (i === 0) return;
				targetKeys = swapArray(targetKeys, i, i - 1);

				break;
			}
		}

		this.setState({ targetKeys });
	};
	down = () => {
		keyX = keyX + 1;
		var { targetKeys, targetSelectedKeys } = this.state;
		for (var i = 0; i < targetKeys.length; i++) {
			if (targetKeys[i] === targetSelectedKeys[0]) {
				if (i === targetKeys.length - 1) return;
				targetKeys = swapArray(targetKeys, i, i + 1);

				break;
			}
		}

		this.setState({ targetKeys });
	};
	bottom = () => {
		keyX = keyX + 1;
		const { targetKeys, targetSelectedKeys } = this.state;
		var x = [];
		for (var i = 0; i < targetKeys.length; i++) {
			if (!targetSelectedKeys.includes(targetKeys[i])) {
				x.push(targetKeys[i]);
			}
		}
		x = [...x, ...targetSelectedKeys];
		this.setState({ targetKeys: x });
	};
	top = () => {
		keyX = keyX + 1;
		const { targetKeys, targetSelectedKeys } = this.state;
		const x = [...targetSelectedKeys];
		for (var i = 0; i < targetKeys.length; i++) {
			if (!targetSelectedKeys.includes(targetKeys[i])) {
				x.push(targetKeys[i]);
			}
		}

		this.setState({ targetKeys: x });
	};
	renderItem = (item) => {
		if (this.state.targetKeys.includes(item.key)) {
			const customLabel = (
				<span className="custom-item" key={item.key}>
					{item.title}
				</span>
			);
			return {
				label: customLabel, // for displayed item
				value: item.title, // for title and filter matching
			};
		} else {
			const customLabel = (
				<span className="custom-item" key={item.key}>
					{item.title}
				</span>
			);
			return {
				label: customLabel, // for displayed item
				value: item.title, // for title and filter matching
			};
		}
	};
	addOrder = () => {
		this.setState({ orderVisiable: true });
	};
	okOrder = () => {
		const orders = this.state.orders;
		orders.push(this.state.infos[this.state.currentOrderKey]);
		this.setState({ orders, orderVisiable: false, currentOrderKey: null });
	};
	okGroup = () => {
		const groups = this.state.groups;
		groups.push(this.state.infos[this.state.currentGroupKey]);
		this.setState({ groups, groupVisiable: false, currentGroupKey: null });
	};
	addGroup = () => {
		this.setState({ groupVisiable: true });
	};

	addQuery = () => {
		this.setState({ queryVisiable: true });
	};
	okQuery = () => {
		const querys = this.state.querys;
		querys.push(this.state.infos[this.state.currentQueryKey]);
		this.setState({ querys, queryVisiable: false, currentQueryKey: null });
	};
	search = () => {
		this.props.form.validateFields((err, fieldsValue) => {
			if (err) return;
			//form.resetFields();
			var qqs;
			if (this.props.mustQuerys != null) {
				qqs = [...this.props.mustQuerys];
			} else {
				qqs = [];
			}

			const oos = [];
			const ggs = [];
			for (let key in fieldsValue) {
				var x = key.indexOf('__');
				let kk = key.substr(x + 2);
				if (key.indexOf('q__') > -1) {
					qqs.push({
						notes: this.state.infos[kk].notes,
						hql: this.state.infos[kk].hql,
						c: this.state.infos[kk].type,
						x: fieldsValue[key].stringX,
						v: fieldsValue[key].string,
					});
				} else if (key.indexOf('o__') > -1) {
					oos.push({ hql: this.state.infos[kk].hql, x: fieldsValue[key].stringX });
				} else if (key.indexOf('g__') > -1) {
					ggs.push({ hql: this.state.infos[kk].hql });
				}
			}

			let param = {
				querys: JSON.stringify(qqs),
				orders: JSON.stringify(oos),
				objId: this.props.objId,
			};
			param = { ...param, ...(this.props.params || {}) };

			this.props.hanldeHighSearch(param);
			this.setState({ visiable: false });
		});
	};

	downloadFile(url, params) {
		this.setState({ confirmLoading: true });

		fetch(url, {
			method: 'POST',
			body: FormdataWrapper(params),
		})
			.then((res) => {
				if (res.status != '200') {
					return res.json();
				} else {
					return res.blob();
				}
			})
			.then((data) => {
				if (data instanceof Blob) {
					let a = document.createElement('a');
					let url = window.URL.createObjectURL(data);
					let filename = (this.props.fileName ? this.props.fileName : '导出文件') + '.xlsx';
					a.href = url;
					a.download = filename;
					a.click();
					window.URL.revokeObjectURL(url);
					a = null;
				} else {
					notification.error({
						message: `文件导出错误`,
						description: data.errMsg,
					});
				}
			})
			.catch((err) => {
				notification.error({
					message: `网络请求超时`,
				});
			})
			.finally(() => {
				this.setState({ confirmLoading: false });
			});
	}

	cancelGroup = () => {
		this.setState({ currentGroupKey: null, groupVisiable: false });
	};
	cancelQuery = () => {
		this.setState({ currentQueryKey: null, queryVisiable: false });
	};
	cancelOrder = () => {
		this.setState({ currentOrderKey: null, orderVisiable: false });
	};
	selectOrder = (e) => {
		this.setState({ currentOrderKey: e });
	};
	selectQuery = (e) => {
		this.setState({ currentQueryKey: e });
	};
	selectGroup = (e) => {
		this.setState({ currentGroupKey: e });
	};

	render() {
		const { form } = this.props;
		const {
			visiable,
			targetKeys,
			sourceSelectedKeys,
			queryVisiable,
			querys,
			orders,
			groups,
			confirmLoading,
			targetSelectedKeys,
			mockData,
			qs,
			os,
			gs,
			currentQueryKey,
			currentOrderKey,
			orderVisiable,
			currentGroupKey,
			groupVisiable,
		} = this.state;

		return (
			<span>
				<ButtonDiy
					name="自定义查询"
					type="default"
					className="defaultBlue"
					handleClick={this.open}
				/>
				<div id="downloadDiv" style={{ display: 'none' }} />
				<Modal
					width={670}
					maskClosable={false}
					destroyOnClose
					title="自定义查询"
					style={{ textAlign: 'center' }}
					visible={visiable}
					onOk={this.search}
					onCancel={this.onCancle}
					confirmLoading={confirmLoading}>
					<div style={{ overflowY: 'auto', maxHeight: '500px', height: '500px' }}>
						<div style={{ textAlign: 'left' }}>
							<ButtonDiy className="primaryBlue" name="新增查询条件" />

							<Select
								style={{ width: 200, paddingRight: '10px' }}
								value={currentQueryKey}
								onChange={this.selectQuery}>
								{qs.map((r) => {
									return (
										<Option key={r.field} value={r.field}>
											{r.name}
										</Option>
									);
								})}
							</Select>
							{currentQueryKey ? (
								<span style={{ paddingLeft: '0' }}>
									<ButtonDiy className="defaultBlue" name="确定" handleClick={this.okQuery} />
								</span>
							) : (
								''
							)}
							<div style={{ height: '12px' }} />
							{querys.map((r, i) => (
								<FormItem
									key={r.name + i}
									labelCol={{ span: 4 }}
									wrapperCol={{ span: 20 }}
									colon={false}
									label={r.name}>
									{form.getFieldDecorator(i + 'q__' + r.field, {
										initialValue: { stringX: '=' },
										rules: [
											{
												validator: (rule, value, callback) => {
													var errors = [];
													if (value == null) {
														errors.push(new Error('请输入查询值!', rule.field));
													} else {
														if (
															value.stringX != 'IS NOT NULL' &&
															value.stringX != 'IS NULL' &&
															value.string == null
														) {
															errors.push(new Error('请输入查询值!', rule.field));
														}
													}
													callback(errors);
												},
											},
										],
									})(
										<QueryItem
											obj={r}
											key={r.name + i}
											deleteQuery={this.deleteQuery.bind(this, i)}
										/>,
									)}
								</FormItem>
							))}
						</div>
						<div style={{ textAlign: 'left', paddingTop: '15px' }}>
							<ButtonDiy className="primaryBlue" name="新增排序条件" />
							<Select
								style={{ width: 200, paddingRight: '10px' }}
								value={currentOrderKey}
								onChange={this.selectOrder}>
								{os.map((r) => {
									for (let j = 0; j < orders.length; j++) {
										if (orders[j].field === r.field) return;
									}
									return (
										<Option key={r.field} value={r.field}>
											{r.name}
										</Option>
									);
								})}
							</Select>
							{currentOrderKey ? (
								<span style={{ paddingLeft: '0' }}>
									<ButtonDiy className="defaultBlue" name="确定" handleClick={this.okOrder} />
								</span>
							) : (
								''
							)}
							<div style={{ height: '12px' }} />
							{orders.map((r, i) => (
								<FormItem
									key={r.name + i}
									labelCol={{ span: 4 }}
									wrapperCol={{ span: 20 }}
									colon={false}
									label={'按' + r.name}>
									{form.getFieldDecorator(i + 'o__' + r.field, {
										initialValue: { stringX: 'desc' },
									})(<OrderItem deleteOrder={this.deleteOrder.bind(this, i)} />)}
								</FormItem>
							))}
						</div>
						{this.props.voClass ? (
							<div style={{ textAlign: 'left', paddingTop: '15px' }}>
								<span style={{ paddingRight: '10px' }}>聚合条件:</span>
								{!groupVisiable ? (
									<ButtonDiy className="primaryBlue" name="新增" handleClick={this.addGroup} />
								) : (
									<span>
										<Select
											style={{ width: 100 }}
											value={currentGroupKey}
											onChange={this.selectGroup}>
											{gs.map((r) => {
												for (var j = 0; j < groups.length; j++) {
													if (groups[j].field == r.field) return;
												}
												return (
													<Option key={r.field} value={r.field}>
														{r.name}
													</Option>
												);
											})}
										</Select>
										{currentGroupKey ? (
											<ButtonDiy className="defaultBlue" name="确定" handleClick={this.okGroup} />
										) : (
											''
										)}
										<ButtonDiy className="defaultBlue" name="取消" handleClick={this.cancelGroup} />
									</span>
								)}
								{groups.map((r, i) => (
									<FormItem
										key={i + r.name}
										labelCol={{ span: 5 }}
										wrapperCol={{ span: 15 }}
										label={'按' + r.name + '聚合'}>
										{form.getFieldDecorator(i + 'g__' + r.field)(
											<Input style={{ display: 'none' }} value="" />,
										)}
										<ButtonDiy
											className="defaultRed"
											name="删除"
											handleClick={this.deleteGroup.bind(this, i)}
										/>
									</FormItem>
								))}
							</div>
						) : (
							''
						)}
					</div>
				</Modal>
			</span>
		);
	}
徐立's avatar
徐立 committed
547
}