Site.tsx 4.83 KB
Newer Older
DarkForst's avatar
DarkForst committed
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
import React, {useState} from 'react';
import {useRequest, Access} from 'umi'
import {Form, Divider, Popconfirm, Button, message} from "antd";

import {fetchNodeList, fetchNodeDelete} from '@/services/device'
import {fetchOrganization, fetchSystemTreeNode, fetchUserTree} from '@/services/dict'
import useRequestTable from "@/useHooks/useRequestTable";
import type {ColumnProps} from "antd/lib/table";
import AddSite from "@/pages/Ops/Device/System/components/AddSite";
import AutoTable from "@/pages/Ops/Device/StandingBook/components/AutoTable";
import {createTree, createUserTree} from "@/utils/utils";
import type {CreateFormProps} from "@/pages/Ops/Device/StandingBook/components/AutoTable/createForm";
import {insideAccess} from '@/access'

const Site: React.FC = () => {
  const [visible, setVisible] = useState<boolean>(false);
  const [type, setType] = useState<'add' | 'edit'>('add')
  const [selectId, setSelectId] = useState<number | undefined>()
  const [form] = Form.useForm()

  const systemTree = useRequest(fetchSystemTreeNode)
  const systemData = systemTree?.data?.map((item: any) => ({value: item.systemId, label: item.name}))
  const sitePage = useRequestTable((payload) => fetchNodeList({...payload, siteIs: true}), {
    form
  });
  const nodeDelete = useRequest(fetchNodeDelete, {
    manual: true,
    onSuccess: () => {
      message.success('删除成功', 1)
      sitePage?.search.refresh()
    }
  })
  const organization = useRequest(fetchOrganization)
  const organizationData = organization.data && createTree(organization.data, 'subList') || []

  const userTree = useRequest(fetchUserTree)
  const userData = createUserTree(userTree?.data || []);

  const valChange = (payload: any) => {
    sitePage?.search.submit(payload)
  }

  const openEdit = (id: number) => {
    setType('edit')
    setVisible(true)
    setSelectId(id)

  }
  const openAdd = () => {
    setType('add')
    setVisible(true)
  }
  const closeModal = () => {
    setVisible(false)
  }

  const allFormData: CreateFormProps[]  = [
    { label: '设备系统', name: 'systemId', type: 'select', selectData: systemData},
    { label: '管理部门', name: 'departmentId', type: 'treeSelect', selectData: organizationData},
    { label: '责任主管', name: 'responsibleSupervisorUid', type: 'treeSelect', selectData: userData},
    { label: '查询', name: 'name', type: 'input'},
  ];

  const columns: ColumnProps<API.Store>[] = [
    { align: 'center', title: '站点名称', dataIndex: 'name'},
    { align: 'center', title: '站点编号', dataIndex: 'code' },
    { align: 'center', title: '所属系统', dataIndex: 'parent',
      render: (val, record) => val?.name || record?.system?.name || ''
    },
    { align: 'center', title: '区域', dataIndex: 'area',
      render: val => val?.name || ''
    },
    { align: 'center', title: '位置', dataIndex: 'position',
      render: val => val?.name || ''
    },
    { align: 'center', title: '管理部门', dataIndex: 'department',
      render: val => val?.name || ''
    },
    { align: 'center', title: '责任人', dataIndex: 'responsible',
      render: val => val?.name || ''
    },
    { align: 'center', title: '联系电话', dataIndex: 'belongingsCode2',
      render: (val, record) => record?.responsible?.mobile || ''
    },
    { align: 'center', title: '责任主管', dataIndex: 'responsibleSupervisor',
      render: val => val?.name || ''
    },
    { align: 'center', title: '联系电话', dataIndex: 'belongingsCode2',
      render: (val, record) => record?.responsibleSupervisor?.mobile || ''
    },

    { align: 'center', title: '操作', dataIndex: 'option', fixed: 'right', width: 100,
      render: (text: any, record: any) => (
        <>
          <Access accessible={insideAccess('oOMEmkFhzOr')}>
            <a onClick={() => openEdit(record.id)}>编辑</a>
            <Divider type="vertical" />
          </Access>
          <Access accessible={insideAccess('CwFkENmnqgk')}>
            <Popconfirm title="确认删除此站点吗?" onConfirm={() => nodeDelete.run(record.id)}>
              <a>删除</a>
            </Popconfirm>
          </Access>

        </>
      ),
    },
  ]
  return (
    <div className="pos_full_page">
      <AutoTable
        bordered
        form={form}
        loading={sitePage.loading || false}
        formData={allFormData}
        tableExtra={
          <Access accessible={insideAccess('oOMEmkFhzOr')}>
            <Button type="primary" onClick={openAdd}>添加站点</Button>
          </Access>
        }
        onValuesChange={valChange}
        columns={columns}
        dataSource={sitePage.dataSource}
        {...sitePage.tableProps}
      />
      <AddSite
        type={type}
        visible={visible}
        selectId={selectId}
        onRefresh={sitePage?.search?.refresh}
        systemData={systemTree?.data}
        handleClose={closeModal}
      />
    </div>
  );
};

export default Site;