AddModal.jsx 14 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
2
import { Form, Modal, Input, Select, notification, DatePicker, Spin, Button, Card, Row, Col } from 'antd';
3 4 5 6 7 8 9 10 11 12
import styles from '../policiesIssued.less'
// import moment from 'moment';
// import locale from 'antd/es/date-picker/locale/zh_CN';
// import 'moment/locale/zh-cn';

import moment from 'moment'
import locale from 'antd/lib/date-picker/locale/zh_CN'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')
import {
13
  getEquipmentInfo, getQutaList, addStrategyOptions, editStrategyOptions, getPredictDevice, getPredictDevicePoint, getPredictInfo
14
} from '@/services/intelligence/api';
15 16
import { DeleteOutlined } from '@ant-design/icons';

17 18 19 20 21 22 23 24


const { TextArea } = Input;
const AddModal = props => {
  const { callBackSubmit = () => { }, type, formObj, visible, deviceType } = props;
  const [loading, setLoading] = useState(false);
  const [isloading, setIsLoading] = useState(false);
  const [deviceCodes, setDeviceCode] = useState([]); //设备编码列表
25 26 27
  const [predictCode, setPredictCode] = useState([]); //预测编码列表
  const [deviceList, setDeviceList] = useState([]); //设备列表
  const [card, setCard] = useState([]); //卡片列表值
28 29
  const [source, setSource] = useState([])
  const [PredictID, setPredictID] = useState()
30 31 32 33 34 35 36
  const [form] = Form.useForm();
  const { Item } = Form;
  // 提交
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let obj = form.getFieldsValue();
37 38 39 40 41 42 43
        let api = addStrategyOptions
        let query = {
          DeviceType: obj.DeviceType,
          StrategyDes: obj.StrategyDes,
          StrategyName: obj.StrategyName,
          StrategyType: obj.StrategyType,
          DetailInfos: card,
44 45
          CreateTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          PredictID
46
        }
47 48 49 50 51 52 53 54 55 56 57 58 59 60
        type === 'edit' ? (query.ID = formObj.ID) && (api = editStrategyOptions) : delete query.ID && (api = addStrategyOptions)
        setLoading(true);
        api(query).then(res => {
          setLoading(false);
          if (res.errMsg === '') {
            form.resetFields();
            callBackSubmit();
            prompt('success', `${type === 'add' ? "增加成功" : res.errMsg}`)
          } else {
            prompt('fail', `${type === 'add' ? "增加失败" : res.errMsg}`)
          }
        }).catch(e => {
          setLoading(false);
        })
61 62 63
      }
    });
  };
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  const prompt = (type, content) => {
    if (type == 'success') {
      notification.success({
        message: '提示',
        duration: 3,
        description: content,
      });
    }
    else {
      notification.error({
        message: '提示',
        duration: 3,
        description: content,
      });
    }
  }
80 81 82 83
  const onFinish = value => { };
  useEffect(() => {
    switch (type) {
      case 'add':
84
        initData()
85 86
        break;
      case 'edit':
87
        initData()
88 89 90 91 92 93
        break;
      default:
        break;
    }
  }, [visible]);

