<template>
  <div class="main-box">
    <div class="saga-build-mess">
      <h4>接口基于数据中心,数据更改基于已创建业务空间本身数据计算而得,有轮廓线即可写入BIMLocation,处理单位是毫米的高度为米</h4>
      <el-checkbox-group v-model="checkTypeList">
        <el-checkbox v-for="item in tabsList" :label="item.Code" :key="item.Rel_type">{{item.Name}}</el-checkbox>
      </el-checkbox-group>
      <el-checkbox-group v-model="checkDataList">
        <el-checkbox label="BIMLocation">根据业务空间轮廓线计算得</el-checkbox>
        <el-checkbox label="Height">根据<b>业务空间或元空间</b>本身高度超过100,则/1000(如果本身没有高度或小于100不会处理)</el-checkbox>
      </el-checkbox-group>
      <el-button @click="queryZone">导入业务空间选中的数据</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'
        },
        {
          "Name": '元空间',
          "Code": 'Ispace',
          "Rel_type": '--'
        },
      ],
      checkDataList: ['BIMLocation'],
      checkTypeList: ['GeneralZone', 'PowerSupplyZone', 'LightingZone', 'NetworkZone', 'AirConditioningZone', 'HeatingZone', 'CleanZone', 'DomesticWaterSupplyZone', 'FireZone', 'SecurityZone', 'TenantZone', 'FunctionZone'],
      codeToName: {
        GeneralZone: '默认分区',
        PowerSupplyZone: '供电分区',
        LightingZone: '照明分区',
        NetworkZone: '网络分区',
        AirConditioningZone: '空调分区',
        HeatingZone: '采暖分区',
        CleanZone: '洁净分区',
        DomesticWaterSupplyZone: '生活给水分区',
        FireZone: '防火分区',
        SecurityZone: '安防分区',
        TenantZone: '租户分区',
        FunctionZone: '功能分区',
        Ispace: '元空间',
      },
      checkAll: false,
      isIndeterminate: true
    }
  },
  computed: {
    ...mapGetters('layout', ['projectId', 'userId', 'secret'])
  },
  methods: {
    init() { },
    // 查询该项目下所有的业务空间
    queryZone() {
      this.checkTypeList.map(t => {
        this.getZone(t, 1, this.codeToName[t]);
      })
    },
    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)
        }
        // 更新BIMLocation
        if (this.checkDataList.indexOf("BIMLocation") > -1) {
          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);
            }
          });
          this.updateZoneData(params, Name)
        }

        // 更新业务空间Height
        if (this.checkDataList.indexOf("Height") > -1) {
          let params2 = {
            data: {
              Content: [],
              Projection: ["Height"]
            },
            zone: Code
          }
          res.Content.map(t => {
            if (t.Height && t.Height > 100) {
              t.Height = (t.Height / 1000).toFixed(2);
              let obj = {
                RoomID: t.RoomID,
                Height: t.Height
              }
              params2.data.Content.push(obj);
            }
          });
          this.updateZoneData(params2, Name)
        }
      })
    },
    updateZoneData(param, Name) {
      if (param.data.Content.length) {
        updateZone(param, res => {
          console.log(`${this.projectId},${Name},${param.data.Projection.toString()},${param.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>