index.js 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
import React, { useState, useImperativeHandle, forwardRef, useMemo, useRef } from 'react'
import { Select, message } from 'antd'
import Drag from '../../../components/Drag'
import FormRender from '../../../FormRender'
import { isJson, isObject } from '../../../../utils/index'
import { BatchEditTableDataInfo } from '../../../../apis/process'

const BatchEdit = (props, ref) => {

  useImperativeHandle(ref, () => ({
    open,
  }))

田翔's avatar
田翔 committed
14
  const { config, timeLimit } = props
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
  const { editFieldGroup, formJson } = config
  const [value, setValue] = useState('')
  const [params, setParams] = useState({ accountTable: '', id: '' })
  const [visible, setVisible] = useState(false)
  const [confirmLoading, setConfirmLoading] = useState(false)
  const formRenderRef = useRef()

  const filedArr = useMemo(() => {
    let array = editFieldGroup ? editFieldGroup.split(',').filter(v => v) : []
    setValue(array.length ? array[0] : '')
    return array
  }, [editFieldGroup])

  const schemaValues = useMemo(() => {
    let properties = {}
    let schema = formJson.properties
    if (isObject(schema)) {
      Object.keys(schema).forEach(v => {
        if (isObject(schema[v]?.properties)) {
          Object.keys(schema[v].properties).forEach(s => {
            if (s === value) {
              properties = {
                [v]: {
                  ...schema[v],
                  properties: {
                    [s]: schema[v].properties[s]
                  }
                }
              }
            }
          })
        }
      })
    }
    return {
      formJson: {
        ...formJson,
        column: 1,
        properties: properties
      },
      values: []
    }
  }, [editFieldGroup, formJson, value])

  const onSelect = (value) => {
    setValue(value)
  }

  const open = (values) => {
    setParams(values)
    setVisible(true)
  }

  const onOk = async () => {
    setConfirmLoading(true)
    const { formValue, errors } = await formRenderRef.current.getValues()
田翔's avatar
田翔 committed
71
    let values = formValue.filter(v => v.fieldName === value) || []
72 73 74
    if (errors.length) {
      return message.error('请完善表单内容')
    }
田翔's avatar
田翔 committed
75
    const { code, data, msg } = await BatchEditTableDataInfo({ ...params, values: values, timeLimit: timeLimit ? Number(timeLimit) : 0 })
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
    if (code === 0) {
      message.success('批量修改成功!')
      setVisible(false)
      props.onOk && props.onOk()
    } else {
      message.error(msg)
    }
    setConfirmLoading(false)
  }

  return (
    <Drag
      width={700}
      title='批量修改'
      visible={visible}
      bodyStyle={{ height: '300px', overflow: 'auto' }}
      onOk={onOk}
      onCancel={() => setVisible(false)}
      confirmLoading={confirmLoading}
      getContainer={false}
      destroyOnClose
    >
      <div style={{ display: 'flex', alignItems: 'center' }}>
        列字段选择:
        <Select
          style={{ minWidth: '250px' }}
          placeholder='请选择字段'
          showArrow
          showSearch
          value={value}
          onSelect={onSelect}
        >
          {filedArr.map(v => <Select.Option key={v} value={v}>{v}</Select.Option>)}
        </Select>
      </div>
      <div style={{ marginTop: '10px' }}>
        <FormRender schemaValues={schemaValues} ref={formRenderRef} />
      </div>
    </Drag>
  )

}

export default forwardRef(BatchEdit)