94 95
  //页面初始化
  const initData = () => {
96 97 98
    getPredictInfo().then(res => {
      res.getMe && res.getMe.length && setSource(res.getMe)
    })
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
    if (type === 'add') {
      form.resetFields();
      setCard([])
      setDeviceList([])
    }
    else {
      form.setFieldsValue({ time: moment(formObj.CreateTime, "YYYYMMDDHHmmss"), ...formObj });
      if (formObj.DetailInfos.length) {
        let req1 = getEquipmentInfo(formObj.DeviceType)
        let req2 = getPredictDevice(formObj.DeviceType)
        Promise.all([req1, req2]).then(respone => {
          console.log('respone', respone);
          respone[0].getMe.length && respone[0].getMe[0].DeviceList.length && setDeviceCode(respone[0].getMe[0].DeviceList)
          respone[1].getMe.length && setPredictCode(respone[1].getMe)
          let arr = []
          formObj.DetailInfos.map((item, index) => {
            console.log(1);
            arr[index] = {}
            arr[index].code = respone[0].getMe.length ? respone[0].getMe[0].DeviceList : []
            arr[index].predictCode = respone[1].getMe.length ? respone[1].getMe : []
            form.setFieldsValue({
              [`deviceCode${index}`]: item.ControlCode,
              [`indextr${index}`]: item.ControlPoint,
              [`max${index}`]: item.PredictCode,
              [`min${index}`]: item.PredictPoint
            })
          })
          setCard(formObj.DetailInfos)
          setDeviceList(arr)
          setLoading(false)
        })
      }
131

132
    }
133

134 135 136
  }
  //获取设备编码
  const getDeviceCode = () => {
137
    setLoading(false)
138 139 140 141 142 143 144 145 146 147 148 149 150
    let obj = form.getFieldsValue();
    let req1 = getEquipmentInfo(obj.DeviceType)
    let req2 = getPredictDevice(obj.DeviceType)
    Promise.all([req1, req2]).then(respone => {
      respone[0].getMe.length && respone[0].getMe[0].DeviceList.length && setDeviceCode(respone[0].getMe[0].DeviceList)
      respone[1].getMe.length && setPredictCode(respone[1].getMe)
      if (deviceList.length) {
        let arr = [...deviceList]
        arr.map((item, index) => {
          arr[index].code = respone[0].getMe.length ? respone[0].getMe[0].DeviceList : []
          arr[index].predictCode = respone[1].getMe.length ? respone[1].getMe : []
          arr[index].indexStr = ''
          arr[index].predictStr = ''
151
          clearInput(index)
152 153 154
        })
        setDeviceList(arr)
      }
155 156
      setLoading(false)
    })
157

158 159 160 161
  }
  const layout = {
    layout: 'horizontal',
    labelCol: {
162
      span: 6,
163 164 165 166 167 168 169
    },
    wrapperCol: {
      span: 16,
    },
  };
  //选择设备类型
  const handleDeviceType = (value) => {
170 171 172 173 174 175 176 177 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 207 208 209 210 211 212
    getDeviceCode()
  }

  //选择设备编码
  const handleChange = (value, option) => {
    let cardList = [...card]
    cardList[+option.index].ControlCode = value
    setCard(cardList)
    let obj = form.getFieldsValue();
    getQutaList({ accountName: obj.DeviceType, AddrSchemeID: option.pointid }).then(res => {
      if (res.getMe && res.getMe.length) {
        let arr = [...deviceList]
        arr[+option.index].indexStr = res.getMe
        setDeviceList(arr)
      }
    })
  }
  //选择预测编码
  const handleChange1 = (value, option) => {
    let cardList = [...card]
    cardList[+option.index].PredictCode = value
    setCard(cardList)
    let obj = form.getFieldsValue();
    getPredictDevicePoint({ deviceType: obj.DeviceType, DeviceCode: value }).then(res => {
      console.log('res', res);
      if (res.getMe && res.getMe.length) {
        let arr = [...deviceList]
        arr[+option.index].predictStr = res.getMe
        setDeviceList(arr)
      }
    })
  }
  //选择设备指标
  const handleChangeIndex = (value, option) => {
    let cardList = [...card]
    cardList[+option.index].ControlPoint = value
    setCard(cardList)
  }
  //选择预测指标
  const handleChangeIndex1 = (value, option) => {
    let cardList = [...card]
    cardList[+option.index].PredictPoint = value
    setCard(cardList)
213
  }
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  const add = () => {
    let obj = form.getFieldsValue();
    if (!obj.DeviceType) {
      notification.error({
        message: '提示',
        duration: 3,
        description: '请选择设备类型',
      });
    } else {
      let cardList = [...card]
      let arr = [...deviceList]
      cardList.push({})
      setCard(cardList)
      arr.push({ code: deviceCodes, predictCode })
      setDeviceList(arr)
    }
  }
232 233 234 235 236 237 238 239 240
  //清除卡片删除一项的值
  const clearInput = (index) => {
    form.setFieldsValue({
      [`deviceCode${index}`]: undefined,
      [`indextr${index}`]: undefined,
      [`max${index}`]: undefined,
      [`min${index}`]: undefined
    })
  }
241 242 243 244
  //删除卡片
  const deleteDevice = (index) => {
    let cardList = [...card]
    let arr = [...deviceList]
245 246 247 248 249 250 251 252
    cardList.splice(index, 1)
    arr.splice(index, 1)
    clearInput(index)
    setCard(cardList)
    setDeviceList(arr)
  }
  const handleOrgin =(value,option) =>{
    setPredictID(option.id)
253 254 255 256
  }

  return (
    <Modal
257
      title={`${type === 'add' ? '策略下发新增' : '编辑'}`}
258 259
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: '150px' }}
260
      width="800px"
