| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- import { PageContainer } from '@ant-design/pro-layout';
- import { observer } from '@formily/react';
- import { model } from '@formily/reactive';
- import { useIntl } from '@@/plugin-locale/localeExports';
- import { useDomFullHeight } from '@/hooks';
- import { useEffect, useState } from 'react';
- import { Empty, PermissionButton } from '@/components';
- import { ProColumns } from '@jetlinks/pro-table';
- import service from '@/pages/DataCollect/service';
- import SearchComponent from '@/components/SearchComponent';
- import { Card, Col, Pagination, Row } from 'antd';
- import ChannelCard from '@/components/ProTableCard/CardItems/DataCollect/channel';
- import { DeleteOutlined, EditOutlined, PlayCircleOutlined, StopOutlined } from '@ant-design/icons';
- import { onlyMessage } from '@/utils/util';
- import Save from '@/pages/DataCollect/Channel/Save';
- import { getMenuPathByCode, MENUS_CODE } from '@/utils/menu';
- import useHistory from '@/hooks/route/useHistory';
- const ChannelModel = model<{
- visible: boolean;
- current: Partial<ChannelItem>;
- currentPage: number;
- }>({
- visible: false,
- current: {},
- currentPage: 0,
- });
- export default observer(() => {
- const intl = useIntl();
- const { minHeight } = useDomFullHeight(`.data-collect-channel-card`, 24);
- const [param, setParam] = useState({ pageSize: 12, terms: [] });
- const { permission } = PermissionButton.usePermission('DataCollect/Channel');
- const [loading, setLoading] = useState<boolean>(true);
- const [dataSource, setDataSource] = useState<any>({
- data: [],
- pageSize: 12,
- pageIndex: 0,
- total: 0,
- });
- const history = useHistory();
- const columns: ProColumns<ChannelItem>[] = [
- {
- title: '通道名称',
- dataIndex: 'name',
- },
- {
- title: '通讯协议',
- dataIndex: 'provider',
- valueType: 'select',
- valueEnum: {
- OPC_UA: {
- text: 'OPC_UA',
- status: 'OPC_UA',
- },
- MODBUS_TCP: {
- text: 'MODBUS_TCP',
- status: 'MODBUS_TCP',
- },
- },
- },
- {
- title: '状态',
- dataIndex: 'state',
- valueType: 'select',
- valueEnum: {
- enabled: {
- text: '正常',
- status: 'enabled',
- },
- disabled: {
- text: '禁用',
- status: 'disabled',
- },
- },
- },
- {
- title: '运行状态',
- dataIndex: 'runningState',
- valueType: 'select',
- valueEnum: {
- running: {
- text: '运行中',
- status: 'running',
- },
- partialError: {
- text: '部分错误',
- status: 'partialError',
- },
- failed: {
- text: '错误',
- status: 'failed',
- },
- // stopped: {
- // text: '已停止',
- // status: 'stopped',
- // },
- },
- },
- {
- title: '说明',
- dataIndex: 'description',
- },
- ];
- const handleSearch = (params: any) => {
- setLoading(true);
- setParam(params);
- service
- .queryChannel({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
- .then((resp) => {
- if (resp.status === 200) {
- setDataSource(resp.result);
- }
- setLoading(false);
- });
- };
- useEffect(() => {
- handleSearch(param);
- }, []);
- const getState = (record: Partial<ChannelItem>) => {
- if (record) {
- if (record?.state?.value === 'enabled') {
- return { ...record?.runningState };
- } else {
- return {
- text: '禁用',
- value: 'disabled',
- };
- }
- } else {
- return {};
- }
- };
- const onPageChange = (page: number, size: number) => {
- if (ChannelModel.currentPage === size) {
- handleSearch({
- ...param,
- pageIndex: page - 1,
- pageSize: size,
- });
- } else {
- ChannelModel.currentPage = size;
- handleSearch({
- ...param,
- pageIndex: 0,
- pageSize: size,
- });
- }
- };
- return (
- <PageContainer>
- <SearchComponent<ChannelItem>
- field={columns}
- target="data-collect-channel"
- onSearch={(data) => {
- const dt = {
- pageSize: 12,
- terms: [...data?.terms],
- };
- handleSearch(dt);
- }}
- />
- <Card
- loading={loading}
- bordered={false}
- style={{ position: 'relative', minHeight }}
- className={'data-collect-channel-card'}
- >
- <div style={{ width: '100%', display: 'flex', justifyContent: 'flex-start' }}>
- <PermissionButton
- isPermission={permission.add}
- onClick={() => {
- ChannelModel.visible = true;
- ChannelModel.current = {};
- }}
- key="button"
- type="primary"
- >
- 新增通道
- </PermissionButton>
- </div>
- <div style={{ height: '100%', paddingBottom: 48 }}>
- {dataSource?.data.length > 0 ? (
- <Row gutter={[24, 24]} style={{ marginTop: 10 }}>
- {(dataSource?.data || []).map((record: any) => (
- <Col key={record.id} span={8}>
- <ChannelCard
- {...record}
- state={getState(record)}
- actions={[
- <PermissionButton
- type={'link'}
- onClick={() => {
- ChannelModel.current = record;
- ChannelModel.visible = true;
- }}
- key={'edit'}
- isPermission={permission.update}
- >
- <EditOutlined />
- {intl.formatMessage({
- id: 'pages.data.option.edit',
- defaultMessage: '编辑',
- })}
- </PermissionButton>,
- <PermissionButton
- key={'action'}
- type={'link'}
- style={{ padding: 0 }}
- isPermission={permission.action}
- popConfirm={{
- title: intl.formatMessage({
- id: `pages.data.option.${
- record?.state?.value !== 'disabled' ? 'disabled' : 'enabled'
- }.tips`,
- defaultMessage: '确认禁用?',
- }),
- onConfirm: async () => {
- const resp =
- record?.state?.value !== 'disabled'
- ? await service.updateChannel(record.id, {
- state: 'disabled',
- runningState: 'stopped',
- })
- : await service.updateChannel(record.id, {
- state: 'enabled',
- runningState: 'running',
- });
- if (resp.status === 200) {
- onlyMessage('操作成功!');
- handleSearch(param);
- } else {
- onlyMessage('操作失败!', 'error');
- }
- },
- }}
- >
- {record?.state?.value !== 'disabled' ? (
- <StopOutlined />
- ) : (
- <PlayCircleOutlined />
- )}
- {intl.formatMessage({
- id: `pages.data.option.${
- record?.state?.value !== 'disabled' ? 'disabled' : 'enabled'
- }`,
- defaultMessage: record?.state?.value !== 'disabled' ? '禁用' : '启用',
- })}
- </PermissionButton>,
- <PermissionButton
- key="delete"
- isPermission={permission.delete}
- type={'link'}
- style={{ padding: 0 }}
- disabled={record?.state?.value !== 'disabled'}
- tooltip={
- record?.state?.value !== 'disabled'
- ? {
- title: '正常的通道不能删除',
- }
- : undefined
- }
- popConfirm={{
- title: '该操作将会删除下属采集器与点位,确定删除?',
- placement: 'topRight',
- onConfirm: async () => {
- await service.removeChannel(record.id);
- handleSearch(param);
- onlyMessage(
- intl.formatMessage({
- id: 'pages.data.option.success',
- defaultMessage: '操作成功!',
- }),
- );
- },
- }}
- >
- <DeleteOutlined />
- </PermissionButton>,
- ]}
- onClick={() => {
- const url = getMenuPathByCode(MENUS_CODE['DataCollect/Collector']);
- history.push(url, { channelId: record.id });
- }}
- />
- </Col>
- ))}
- </Row>
- ) : (
- <div style={{ height: minHeight - 150 }}>
- <Empty />
- </div>
- )}
- </div>
- {dataSource?.data?.length > 0 && (
- <div
- style={{
- display: 'flex',
- justifyContent: 'flex-end',
- position: 'absolute',
- width: '100%',
- bottom: 10,
- right: '2%',
- }}
- >
- <Pagination
- showSizeChanger
- size="small"
- className={'pro-table-card-pagination'}
- total={dataSource?.total || 0}
- current={dataSource?.pageIndex + 1}
- onChange={(page, size) => {
- onPageChange(page, size);
- }}
- onShowSizeChange={(current, size) => {
- handleSearch({
- ...param,
- pageIndex: 1,
- pageSize: size,
- });
- }}
- pageSizeOptions={[12, 24, 48, 96]}
- pageSize={dataSource?.pageSize}
- showTotal={(num) => {
- const minSize = dataSource?.pageIndex * dataSource?.pageSize + 1;
- const MaxSize = (dataSource?.pageIndex + 1) * dataSource?.pageSize;
- return `第 ${minSize} - ${MaxSize > num ? num : MaxSize} 条/总共 ${num} 条`;
- }}
- />
- </div>
- )}
- </Card>
- {ChannelModel.visible && (
- <Save
- data={ChannelModel.current}
- close={() => {
- ChannelModel.visible = false;
- }}
- reload={() => {
- ChannelModel.visible = false;
- handleSearch(param);
- }}
- />
- )}
- </PageContainer>
- );
- });
|