"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = SBYXQK;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _antd = require("antd");
var _ahooks = require("ahooks");
var _lodash = _interopRequireDefault(require("lodash"));
var _api = require("../../api");
var _moment = _interopRequireDefault(require("moment"));
var _vmNodata = _interopRequireDefault(require("../../../vm-nodata"));
var _echartsForReact = _interopRequireDefault(require("echarts-for-react"));
var echarts = _interopRequireWildcard(require("echarts"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function renderItem(params, api) {
  var categoryIndex = api.value(0);
  var start = api.coord([api.value(1), categoryIndex]);
  var end = api.coord([api.value(2), categoryIndex]);
  var height = 15;
  var rectShape = echarts.graphic.clipRectByRect({
    x: start[0],
    y: start[1] - height / 2,
    width: end[0] - start[0],
    height: height
  }, {
    x: params.coordSys.x,
    y: params.coordSys.y,
    width: params.coordSys.width,
    height: params.coordSys.height
  });
  var shapes;
  shapes = rectShape && {
    type: 'rect',
    transition: ['shape'],
    shape: rectShape,
    style: api.style()
  };
  return shapes;
}
function SBYXQK(props) {
  var _a;
  var prefix = props.prefix,
    facilityId = props.facilityId,
    deviceId = props.deviceId;
  var _useSetState = (0, _ahooks.useSetState)({
      timeType: 0,
      dataList: []
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var _useSetState3 = (0, _ahooks.useSetState)({
      categories: [],
      datatemp: []
    }),
    _useSetState4 = _slicedToArray(_useSetState3, 2),
    chart = _useSetState4[0],
    setChart = _useSetState4[1];
  var timeType = state.timeType,
    dataList = state.dataList;
  var legends = [{
    name: '停止',
    color: '#8C8C8C',
    monitorItemCode: 'w_runstatus'
  }, {
    name: '工频',
    color: '#49E6A2',
    monitorItemCode: 'w_gp_runstatus'
  }, {
    name: '变频',
    color: '#438EF7',
    monitorItemCode: 'w_bp_runstatus'
  }, {
    name: '故障',
    color: '#FAAB59',
    monitorItemCode: 'w_faultstatus'
  }, {
    name: '未知',
    color: '#8A7FEE',
    monitorItemCode: 'unKnow'
  }];
  var convertSecondsToHoursAndMinutes = function convertSecondsToHoursAndMinutes(seconds) {
    var days = Math.floor(seconds / (60 * 60 * 24));
    var hours = Math.floor(seconds % (60 * 60 * 24) / (60 * 60));
    var minutes = Math.floor(seconds % (60 * 60 * 24) % (60 * 60) / 60);
    var remainingSeconds = seconds % (3600 * 24) % 3600 % 60;
    return "".concat(days ? "".concat(days, "\u5929") : '').concat(days || hours ? "".concat(hours, "\u5C0F\u65F6") : '').concat(days || hours || minutes ? "".concat(minutes, "\u5206\u949F") : '').concat(remainingSeconds ? "".concat(remainingSeconds, "\u79D2") : '');
  };
  (0, _react.useEffect)(function () {
    var _a;
    if (dataList === null || dataList === void 0 ? void 0 : dataList.length) {
      var s = new Set();
      dataList.forEach(function (item) {
        s.add(item.id);
      }); // ===> 根据Set 的size 属性 用来获取不重复的数据有多少个
      var newData = Array.from(
      // ====>  这一步很重要，要确定二维数组的长度是多少
      {
        length: s.size
      }, function () {
        return [];
      });
      dataList === null || dataList === void 0 ? void 0 : dataList.forEach(function (item) {
        var index = _toConsumableArray(s).indexOf(item.id); // ====>  循环原数组，将key相同的对象提取到二维数组中
        newData[index].push(item);
      });
      newData = newData.map(function (item, index) {
        return item.map(function (it) {
          return Object.assign(Object.assign({}, it), {
            index: index
          });
        });
      });
      var data = (_a = _lodash["default"].flatten(newData)) === null || _a === void 0 ? void 0 : _a.map(function (item) {
        return {
          groupId: item.index,
          facilityName: item.facilityName,
          monitorItemName: item.monitorItemName,
          beginTime: item.beginTime,
          endTime: item.endTime,
          monitorItemCode: item === null || item === void 0 ? void 0 : item.monitorItemCode
        };
      });
      var datatemp = data.map(function (d) {
        var _a, _b;
        var _beginTime = new Date(d.beginTime);
        var _endTime = new Date(d.endTime);
        var differenceInMilliseconds = Math.abs(_endTime - _beginTime);
        var differenceInSeconds = differenceInMilliseconds / 1000;
        return {
          facilityName: d.facilityName,
          value: [parseInt(d.groupId), new Date(d.beginTime).getTime(), new Date(d.endTime).getTime()],
          startday: d.beginTime,
          endday: d.endTime,
          monitorItemName: d.monitorItemName,
          runTime: convertSecondsToHoursAndMinutes(differenceInSeconds),
          itemStyle: {
            normal: {
              color: (_b = (_a = legends.filter(function (a) {
                return (a === null || a === void 0 ? void 0 : a.monitorItemCode) == (d === null || d === void 0 ? void 0 : d.monitorItemCode);
              })) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.color
            }
          }
        };
      });
      var categories = Array.from(new Set(data.map(function (item) {
        return item.facilityName;
      })));
      setChart({
        categories: categories,
        datatemp: datatemp
      });
    }
  }, [dataList]);
  var option = {
    grid: {
      left: '1%',
      right: '6%',
      bottom: '25px',
      top: '1%',
      containLabel: true
    },
    tooltip: {
      formatter: function formatter(params) {
        var _a, _b;
        return "".concat(params.name, " \n                        <br/> \u5F00\u59CB\u65F6\u95F4\uFF1A\n                        ").concat((0, _moment["default"])(params.value[1]).format('YYYY-MM-DD HH:mm:ss'), "\n                        <br/> \u7ED3\u675F\u65F6\u95F4\uFF1A\n                        ").concat((0, _moment["default"])(params.value[2]).format('YYYY-MM-DD HH:mm:ss'), "\n                         <br/>").concat(params.marker).concat((_a = params === null || params === void 0 ? void 0 : params.data) === null || _a === void 0 ? void 0 : _a.monitorItemName, "\u8FD0\u884C\uFF1A\n                         ").concat((_b = params === null || params === void 0 ? void 0 : params.data) === null || _b === void 0 ? void 0 : _b.runTime, "\n                        ");
      }
    },
    dataZoom: [{
      type: 'slider',
      filterMode: 'weakFilter',
      showDataShadow: false,
      labelFormatter: '',
      handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
      handleSize: 14,
      height: 8,
      bottom: 12
    }, {
      type: 'inside',
      filterMode: 'weakFilter'
    }],
    xAxis: {
      scale: true,
      type: 'time',
      min: (0, _moment["default"])().subtract(timeType, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
      max: (0, _moment["default"])().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
      splitNumber: 4,
      axisLabel: {
        formatter: function formatter(val) {
          return "".concat((0, _moment["default"])(val).format('M-D'), "\n").concat((0, _moment["default"])(val).format('HH:mm:ss'));
        },
        padding: [0, 0, 0, 0],
        showMaxLabel: true
      }
    },
    yAxis: [{
      yAxisIndex: 0,
      type: 'category',
      data: chart === null || chart === void 0 ? void 0 : chart.categories,
      axisLine: {
        show: false,
        lineStyle: {
          color: '#030101',
          width: 2,
          type: 'solid'
        }
      },
      splitLine: {
        show: false,
        lineStyle: {
          color: ['#CCCCCC'],
          type: 'solid'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#8C8C8C',
        fontSize: '12'
      }
    }],
    series: [{
      type: 'custom',
      renderItem: renderItem,
      encode: {
        x: [1, 2],
        y: 0
      },
      data: chart === null || chart === void 0 ? void 0 : chart.datatemp
    }]
  };
  (0, _react.useEffect)(function () {
    if (deviceId) {
      _api.SbxlService.findWaterPumpRunView({
        facilityId: facilityId,
        deviceId: deviceId,
        begin: (0, _moment["default"])().subtract(timeType, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        end: (0, _moment["default"])().endOf('day').format('YYYY-MM-DD HH:mm:ss')
      }).then(function (data) {
        var list = (data === null || data === void 0 ? void 0 : data.rows) || [];
        var arr = [];
        list === null || list === void 0 ? void 0 : list.map(function (item) {
          var _a;
          (_a = item === null || item === void 0 ? void 0 : item.details) === null || _a === void 0 ? void 0 : _a.map(function (it) {
            var _a, _b;
            arr === null || arr === void 0 ? void 0 : arr.push(Object.assign(Object.assign({}, it), {
              facilityId: item === null || item === void 0 ? void 0 : item.facilityId,
              id: item === null || item === void 0 ? void 0 : item.facilityId,
              facilityName: item === null || item === void 0 ? void 0 : item.facilityName,
              monitorItemName: (_b = (_a = legends.filter(function (a) {
                return (a === null || a === void 0 ? void 0 : a.monitorItemCode) == (it === null || it === void 0 ? void 0 : it.monitorItemCode);
              })) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.name
            }));
          });
        });
        setState({
          dataList: arr
        });
      });
    }
  }, [facilityId, deviceId, timeType]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-sbyxqk")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-title")
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u6C34\u6CF5\u8FD0\u884C\u60C5\u51B5"), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement("span", null, "\u65F6\u6BB5\uFF1A"), /*#__PURE__*/_react["default"].createElement(_antd.Radio.Group, {
    style: {
      marginLeft: 10
    },
    options: [{
      label: '近24小时',
      value: 0
    }, {
      label: '近3天',
      value: 2
    }, {
      label: '近7天',
      value: 6
    }],
    optionType: "button",
    value: timeType,
    onChange: function onChange(e) {
      var value = e.target.value;
      setState({
        timeType: value
      });
    }
  }))), (dataList === null || dataList === void 0 ? void 0 : dataList.length) ? (/*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-chart")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend")
  }, legends.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items"),
      key: item.name
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items-icon"),
      style: {
        backgroundColor: item.color
      }
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(prefix, "-sbxl-main-top-sbyxqk-legend-items-text")
    }, item.name));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      width: '100%',
      height: "".concat(30 * ((_a = chart === null || chart === void 0 ? void 0 : chart.categories) === null || _a === void 0 ? void 0 : _a.length) + 40, "px"),
      minHeight: '200px'
    }
  }, /*#__PURE__*/_react["default"].createElement(_echartsForReact["default"], {
    echarts: echarts,
    option: option,
    style: {
      width: '100%',
      height: "100%"
    },
    notMerge: true,
    lazyUpdate: true
  })))) : (/*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: 100,
      marginTop: 10
    }
  }, /*#__PURE__*/_react["default"].createElement(_vmNodata["default"], null))));
}
//# sourceMappingURL=SBYXQKV2.js.map