261 262 263 264 265 266 267 268 269 270 271 272 273 274
      destroyOnClose
      maskClosable={false}
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      confirmLoading={loading}
      forceRender={true}
      getContainer={false}
    >
      <Spin spinning={isloading} delay={300}>
        {visible && (
          <Form form={form} {...layout} onFinish={onFinish}>
            <Item
275 276
              label="策略名称"
              name="StrategyName"
277
              rules={[{ required: true, message: '请输入策略名称' }]}
278 279 280 281
            >
              <Input placeholder="请输入任务名称" allowClear />
            </Item>
            <Item
282 283
              label="策略类型"
              name="StrategyType"
284
              rules={[{ required: true, message: '请选择策略类型' }]}
285 286
            >
              <Select >
287 288
                <Select.Option value='实时控制'>实时控制</Select.Option>
                <Select.Option value='时段控制'>时段控制</Select.Option>
289 290 291

              </Select>
            </Item>
292 293 294 295 296 297 298 299 300
            <Item
              label="数据来源"
              name="PredictName"
              rules={[{ required: true, message: '请选择数据来源' }]}
            >
              <Select  onChange={(value, option) => handleOrgin(value, option)}>
                {source.length ? source.map((item, index) => { return <Select.Option key={index} id={item.ID} value={item.PredictName}>{item.PredictName}</Select.Option> }) : ''}
              </Select>
            </Item>
301 302
            <Item
              label="设备类型"
303
              name="DeviceType"
304 305 306 307 308 309 310
            >
              <Select onChange={handleDeviceType} >
                {deviceType.length ? deviceType.map((item, index) => { return <Select.Option key={index} value={item.DeviceType}>{item.DeviceType}</Select.Option> }) : ''}
              </Select>
            </Item>
            <Item
              label="描述"
311
              name="StrategyDes"
312 313 314
            >
              <TextArea placeholder="可输入描述信息" rows={4} allowClear />
            </Item>
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
            <Button size="small" onClick={() => { add() }}>
              新增
            </Button>
            <div className={styles.deviceList}>
              {deviceList.length ? deviceList.map((item, index) => {
                return <Card key={index} style={{ marginTop: '0.5rem' }}>
                  <Row gutter='36' justify='space-between'>
                    <Col span={12}>
                      <Item
                        label="控制编码"
                        name={'deviceCode' + index}
                        rules={[{ required: true, message: '请选择设备编码' }]}
                      >
                        <Select
                          placeholder="请选择控制编码"
                          onChange={(value, option) => handleChange(value, option)}
                        >
                          {item.code.length ? item.code.map((item, codeIndex) => { return <Select.Option index={index} key={codeIndex} value={item.Code} pointid={item.PointAddressID}>{item.Code}</Select.Option> }) : ''}
                        </Select>
                      </Item>
                    </Col>
                    <Col span={12}>
                      <Item
                        label="控制指标"
                        name={'indextr' + index}
                        rules={[{ required: true, message: '请选择指标名称' }]}
                      >
                        <Select
                          placeholder="请选择控制指标"
                          onChange={(value, option) => handleChangeIndex(value, option)}
                        >
                          {item.indexStr && item.indexStr.length ? item.indexStr.map((item, zindex) => { return <Select.Option key={zindex} index={index} value={item.Name}>{item.Name}</Select.Option> }) : ''}
                        </Select>
                      </Item>
                    </Col>
                  </Row>
                  <Row gutter='24' justify='space-between'>
                    <Col span={12}>
                      <Item
                        label="预测编码"
                        name={'max' + index}
                        rules={[{ required: true, message: '请输入预测编码' }]}
                      >
                        <Select
                          placeholder="请选择预测编码"
                          onChange={(value, option) => handleChange1(value, option)}
                        >
                          {item.predictCode.length ? item.predictCode.map((item, codeIndex) => { return <Select.Option index={index} key={codeIndex} value={item} >{item}</Select.Option> }) : ''}
                        </Select>
                        {/* <Input placeholder="请输入最大值" onChange={(value) => inputMax(value, index)} index={index} allowClear /> */}
                      </Item>
                    </Col>
                    <Col span={12}>
                      <Item
                        label="预测指标"
                        name={'min' + index}
                        rules={[{ required: true, message: '请输入预测指标' }]}
                      >
                        <Select
                          placeholder="请选择预测指标"
                          onChange={(value, option) => handleChangeIndex1(value, option)}
                        >
                          {item.predictStr && item.predictStr.length ? item.predictStr.map((item, zindex) => { return <Select.Option key={zindex} index={index} value={item}>{item}</Select.Option> }) : ''}
                        </Select>
                      </Item>
                    </Col>
                  </Row>
                  <DeleteOutlined
                    className={styles.deleteItem}
                    onClick={() => deleteDevice(index)}
                    style={{ fontSize: '16px', color: '#e86060' }}
                  />
                </Card>
              }) : ''}
            </div>
390 391 392 393 394 395 396 397
          </Form>
        )}
      </Spin>

    </Modal>
  );
};
export default AddModal;