123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
- import styles from './index.module.scss'
- import { Button, Cascader, Checkbox, Input, Popconfirm, Select, Table } from 'antd'
- import { useDispatch, useSelector } from 'react-redux'
- import { A2FromDataType, options1 } from './data'
- import {
- A2_APIgetCity,
- A2_APIgetlist,
- A2_APIgetlistDerive,
- A2_APIremoves
- } from '@/store/action/A2Psychz'
- import { RootState } from '@/store'
- import { A2tableType } from '@/types'
- import { MessageFu } from '@/utils/message'
- import { A1addType } from '../A1Camera/data'
- import AddPsychz from './AddPsychz'
- import dayjs from 'dayjs'
- import ExportJsonExcel from 'js-export-excel'
- const baseTableSelect: A2FromDataType = {
- siteArr: undefined,
- province: '',
- city: '',
- region: '',
- searchKey: '',
- pmUser: '',
- typeIn: '',
- pageSize: 10,
- pageNum: 1,
- isSiteEmpty: 0
- }
- function A2Psychz() {
- // 站址地区的数据-下拉框
- const [optionsCity, setOptionsCity] = useState<any>([])
- const getCityFu = useCallback(async () => {
- const res = await A2_APIgetCity()
- if (res.code === 0) {
- const obj = res.data
- const arr = []
- for (const k in obj) {
- const temp1: any = {
- value: k,
- label: k,
- children: []
- }
- const children1Obj = Reflect.get(obj, k)
- for (const k2 in children1Obj) {
- const children2Arr = Reflect.get(children1Obj, k2)
- const objTemp = {
- value: k2,
- label: k2,
- children: children2Arr.map((v2: any) => ({
- value: v2.region,
- label: v2.region
- }))
- }
- temp1.children.push(objTemp)
- }
- arr.push(temp1)
- }
- setOptionsCity(arr)
- }
- }, [])
- useEffect(() => {
- getCityFu()
- }, [getCityFu])
- const dispatch = useDispatch()
- // 筛选和分页
- const [tableSelect, setTableSelect] = useState(baseTableSelect)
- const tableSelectRef = useRef({} as A2FromDataType)
- useEffect(() => {
- tableSelectRef.current = { ...tableSelect }
- }, [tableSelect])
- // 点击搜索的 时间戳
- const [timeKey, setTimeKey] = useState(-1)
- // 点击搜索
- const clickSearch = useCallback(() => {
- setTableSelect({ ...tableSelect, pageNum: 1 })
- setTimeout(() => {
- setTimeKey(Date.now())
- }, 50)
- }, [tableSelect])
- // 发送接口的函数
- const getListFu = useCallback(() => {
- const objTemp: any = {}
- if (tableSelectRef.current.siteArr) {
- const temp = tableSelectRef.current.siteArr
- objTemp.province = temp[0] || ''
- objTemp.city = temp[1] || ''
- objTemp.region = temp[2] || ''
- }
- const obj = {
- ...tableSelectRef.current,
- ...objTemp
- }
- dispatch(A2_APIgetlist(obj))
- }, [dispatch])
- useEffect(() => {
- // 从进度统计进来------带入参数
- const urlStr = decodeURI(window.location.href)
- if (urlStr.includes('?C=')) {
- const path = urlStr.split('?C=')[1]
- const pathArr = path.split('/')
- const obj: A2FromDataType = {
- ...baseTableSelect,
- siteArr: pathArr
- }
- setTableSelect(obj)
- // 注意 这里有个坑。 setTableSelect 不能直接设置 province 和 city
- tableSelectRef.current = { ...obj, province: pathArr[0], city: pathArr[1] || '' }
- }
- if (urlStr.includes('?N=')) {
- const obj: A2FromDataType = {
- ...baseTableSelect,
- isSiteEmpty: 1
- }
- setTableSelect(obj)
- tableSelectRef.current = obj
- }
- getListFu()
- }, [getListFu])
- useEffect(() => {
- if (timeKey !== -1) getListFu()
- }, [getListFu, timeKey])
- // 输入框的改变
- const txtChangeFu = useCallback(
- (txt: string, key: 'pmUser' | 'searchKey') => {
- setTableSelect({ ...tableSelect, [key]: txt })
- },
- [tableSelect]
- )
- // 点击重置
- const [inputKey, setInputKey] = useState(1)
- const resetSelectFu = useCallback(() => {
- // 把2个输入框和时间选择器清空
- setInputKey(Date.now())
- setTableSelect(baseTableSelect)
- setTimeout(() => {
- setTimeKey(Date.now())
- }, 50)
- }, [])
- // 从仓库获取列表
- const A2TableList = useSelector((state: RootState) => state.A2Psychz.A2TableList)
- // 页码变化
- const paginationChange = useCallback(
- () => (pageNum: number, pageSize: number) => {
- setTableSelect({ ...tableSelect, pageNum, pageSize })
- setTimeout(() => {
- setTimeKey(Date.now())
- }, 50)
- },
- [tableSelect]
- )
- // 点击删除
- const delByIdFu = useCallback(
- async (id: number) => {
- const res = await A2_APIremoves(id)
- if (res.code === 0) {
- MessageFu.success('删除成功!')
- getListFu()
- getCityFu()
- }
- },
- [getCityFu, getListFu]
- )
- const columns = useMemo(() => {
- return [
- {
- title: '站址地区',
- render: (item: A2tableType) =>
- !item.province && !item.city && !item.region
- ? '(空)'
- : `${item.province}-${item.city}-${item.region}`
- },
- {
- title: '详细地址',
- render: (item: A2tableType) =>
- item.address ? (
- item.address.length >= 25 ? (
- <span style={{ cursor: 'pointer' }} title={item.address}>
- {item.address.substring(0, 25) + '...'}
- </span>
- ) : (
- item.address
- )
- ) : (
- '(空)'
- )
- },
- {
- title: '站址名称',
- render: (item: A2tableType) => item.name || '(空)'
- },
- {
- title: '站址编号',
- render: (item: A2tableType) => item.siteNum || '(空)'
- },
- {
- title: '机房编码',
- dataIndex: 'roomNum'
- },
- {
- title: '项目经理',
- render: (item: A2tableType) => {
- if (item.pmUserId === 1) return '管理员'
- else {
- return item.pmName || '(空)'
- }
- }
- },
- {
- title: '最近编辑时间',
- dataIndex: 'updateTime'
- },
- {
- title: '录入方式',
- render: (item: A2tableType) =>
- item.typeIn === 'pc' ? '系统' : item.typeIn === 'app-scan' ? '移动端扫码' : '移动端手工'
- },
- {
- title: '操作',
- render: (item: A2tableType) => (
- <>
- <Button
- size='small'
- type='text'
- onClick={() => setOpenInfo({ id: item.id, txt: '编辑' })}
- >
- 编辑
- </Button>
- <Popconfirm
- title='删除后无法恢复,是否删除?'
- okText='删除'
- cancelText='取消'
- onConfirm={() => delByIdFu(item.id)}
- okButtonProps={{ loading: false }}
- >
- <Button size='small' type='text' danger>
- 删除
- </Button>
- </Popconfirm>
- </>
- )
- }
- ]
- }, [delByIdFu])
- const [openInfo, setOpenInfo] = useState<A1addType>({ id: 0, txt: '' })
- // 点击导出
- const deriveFu = useCallback(async () => {
- if (A2TableList.total > 30000)
- return MessageFu.warning('只支持导出最多30000条数据。请增加筛选条件,并重新尝试')
- if (A2TableList.list.length === 0) return MessageFu.warning('当前搜索条件没有数据!')
- const name = '机房管理' + dayjs(new Date()).format('YYYY-MM-DD HH:mm')
- const objTemp: any = {}
- if (tableSelectRef.current.siteArr) {
- const temp = tableSelectRef.current.siteArr
- objTemp.province = temp[0] || ''
- objTemp.city = temp[1] || ''
- objTemp.region = temp[2] || ''
- }
- const res = await A2_APIgetlistDerive({
- ...tableSelectRef.current,
- ...objTemp,
- pageNum: 1,
- pageSize: 99999
- })
- if (res.code === 0) {
- if (res.data.records.length <= 0) return MessageFu.warning('当前搜索条件没有数据!')
- const option = {
- fileName: name,
- datas: [
- {
- sheetData: res.data.records.map((v: A2tableType) => ({
- ...v,
- myCity:
- !v.province && !v.city && !v.region
- ? '(空)'
- : `${v.province}-${v.city}-${v.region}`,
- address: v.address || '(空)',
- name: v.name || '(空)',
- siteNum: v.siteNum || '(空)',
- pmName: v.pmUserId === 1 ? '管理员' : v.pmName || '(空)',
- typeIn:
- v.typeIn === 'pc' ? '系统' : v.typeIn === 'app-scan' ? '移动端扫码' : '移动端手工'
- })),
- sheetName: name,
- sheetFilter: [
- 'myCity',
- 'address',
- 'name',
- 'siteNum',
- 'roomNum',
- 'pmName',
- 'updateTime',
- 'typeIn'
- ],
- sheetHeader: [
- '站址地区',
- '详细地址',
- '站址名称',
- '站址编号',
- '机房编码',
- '项目经理',
- '最近编辑时间',
- '录入方式'
- ],
- columnWidths: [10, 10, 10, 10, 10, 10, 10, 10]
- }
- ]
- }
- const toExcel = new ExportJsonExcel(option) //new
- toExcel.saveExcel() //保存
- }
- }, [A2TableList.list.length, A2TableList.total])
- return (
- <div className={styles.A2Psychz}>
- <div className='pageTitle'>
- {openInfo.txt === '新增' ? '新增机房' : openInfo.txt === '编辑' ? '编辑机房' : '机房管理'}
- </div>
- {/* 顶部筛选 */}
- <div className='A2top'>
- {/* 左侧输入框 */}
- <div className='A2top1'>
- <div className='A2topRow'>
- <span>项目经理:</span>
- <Input
- key={inputKey}
- maxLength={10}
- style={{ width: 240 }}
- placeholder='请输入姓名,最多10字'
- allowClear
- onChange={e => txtChangeFu(e.target.value, 'pmUser')}
- />
- </div>
- <div className='A2topRow'>
- <span>站址地区:</span>
- <Cascader
- disabled={!!tableSelect.isSiteEmpty}
- changeOnSelect
- style={{ width: 308 }}
- options={optionsCity}
- value={tableSelect.siteArr}
- placeholder='全部'
- onChange={e => {
- setTableSelect({
- ...tableSelect,
- siteArr: e as string[]
- })
- }}
- />
- </div>
- <div className='A2topRow'>
- <Checkbox
- checked={!!tableSelect.isSiteEmpty}
- onChange={e =>
- setTableSelect({
- ...tableSelect,
- siteArr: e.target.checked ? undefined : tableSelect.siteArr,
- isSiteEmpty: e.target.checked ? 1 : 0
- })
- }
- >
- 仅查看站址地区为空的场景
- </Checkbox>
- </div>
- </div>
- <div className='A2top2'>
- <div>
- <div className='A2topRow'>
- <span>录入方式:</span>
- <Select
- style={{ width: 240 }}
- value={tableSelect.typeIn}
- onChange={e => setTableSelect({ ...tableSelect, typeIn: e })}
- options={options1}
- />
- </div>
- <div className='A2topRow'>
- <span>搜索项:</span>
- <Input
- key={inputKey}
- maxLength={24}
- style={{ width: 322 }}
- placeholder='请输入站址名称/站置编号/机房编码,最多24字'
- allowClear
- onChange={e => txtChangeFu(e.target.value, 'searchKey')}
- />
- </div>
- </div>
- {/* 按钮 */}
- <div>
- <Button onClick={resetSelectFu}>重置</Button> 
- <Button type='primary' onClick={clickSearch}>
- 查询
- </Button>
-  
- <Button type='primary' onClick={() => setOpenInfo({ id: -1, txt: '新增' })}>
- 新增
- </Button>
-  
- <Button type='primary' onClick={deriveFu}>
- 导出表格
- </Button>
- </div>
- </div>
- </div>
- {/* 表格主体 */}
- <div className='tableMain'>
- <Table
- scroll={{ y: 578 }}
- dataSource={A2TableList.list}
- columns={columns}
- rowKey='id'
- pagination={{
- showQuickJumper: true,
- position: ['bottomCenter'],
- showSizeChanger: true,
- current: tableSelect.pageNum,
- pageSize: tableSelect.pageSize,
- total: A2TableList.total,
- onChange: paginationChange()
- }}
- />
- </div>
- {/* 新增和编辑出来的页面 */}
- {openInfo.id ? (
- <AddPsychz
- openInfo={openInfo}
- closeFu={() => setOpenInfo({ id: 0, txt: '' })}
- upTableFu={() => {
- getListFu()
- getCityFu()
- }}
- addTableFu={() => {
- resetSelectFu()
- getCityFu()
- }}
- />
- ) : null}
- {/* 右下角的列表数量 */}
- <div className='tableNumBox'>
- 共 <span>{A2TableList.total}</span> 条数据
- </div>
- </div>
- )
- }
- const MemoA2Psychz = React.memo(A2Psychz)
- export default MemoA2Psychz
|