AddModal.jsx 8.62 KB
Newer Older
邓超's avatar
邓超 committed
1 2 3 4 5
import React, { useEffect, useState } from 'react';
import {
  flowReloadFlowNodes,
  reloadTimeLimitadFlowNodes,
  operateFlowTimer,
6
  reloadFlowNodes,
邓超's avatar
邓超 committed
7
} from '@/services/flow/flow';
邓超's avatar
邓超 committed
8 9 10 11
import { Form, Modal, Input, notification, Select } from 'antd';
const { Option } = Select;

const AddModal = props => {
12
  const { onSubumit, handleCancel, visible, msg, flowId, modalType, title } = props;
邓超's avatar
邓超 committed
13 14
  const [flowNodes, setFlowNodes] = useState([]);
  const [timeLimitFlowNodes, setTimeLimitFlowNodes] = useState([]);
15
  const [timeLimitFlowNodesEnd, setTimeLimitFlowNodesEnd] = useState([]);
邓超's avatar
邓超 committed
16 17
  const [startNodeIndex, setStartNodeIndex] = useState(null);
  const [endNodeIndex, setEndNodeIndex] = useState(null);
18 19
  const [keepTableName, setKeepTableName] = useState('');
  const [keepData, setKeepData] = useState('');
邓超's avatar
邓超 committed
20 21
  const [form] = Form.useForm();
  useEffect(() => {
22
    console.log(title);
邓超's avatar
邓超 committed
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
    form.resetFields();
    setStartNodeIndex(null);
    setEndNodeIndex(null);
    if (visible) {
      getFlowNodes();
      if (modalType === 'edit') {
        getFormData();
      } else {
        form.setFieldsValue({ FlowName: title });
      }
    }
  }, [visible]);
  // 获取到节点后存入当前选中对应的索引用于限制选择节点
  useEffect(() => {
    if (flowNodes.length > 0) {
      flowNodes.forEach((item, index) => {
        if (item.Name === msg.StartNode && modalType === 'edit') {
          setStartNodeIndex(index);
        }
        if (item.Name === msg.EndNode && modalType === 'edit') {
          setEndNodeIndex(index);
        }
      });
    }
  }, [flowNodes]);
  // 根据下拉框选择的流程节点关联的表名加载指派字段
49 50
  const getLimitadFlowNodes = e => {
    reloadTimeLimitadFlowNodes({ flowNodeTableName: e }).then(res => {
邓超's avatar
邓超 committed
51 52 53 54 55
      if (res.code === 0) {
        setTimeLimitFlowNodes(res.data);
      }
    });
  };
56 57 58 59 60 61 62 63

  const getLimitadFlowNodesEnd = e => {
    reloadTimeLimitadFlowNodes({ flowNodeTableName: e }).then(res => {
      if (res.code === 0) {
        setTimeLimitFlowNodesEnd(res.data);
      }
    });
  };
邓超's avatar
邓超 committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  // 根据流程ID加载起止节点和终止节点
  const getFlowNodes = () => {
    flowReloadFlowNodes({ flowId }).then(res => {
      if (res.code === 0) {
        setFlowNodes(res.data);
      }
    });
  };
  // 获取表单回显
  const getFormData = () => {
    console.log(msg);
    form.setFieldsValue({ ...msg, FlowName: title });
  };
  // 表单监听
  const onValuesChange = val => {
79 80 81
    console.log(val);
    console.log(Object.keys(val)[0]);
    console.log(flowNodes);
邓超's avatar
邓超 committed
82
    if (Object.keys(val)[0] === 'StartNode') {
83 84 85 86 87 88 89 90 91 92
      reloadFlowNodes({ flowName: title }).then(res => {
        if (res.code === 0) {
          console.log(res.data[0].Items);
          let aa = res.data[0].Items.find(i => i.name == val.StartNode);
          console.log(val.StartNode);
          console.log(aa.extendTableName);
          getLimitadFlowNodes(aa.extendTableName);
          // 存入需要展开得节点
        }
      });
邓超's avatar
邓超 committed
93 94 95 96 97 98 99
      flowNodes.forEach((item, index) => {
        if (item.Name === val.StartNode) {
          setStartNodeIndex(index);
        }
      });
    }
    if (Object.keys(val)[0] === 'EndNode') {
100 101 102 103 104 105 106 107 108 109
      reloadFlowNodes({ flowName: title }).then(res => {
        if (res.code === 0) {
          console.log(res.data[0].Items);
          let aa = res.data[0].Items.find(i => i.name == val.EndNode);
          console.log(val.EndNode);
          console.log(aa.extendTableName);
          getLimitadFlowNodesEnd(aa.extendTableName);
          // 存入需要展开得节点
        }
      });
邓超's avatar
邓超 committed
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
      flowNodes.forEach((item, index) => {
        if (item.Name === val.EndNode) {
          setEndNodeIndex(index);
        }
      });
    }
  };
  // 提交表单
  const onFinish = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let obj = {};
        console.log(modalType);
        if (modalType === 'add') {
          obj = { ...validate, ID: 0 };
        } else {
          obj = { ...validate, ID: msg.ID };
        }
        operateFlowTimer(obj)
          .then(res => {
            if (res.code === 0) {
              notification.success({
                message: '提示',
                duration: 3,
                description: '编辑成功',
              });
              onSubumit();
            } else {
              notification.error({
                message: '提示',
                duration: 3,
                description: res.msg,
              });
            }
          })
          .catch(() => {
            notification.error({
              message: '提示',
              duration: 3,
              description: '网络异常',
            });
          });
      }
    });
  };
  return (
    <Modal
      title="流程节点辅助视图配置"
      visible={visible}
      onOk={onFinish}
      onCancel={handleCancel}
      maskClosable={false}
      destroyOnClose
    >
      <Form
        form={form}
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 18 }}
        initialValues={{ remember: true }}
        onValuesChange={onValuesChange}
      >
        <Form.Item label="流程名称" name="FlowName">
          <Input disabled />
        </Form.Item>
        <Form.Item label="规则名称" name="Name" rules={[{ required: true }]}>
          <Input placeholder="请输入规则名称" />
        </Form.Item>
