'use strict' import React, { useEffect, useState, useRef } from 'react'; import { connect } from 'react-redux'; import { Row, Form, Col, Select, Button, Notification } from '@douyinfe/semi-ui'; import moment from 'moment'; import AbnRecognize from './AbnRecognize' const methodType = { 'interrupt': 1, 'burr': 2, 'trend': 3, } const methodDes = { 'interrupt': "中断", 'burr': "毛刺", 'trend': "异常趋势", } const Calc = (props) => { const { actions, dispatch, pepProjectId, abnMethods, abnParam } = props const { install, data } = actions const [structId, setStructId] = useState(null) const [methodType, setMethodType] = useState('interrupt') const [loading, setLoading] = useState(false) const [structList, setStructList] = useState([])//结构物筛选列表 const [project, setProject] = useState('')//project(eg:{projcet:'nbjj'}) const [reCalcFactorId, setReCalcFactorId] = useState(null); const [msg, setMsg] = useState('') const [factosList, setFactorsList] = useState([])//监测因素列表 const [factorId, setFactorId] = useState(null) const [sensorList, setSensorList] = useState([])//设备列表 const [sensorId, setSensorId] = useState([])//设备id' const [itemId, setItemId] = useState(null)//监测项索引 const [itemName, setItemName] = useState('')//检测项name const form = useRef() const form2 = useRef() const method = { 'interrupt': 1, 'burr': 2, 'trend': 3, } //初始化 useEffect(() => { // getData() dispatch(install.getAbnMethods()) }, []) console.log('factore111',factorId) useEffect(() => { if(project&&factorId){ const id = [factorId, -1].join(',') dispatch(install.getAbnParamList({ factorId: id })) } }, [factorId,project]) //监听变化 useEffect(() => { setStructList([]) form.current.reset() form2.current.reset() form2.current.setValue('method', methodType) getData() }, [pepProjectId]) const checkInterger = (rule, val) => { // if(val){ const strRegex = /^[1-9]*[1-9][0-9]*$/ if (strRegex.test(val)) { return true } return false // } }; const checkNumber = (rule, val) => { const strRegex = /^-?\d+(\.\d+)?$/ if (strRegex.test(val)) { return true } return false } const checkPoint = (rule, value) => { const pattern = /^[1-9]*[1-9][0-9]*$/; if (pattern.test(value) && value != 1) { return true } return false } const getJson = (values) => { let paramJson; switch (methodType) { case 'interrupt': return paramJson = { "thr_int": Number(values.iv) }; case 'burr': return paramJson = { "thr_burr": Number(values.bv1) } case 'trend': return paramJson = { "thr_burr": Number(values.bv2),//毛刺阈值 "win_med": Number(values.ws),//滑动中值窗口 "win_avg": Number(values.rc),//滑动均值窗口 "win_grad": Number(values.pn),//渐变点个数 "thr_grad": Number(values.gv),//渐变阈值 "thr_der": Number(values.dv),//导数阈值 "days_Last": Number(values.time),//时间跨度 } } } const getStationstoSave = () => { let toSave = [], toSaveName = [], notToSave = [], notToSaveName = [] let selectIds = sensorId for (let i = 0; i < selectIds.length; i++) { let seStaName = sensorList.find(a => a.value == selectIds[i]).label let cfg if (methodType == "interrupt") { cfg = abnParam.find(a => a.abnType == 1 && a.factorId == factorId && a.sensorId == selectIds[i]) } else { let type = methodType == "burr" ? 2 : 3 cfg = abnParam.find(a => a.abnType == type && a.factorId == factorId && a.sensorId == selectIds[i]) } if (!cfg) { toSave.push(selectIds[i]) toSaveName.push(seStaName) } else { notToSave.push(selectIds[i]) notToSaveName.push(seStaName) } } return { toSave: toSave, toSaveName: toSaveName, notToSave: notToSave, notToSaveName: notToSaveName }; } //保存配置 const handleSave = () => { setMsg('') form2.current.validate().then(res => { if (sensorId.length != 0) { let ids = getStationstoSave() if (ids.toSave.length != 0) { let paramJson = getJson(res) let data = { sensorId:ids.toSave,//测点 sensorName: ids.toSaveName, factorId: factorId, factorName: factosList.find(a => a.value == factorId).label, abnType: method[methodType],//算法类型 enabled: true, params: paramJson, itemId: itemId, } let pushData = data.sensorId.map(d => { return { sensorId: project+':'+d,//测点 sensorName: sensorList.find(a => a.value == d).label, factorId: factorId, factorName: factosList.find(a => a.value == factorId).label, abnType: method[methodType],//算法类型 enabled: true, params: paramJson, itemId: methodType == 'interrupt' ? null : itemId } }) dispatch(install.addAbnParam(pushData)).then(res => { if (res.success) { const id = [factorId, -1].join(',') dispatch(install.getAbnParamList({ factorId: id })) } }) if (ids.notToSave.length != 0) { let fact = methodType == 'interrupt' ? "因素" : "子项" Notification.warning({ content: `已存在传感器: ${ids.notToSaveName.join(',')} 在当前监测${fact}下的${methodDes[methodType]}参数配置,本次保存的传感器为:${ids.toSaveName.join(',')}`, duration: 3, }) } } else { let sg = methodType == 'interrupt' ? "因素下的中断" : methodType == 'burr' ? "子项下的毛刺" : "子项下的异常趋势" Notification.warning({ content: `已存在选定测点在当前监测 ${sg} 参数配置,本次无可保存的参数配置`, duration: 3, }) } } else { Notification.warning({ content: `请选择测点`, duration: 3, }) return false } }) } //数据对比 const dataCompare = () => { // this.setState({ msg: '' }); // this.props.form.validateFields((err, values) => { // if (!err) { // if (this.props.stationsValue.length != 0) { // let paramJson = this.getJson(values); // let data = { // station: this.props.stationsValue[0],//第一个测点 // factorId: this.props.factorId, // itemId: this.props.itemId, // abnType: this.props.methodType,//算法类型 // enabled: true, // params: paramJson // }; // this.props.dataCompara(data); // } else { // message.error('请选择测点!'); // return false; // } // } // }); } //结构物筛选发生变化回调函数 const reCalcFactorChange = (value) => { setItemName(factosList?.find(item => item.value === value[0])?.children?.find(p => p.value === value[1])?.label) setItemId(value[1]) // form2.current.setValue('point', '') setFactorId(value[0]) } //点位筛选发生改变的回调函数 const pointChange = (value) => { setSensorId(value) // setProject(structList.find(item => item.value == structId)?.project) } //监听结构物变化,查询监测因素 useEffect(() => { setLoading(true); let queryParams = { structId, cacl: 1 } if (structId) { dispatch(data.getFactors({ ...queryParams })).then(res => { if (res.success) { const list = res.payload.data?.map(item => { const itemList = item.ItemNames && item.ItemNames.split(',') || [] let child = [] itemList && itemList.length && itemList.forEach((i, index) => { child.push({ label: i, value: index }) }) return { label: item.Name, value: item.FactorID, children: child } }) form2.current.setValue('factor', [list[0]?.value, list[0]?.children[0]?.value]) setFactorsList(list) setFactorId(list[0]?.value) setItemId(list[0]?.children[0]?.value) setItemName(list[0]?.children[0]?.label) setLoading(false) } }) } }, [structId]) useEffect(() => { let queryParams = { structId, SafetyFactorTypeId: factorId } if (factorId && structId) { dispatch(data.getSensors({ ...queryParams })).then(res => { if (res.success) { const list = res.payload.data?.map(item => { return { value: item.SensorId, label: item.SensorLocationDescription, } }) form2.current.setValue('point', [list[0]?.value]) setSensorId([list[0]?.value]) setSensorList(list) setLoading(false); setProject(res.payload.data[0].Project) } }) } }, [factorId]) //获取结构物函数 const getData = (queryParams = { pomsProjectId: pepProjectId }) => { setLoading(true); dispatch(data.getProjectAllStructures({ ...queryParams })).then(res => { if (res.success) { const uniqueIds = new Set(); const list = res.payload.data?.filter(item => { const duplicate = uniqueIds.has(item.strucId); uniqueIds.add(item.strucId); return !duplicate })?.map(item => ({ value: item.strucId, label: item.strucName, project: item.Project, })) form.current.setValue('struct', list[0]?.value) setStructId(list[0]?.value) setStructList(list) setLoading(false) } } ) } const changeMethod = (value) => { setMethodType(value) } const clearFactorAndSensor = () => { form2.current.setValue('point', '') form2.current.setValue('factor', '') } const structChange = (value) => { setStructId(value) clearFactorAndSensor() } const renderParamsConfig = () => { switch (methodType) { case 'interrupt': return (