WaterFlowControlView.js 9.22 KB
Newer Older
1
/* eslint-disable */
涂茜's avatar
涂茜 committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 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 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 213 214 215 216 217 218 219 220 221 222 223 224 225 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
class WaterFlowControlView {
  constructor(options) {
    this.linkDataArray = [];
    this.browserArray = [];
    this.hBarArray = [];
    this.allClose = [];
    this.myDiagram = null;
  }

  /** 获取处理好的json************************************************* */
  waterFlowControlByDiagramJson(oldJson, myDiagrams) {
    this.myDiagram = myDiagrams;
    this.hBarArray = this.getAllNodeByCategory('HBar');
    if (!this.hBarArray.length) return null;
    let newnodeDataArray = JSON.parse(this.myDiagram.model.toJson()).nodeDataArray;
    this.newJson = JSON.parse(this.myDiagram.model.toJson());
    let oldnodeDataArray = oldJson.nodeDataArray;
    let _oldControlArray = oldnodeDataArray.filter(function (item) {
      return item.switch == '是' && item.category != 'switchCase';
    });
    let _newControlArray = newnodeDataArray.filter(function (item) {
      return item.switch == '是' && item.category != 'switchCase' && item.realVal != '--';
    });
    let diffentArr = this.diffent(_oldControlArray, _newControlArray, 'key', 'switchState');
    if (!diffentArr.length) return null;
    this.linkDataArray = JSON.parse(this.myDiagram.model.toJson()).linkDataArray;
    this.flag = false;
    for (let j = 0; j < newnodeDataArray.length; j++) {
      if (
        newnodeDataArray[j].switch == '是' &&
        newnodeDataArray[j].category != 'switchCase' &&
        oldnodeDataArray[j].switchState &&
        newnodeDataArray[j].realVal != '--'
      ) {
        //代表控制属性值有修改
        this.flag = true;
        let controlValue = newnodeDataArray[j].switchState;
        let isHavingDash = controlValue == '开' ? true : false;
        let key = newnodeDataArray[j].key;
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, isHavingDash);
        //判断此条线路上面是否还有其他的控制开关
        let otherControl = false;
        for (let k = 0; k < newnodeDataArray.length; k++) {
          for (let i = 1; i < this.browserArray.length; i++) {
            if (
              newnodeDataArray[k].key == this.browserArray[i] &&
              newnodeDataArray[k].switchState == '关' &&
              controlValue == '开'
            ) {
              otherControl = true;
              break;
            }
          }
        }
        if (!otherControl) {
          this.newJson.linkDataArray = this.linkDataArray;
          //如果从全关状态下 开启摸个按钮
          this.hBarArray.map((obj, index) => {
            if (obj == key) {
              // 合管和状态管是同一个元器件
              this.allClose[index] = isHavingDash;
              for (let temp of this.newJson.linkDataArray) {
                if (temp.from == obj) {
                  this.browserArray = [];
                  this.browserArray.push(temp.to);
                  this.changWaterFlow(temp.to, isHavingDash);
                  this.newJson.linkDataArray = this.linkDataArray;
                }
                if (temp.to == obj) {
                  this.browserArray = [];
                  this.browserArray.push(temp.from);
                  this.changWaterFlow(temp.from, isHavingDash);
                  this.newJson.linkDataArray = this.linkDataArray;
                }
              }
            } else if (this.allClose[index] && controlValue == '开') {
              //合管周边管子水流开启
              this.allClose[index] = false; //现在不是全部关闭
              if (this.isIn(obj, key)) {
                for (let temp of this.newJson.linkDataArray) {
                  if (temp.from == obj) {
                    this.browserArray = [];
                    this.browserArray.push(temp.to);
                    this.changWaterFlow(temp.to, isHavingDash);
                    this.newJson.linkDataArray = this.linkDataArray;
                  }
                }
              } else if (this.isOut(obj, key)) {
                for (let temp of this.newJson.linkDataArray) {
                  if (temp.to == obj) {
                    this.browserArray = [];
                    this.browserArray.push(temp.from);
                    this.changWaterFlow(temp.from, isHavingDash);
                    this.newJson.linkDataArray = this.linkDataArray;
                  }
                }
              }
            }
          });
        }
      }
    }
    if (!this.flag) {
      return null;
    }
    //判断合节点的连接个数
    let te = [];
    this.hBarArray.map((data) => {
      te.push(data);
    });
    for (let z = 0; z < te.length; z++) {
      let key = te[z];
      let open = 0;
      let close = 0;
      let inArray = [];
      let outArray = [];
      this._hBarArray = this.hBarArray.concat();
      delete this._hBarArray[z];
      for (let temp of this.newJson.linkDataArray) {
        if (temp.from == key) inArray.push(temp);
        if (temp.to == key) outArray.push(temp);
      }
      //全部关闭
      let inarrTemp = inArray.filter((obj) => {
        return obj.isHavingDash == false;
      });
      if (inarrTemp.length == inArray.length && inArray.length != 0) {
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, false, this._hBarArray);
        this.newJson.linkDataArray = this.linkDataArray;
        this.allClose[z] = true;
      }
      let outarrTemp = outArray.filter((obj) => {
        return obj.isHavingDash == false;
      });
      if (outarrTemp.length == outArray.length && outArray.length != 0) {
        this.browserArray = [];
        this.browserArray.push(key);
        this.changWaterFlow(key, false, this._hBarArray);
        this.newJson.linkDataArray = this.linkDataArray;
        this.allClose[z] = true;
      }
    }
    this.newJson.nodeDataArray.forEach(
      function (item) {
        for (let i = 0; i < te.length; i++) {
          if (item.key == te[i]) {
            item.typeDash = this.allClose[i];
            return false;
          }
        }
      }.bind(this),
    );
    return this.newJson;
  }

  /** 获取所有合管id************************************************* */
  getAllNodeByCategory(category) {
    let nodeDataArray = JSON.parse(this.myDiagram.model.toJson()).nodeDataArray;
    let retVal = [];
    for (let j = 0; j < nodeDataArray.length; j++) {
      if (
        nodeDataArray[j].category == category ||
        (nodeDataArray[j].hbControl && nodeDataArray[j].hbControl == '是')
      ) {
        retVal.push(nodeDataArray[j].key);
        if (this.allClose[retVal.length - 1] == undefined) this.allClose.push(false);
      }
    }
    return retVal;
  }

  /***/
  changWaterFlow(key, isHavingDash, hBarArray) {
    hBarArray = hBarArray ? hBarArray : this.hBarArray.concat();
    if (this.isInArray(hBarArray, key)) {
      //判断key是否是合管
      return;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.from == key && temp.from != temp.to) {
        this.linkDataArray[j].isHavingDash = isHavingDash;
        if (!this.isInArray(this.browserArray, temp.to)) {
          this.browserArray.push(temp.to);
          this.changWaterFlow(temp.to, isHavingDash, hBarArray);
        }
      } else if (temp.to == key && temp.to != temp.from) {
        this.linkDataArray[j].isHavingDash = isHavingDash;
        if (!this.isInArray(this.browserArray, temp.from)) {
          this.browserArray.push(temp.from);
          this.changWaterFlow(temp.from, isHavingDash, hBarArray);
        }
      }
    }
  }

  /***/
  isInArray(array, key) {
    let retValue = false;
    for (let j = 0; j < array.length; j++) {
      if (array[j] == key) {
        retValue = true;
        break;
      }
    }
    return retValue;
  }

  isIn(hBarKey, nodeKey) {
    let val = false;
    if (hBarKey == nodeKey) {
      //判断key是否是合管
      val = true;
      return val;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.from && temp.from == nodeKey && temp.from != temp.to) {
        val = val == true ? true : this.isIn(hBarKey, temp.to);
      }
    }
    return val;
  }

  isOut(hBarKey, nodeKey) {
    let val = false;
    if (hBarKey == nodeKey) {
      //判断key是否是合管
      val = true;
      return val;
    }
    for (let j = 0; j < this.linkDataArray.length; j++) {
      let temp = this.linkDataArray[j];
      if (temp.to && temp.to == nodeKey && temp.to != temp.from) {
        val = val == true ? true : this.isOut(hBarKey, temp.from);
      }
    }
    return val;
  }

  diffent(fArr, cArr, key, field) {
    let diffRes = [];
    let fDatas = [];
    let cDatas = [];
    for (let i in fArr) {
      let flg = false;
      for (let j in cArr) {
        if (cArr[j][key] === fArr[i][key] && cArr[j][field] === fArr[i][field]) {
          flg = true;
          break;
        }
      }
      if (!flg) {
        fDatas.push(fArr[i]);
      }
    }
    for (let i in cArr) {
      let flg = false;
      for (let j in fArr) {
        if (fArr[j][field] === cArr[i][field] && fArr[j][field] === cArr[i][field]) {
          flg = true;
          break;
        }
      }
      if (!flg) {
        cDatas.push(cArr[i]);
      }
    }
    diffRes.push(...cDatas.concat(fDatas));
    return diffRes;
  }
}

export default WaterFlowControlView;