complex.jsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import React, {
  2. useRef,
  3. useState,
  4. useEffect,
  5. forwardRef,
  6. useImperativeHandle,
  7. } from "react";
  8. import { Form, Input, Select, message } from "antd";
  9. import { Modal, Button, Table } from "antd";
  10. import Formular from "./components/Formular";
  11. import API2 from "../../../api/alarm";
  12. import styles from "./complex.module.less";
  13. import oneNode from "./components/oneNode";
  14. const { Search } = Input;
  15. function TranslateText(arr) {
  16. const dtLanguage = localStorage.getItem("dtLanguage");
  17. if (!Array.isArray(arr)) {
  18. return arr;
  19. }
  20. if (arr.length < 2 && arr.length > 0) {
  21. return arr?.[0];
  22. }
  23. if (dtLanguage === "en") {
  24. return arr?.[1];
  25. }
  26. return arr?.[0];
  27. }
  28. const Complex = (props, ref) => {
  29. const { alarmId, groupList = [] } = props;
  30. const [form] = Form.useForm();
  31. const formuRef = useRef();
  32. // const [alarmId, setAlarmId] = useState(searchParams.get('alarm_id'))
  33. // const [token, setToken] = useState(searchParams.get('token'))
  34. // const [apiUrl, setApiUrl] = useState(decodeURIComponent(searchParams.get('api_url')) + '/')
  35. const [data, setData] = useState();
  36. const [loading, setLoading] = useState(false);
  37. const [deviceList, setDeviceList] = useState([]);
  38. const [paginationProps, setPaginationProps] = useState({
  39. pageSize: 10,
  40. current: 1,
  41. showTotal: (total) => `共${total}条`,
  42. // showQuickJumper: true,
  43. // showSizeChanger:true,
  44. // size:'Large',
  45. });
  46. const handleTableChange = async (pagination) => {
  47. setPaginationProps({ ...pagination, showTotal: (total) => `共${total}条` });
  48. // setCurrentPage(pagination.current);
  49. // setPageSize(pagination.pageSize)
  50. };
  51. useEffect(() => {
  52. onSearch("");
  53. }, []);
  54. useEffect(() => {
  55. async function run() {
  56. if (alarmId && alarmId != -1) {
  57. const { state, data } = await API2.detailAlarm(Number(alarmId));
  58. form.setFieldsValue({
  59. name: data.name,
  60. alarm_level: data.alarm_level,
  61. group_id: data.group_id,
  62. });
  63. formuRef.current.backEndInput(data.formula);
  64. setData(data);
  65. }
  66. }
  67. run();
  68. }, [alarmId]);
  69. const onModalOk = async () => {
  70. if (alarmId == -1) {
  71. const value = await form.validateFields();
  72. const obj = {
  73. ...value,
  74. formula: formuRef.current.getFinalStr(),
  75. ext_notify_list: [],
  76. is_complex: true,
  77. type: 1,
  78. sub_type: 12,
  79. status: 1,
  80. defer_unit: 2,
  81. };
  82. const { state } = await API2.addRule(obj);
  83. if (state === 0) {
  84. message.success("保存成功");
  85. props.onConfirm();
  86. }
  87. } else {
  88. const value = await form.validateFields();
  89. const obj = {
  90. ...data,
  91. ...value,
  92. formula: formuRef.current.getFinalStr(),
  93. ext_notify_list: [],
  94. };
  95. const { state } = await API2.editRule(obj);
  96. if (state === 0) {
  97. message.success("保存成功");
  98. props.onConfirm();
  99. }
  100. }
  101. form.resetFields();
  102. props.onConfirm();
  103. };
  104. useImperativeHandle(ref, () => {
  105. return {
  106. ok: onModalOk,
  107. };
  108. });
  109. const symbClick = (e) => {
  110. let innerText = e.target.innerText;
  111. if (innerText === "≥") {
  112. innerText = ">=";
  113. } else if (innerText === "≤") {
  114. innerText = "<=";
  115. } else if (innerText === "&&(and)") {
  116. innerText = "&&";
  117. } else if (innerText === "||(or)") {
  118. innerText = "||";
  119. }
  120. formuRef.current.dtInsertFormular(innerText);
  121. if (innerText === "()") {
  122. //判断为括号则把光标移到括号中间
  123. formuRef.current.moveInTextNode();
  124. }
  125. };
  126. const insertToFomular = (poi) => {
  127. const { point_id, name } = poi;
  128. const res = oneNode(point_id, name);
  129. formuRef.current.dtInsertFormular(res);
  130. };
  131. const columns = [
  132. {
  133. title: TranslateText(["点位编号", "Point id"]),
  134. dataIndex: "point_id",
  135. ellipsis: true,
  136. },
  137. {
  138. title: TranslateText(["点位名称", "Point name"]),
  139. dataIndex: "name",
  140. ellipsis: true,
  141. },
  142. ];
  143. const onSearch = async (v) => {
  144. const res = await API2.searchPoint({
  145. key_word: v ?? "",
  146. count: 50,
  147. type: 1,
  148. no_child: true,
  149. });
  150. if (res.state === 0) {
  151. setDeviceList(res.data || []);
  152. return;
  153. }
  154. setDeviceList([]);
  155. };
  156. const clickTableRow = (record) => {
  157. // child.current.handlePickPoint({name:record.name, point_id:record.point_id})
  158. insertToFomular({ name: record.name, point_id: record.point_id });
  159. };
  160. const onModalCancel = () => {
  161. props.onConfirm();
  162. form.resetFields();
  163. };
  164. return (
  165. <div className={styles.wrapper}>
  166. <div className="main">
  167. <div className="left">
  168. <Form
  169. name="basic"
  170. autoComplete="off"
  171. form={form}
  172. requiredMark={false}
  173. labelAlign="right"
  174. colon={false}
  175. labelCol={{
  176. span: 4,
  177. }}
  178. wrapperCol={{
  179. span: 15,
  180. }}
  181. >
  182. <Form.Item
  183. label={TranslateText(["告警名称", "Alarm name"])}
  184. name="name"
  185. rules={[
  186. {
  187. required: true,
  188. message: TranslateText(["请输入告警名称", "Please input"]),
  189. },
  190. ]}
  191. >
  192. <Input
  193. placeholder={TranslateText(["请输入告警名称", "Please input"])}
  194. style={{ width: "240px" }}
  195. />
  196. </Form.Item>
  197. <Form.Item
  198. label={TranslateText(["条件符号", "Symbol"])}
  199. // name="symb"
  200. >
  201. <div className="symb" style={{ width: "500px" }}>
  202. <Button type="primary" onClick={symbClick}>
  203. ()
  204. </Button>
  205. <Button type="primary" onClick={symbClick}>
  206. {"&&(and)"}
  207. </Button>
  208. <Button type="primary" onClick={symbClick}>
  209. {"||(or)"}
  210. </Button>
  211. <Button type="primary" onClick={symbClick}>
  212. </Button>
  213. <Button type="primary" onClick={symbClick}>
  214. {">"}
  215. </Button>
  216. <Button type="primary" onClick={symbClick}>
  217. =
  218. </Button>
  219. <Button type="primary" onClick={symbClick}>{`<`}</Button>
  220. <Button type="primary" onClick={symbClick}>
  221. </Button>
  222. <Button type="primary" onClick={symbClick}>
  223. +
  224. </Button>
  225. <Button type="primary" onClick={symbClick}>
  226. -
  227. </Button>
  228. <Button type="primary" onClick={symbClick}>
  229. ×
  230. </Button>
  231. <Button type="primary" onClick={symbClick}>
  232. ÷
  233. </Button>
  234. </div>
  235. </Form.Item>
  236. <Form.Item
  237. label={TranslateText(["条件编辑", "Edit"])}
  238. // name="formu"
  239. >
  240. <Formular ref={formuRef} />
  241. </Form.Item>
  242. <Form.Item
  243. label={TranslateText(["告警组", "Alarm group"])}
  244. name={"group_id"}
  245. >
  246. <Select
  247. style={{ width: 240 }}
  248. options={[
  249. ...[
  250. {
  251. label: "未分组",
  252. value: 0,
  253. },
  254. ],
  255. ...(groupList?.map((item) => {
  256. return {
  257. label: item?.name,
  258. value: item?.id,
  259. };
  260. }) ?? []),
  261. ]}
  262. placeholder={TranslateText(["请选择", "Please choose"])}
  263. />
  264. </Form.Item>
  265. <Form.Item
  266. label={TranslateText(["告警级别", "Alarm level"])}
  267. name="alarm_level"
  268. rules={[
  269. {
  270. required: true,
  271. message: TranslateText(["请输入告警级别", "Please input"]),
  272. },
  273. ]}
  274. >
  275. <Select
  276. placeholder={TranslateText(["请选择", "Please choose"])}
  277. style={{ width: "240px" }}
  278. >
  279. <Select.Option value={1}>
  280. {TranslateText(["低", "Low"])}
  281. </Select.Option>
  282. <Select.Option value={2}>
  283. {TranslateText(["中", "Medium"])}
  284. </Select.Option>
  285. <Select.Option value={3}>
  286. {TranslateText(["高", "High"])}
  287. </Select.Option>
  288. </Select>
  289. </Form.Item>
  290. </Form>
  291. </div>
  292. <div
  293. style={{
  294. borderRight: "1px solid rgba(255, 255, 255, 0.12)",
  295. marginLeft: "16px",
  296. }}
  297. ></div>
  298. <div className="right">
  299. <div className="header">
  300. <Search
  301. placeholder={TranslateText(["请输入点位名称", "Please input"])}
  302. onSearch={onSearch}
  303. style={{ width: "240px" }}
  304. // className={styles.deviceListTreeSearch}
  305. />
  306. </div>
  307. <Table
  308. onRow={(record) => {
  309. return {
  310. onClick: (event) => {
  311. clickTableRow(record);
  312. }, // 点击行
  313. };
  314. }}
  315. key={deviceList}
  316. loading={loading}
  317. columns={columns}
  318. dataSource={deviceList}
  319. rowKey="point_id"
  320. // className="deviceListTable"
  321. pagination={paginationProps}
  322. onChange={handleTableChange}
  323. size="middle"
  324. />
  325. </div>
  326. </div>
  327. </div>
  328. );
  329. };
  330. export default forwardRef(Complex);