import FloorController from "./floorController";
import { baseHttpUtils } from "@/utils/http/baseHttpUtils";

export interface BuildReq {
  pageNumber: number;
  pageSize: number;
  orders: string;
  projection: string[];
}
class BuildController {
  static http = new baseHttpUtils();
  static pre = "/datacenter/object";
  /**
   * 获取建筑信息
   * @param {BuildReq} params
   * @returns
   */
  static async buildingQuery(params: BuildReq) {
    return await this.http.postRequest(this.pre + "/building/query", params);
  }
  static async getData(buildFloorData) {
    let data,
      buildParams = {
        pageNumber: 1,
        pageSize: 1000,
        orders: "localName asc",
        projection: ["id", "localName"],
      },
      floorParams = {
        orders: "floorSequenceID desc",
        pageNumber: 1,
        pageSize: 1000,
        projection: ["buildingId", "id", "localName", "floorSequenceID"],
      };
    Promise.all([
      this.buildingQuery(buildParams),
      FloorController.floorQuery(floorParams),
    ]).then(function (values: any) {
      let builData = values[0].content,
        floorData = values[1].content;
      data = builData.map((build) => {
        return {
          value: build.id,
          label: build.localName,
        };
      });
      data.unshift({
        value: "",
        label: "未明确建筑",
      });
      data.forEach((build) => {
        floorData.forEach((floor) => {
          if (build.value == floor.buildingId && floor.id && floor.localName) {
            if (build.children) {
              build.children.push({
                value: floor.id,
                label: floor.localName,
              });
            } else {
              build.children = [];
              build.children.push(
                {
                  value: "",
                  label: "未明确楼层",
                },
                {
                  value: floor.id,
                  label: floor.localName,
                }
              );
            }
          }
        });
      });
      data.forEach((item) => {
        if (item.children && item.children.length) {
          item.children.forEach((child) => {
            if (child.value != "") {
              buildFloorData.push({
                Code: item.value + "-" + child.value,
                Name: item.label + "-" + child.label,
              });
            } else {
              buildFloorData.push({
                Code: item.value,
                Name: item.label + "-" + child.label,
              });
            }
          });
        } else {
          buildFloorData.push({
            Code: item.value,
            Name: item.label,
          });
        }
      });
    });
  }
}
export default BuildController;
