<template>
  <div class="main-box">
    <div class="saga-build-mess">
      <el-button @click="queryZone">导入业务空间BIMLocation</el-button>
    </div>
  </div>
</template>
<script>
import {
  queryZone,
  updateZone,
} from "@/api/scan/request";
import { mapGetters } from "vuex";
import pako from '@/assets/pako/pako'
import axios from "axios";
export default {
  data() {
    return {
      options: [],
      tabsList: [
        {
          "Code": "GeneralZone",
          "Name": "默认分区",
          "Rel_type": "99"
        },
        {
          "Name": "供电分区",
          "Rel_type": "1",
          "Code": "PowerSupplyZone"
        },
        {
          "Name": "照明分区",
          "Rel_type": "2",
          "Code": "LightingZone"
        },
        {
          "Name": "网络分区",
          "Rel_type": "3",
          "Code": "NetworkZone"
        },
        {
          "Code": "AirConditioningZone",
          "Name": "空调分区",
          "Rel_type": "4"
        },
        {
          "Name": "采暖分区",
          "Rel_type": "5",
          "Code": "HeatingZone"
        },
        {
          "Name": "洁净分区",
          "Rel_type": "6",
          "Code": "CleanZone"
        },
        {
          "Name": "生活给水分区",
          "Rel_type": "7",
          "Code": "DomesticWaterSupplyZone"
        },
        {
          "Code": "FireZone",
          "Name": "防火分区",
          "Rel_type": "8"
        },
        {
          "Name": "安防分区",
          "Rel_type": "9",
          "Code": "SecurityZone"
        },
        {
          "Name": "租户分区",
          "Rel_type": "10",
          "Code": "TenantZone"
        },
        {
          "Name": '功能分区',
          'Rel_type': '11',
          "Code": 'FunctionZone'
        }
      ],
    }
  },
  computed: {
    ...mapGetters('layout', ['projectId', 'userId', 'secret'])
  },
  methods: {
    init() { },
    // 查询该项目下所有的业务空间
    queryZone() {
      this.tabsList.map(t => {
        this.getZone(t.Code, 1, t.Name);
      })
    },
    getZone(Code, PageNumber, Name) {
      let pa = {
        data: {
          PageNumber: PageNumber,
          PageSize: 1000,
          Orders: 'RoomID asc'
        },
        zone: Code
      }
      queryZone(pa, res => {
        if (res.Total > PageNumber * 1000) {
          let num = PageNumber + 1
          console.log(num)
          this.getZone(Code, num, Name)
        }
        let params = {
          data: {
            Content: [],
            Projection: ["BIMLocation"]
          },
          zone: Code
        }
        res.Content.map(t => {
          if (t.Outline && t.Outline[0]) {
            t.BIMLocation = this.getAverageVal(t.Outline);
            let obj = {
              RoomID: t.RoomID,
              BIMLocation: t.BIMLocation
            }
            params.data.Content.push(obj);
          }
        });
        if (params.data.Content.length) {
          updateZone(params, res => {
            console.log(`${this.projectId},${Name},${params.data.Content.length}条更新完成`);
          })
        }
      })
    },
    getAverageVal(Outline) {
      let X = 0, Y = 0, Z = 0, len = 0;
      Outline.map(t => {
        if (t[0]) {
          t[0].map(item => {
            X += item.X;
            Y += item.Y;
            Z += item.Z;
          })
          len += t[0].length
        }
      })
      X = (X / len).toFixed(2);
      Y = (Y / len).toFixed(2);
      Z = (Z / len).toFixed(2);
      return `${X},${Y},${Z}`
    },
  },
  created() {
    this.init()
  },
  watch: {
    projectId() {
      this.init()
    }
  }
}
</script>