177
        <Form.Item label="起止节点" style={{ marginBottom: 0, message: '请选择节点' }} required>
邓超's avatar
邓超 committed
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
          <div style={{ display: 'flex' }}>
            <Form.Item
              name="StartNode"
              style={{ width: '100%' }}
              rules={[{ required: true, message: '请选择节点' }]}
            >
              <Select>
                {flowNodes.map((item, index) => (
                  <Option
                    value={item.Name}
                    key={item.ID}
                    disabled={endNodeIndex !== null && index >= endNodeIndex}
                  >
                    {item.Name}
                  </Option>
                ))}
              </Select>
            </Form.Item>
            <span style={{ width: '40px', textAlign: 'center' }}>--</span>
            <Form.Item
              name="EndNode"
              style={{ width: '100%' }}
              rules={[{ required: true, message: '请选择节点' }]}
            >
              <Select>
                {flowNodes.map((item, index) => (
                  <Option
                    value={item.Name}
                    key={item.ID}
207
                    disabled={startNodeIndex !== null && index <= startNodeIndex}
邓超's avatar
邓超 committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                  >
                    {item.Name}
                  </Option>
                ))}
              </Select>
            </Form.Item>
          </div>
        </Form.Item>
        <Form.Item label="默认时限" style={{ marginBottom: 0 }} required>
          <div style={{ display: 'flex' }}>
            <Form.Item
              name="TimeLimitInt"
              style={{ marginRight: '18px', width: '100%' }}
              rules={[
                { required: true, message: '请选填写时限' },
                {
                  validator: (_, value) =>
225
                    value < 0 ? Promise.reject(new Error('默认时限需要大于零')) : Promise.resolve(),
邓超's avatar
邓超 committed
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
                },
              ]}
            >
              <Input placeholder="请输入默认时限" />
            </Form.Item>
            <Form.Item
              name="TimeUnit"
              style={{ width: '100%' }}
              rules={[{ required: true, message: '请选择时限单位' }]}
            >
              <Select>
                <Option value="小时">小时</Option>
                <Option value="自然日">自然日</Option>
                <Option value="工作日">工作日</Option>
              </Select>
            </Form.Item>
          </div>
        </Form.Item>
        <Form.Item
          label="时限指派字段"
          name="TimeLimitField"
          rules={[{ required: true, message: '请选择时限指派字段' }]}
        >
          <Select>
            {timeLimitFlowNodes.map(item => (
              <Option value={item.Name} key={item.ID}>
皮倩雯's avatar
皮倩雯 committed
252
                <span>{item.Name}</span>
邓超's avatar
邓超 committed
253 254 255 256 257 258 259 260 261 262
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label="超时记录字段"
          name="TimeoutField"
          rules={[{ required: true, message: '请选择超时记录字段' }]}
        >
          <Select>
263
            {timeLimitFlowNodesEnd.map(item => (
邓超's avatar
邓超 committed
264
              <Option value={item.Name} key={item.ID}>
皮倩雯's avatar
皮倩雯 committed
265
                <span>{item.Name}</span>
邓超's avatar
邓超 committed
266 267 268 269 270 271 272 273 274
              </Option>
            ))}
          </Select>
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default AddModal;