import request from "./request";
import {requestfile} from "./request";
const { config: { mesUrl=  "http://120.202.38.15:8081/gateway", edgeQueryUrl } = {} } = global;

function getFactory() {
  let url = `${mesUrl}/mdgeneric/md/bmWorkCenter/findAllWithPage`;
  let bodys = {
    removePermission: true,
    page: 1,
    pageSize: 100000,
    sorted: "serialNumber",
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getOperation(val) {
  let url = `${mesUrl}/mdgeneric/md/bmOperation/findOpByWorkCenterCode`;
  let bodys = {
    code: val,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}

function getuserList(val) {
  let url = `${mesUrl}/console/sys/user/findAllWithPage`;
  let bodys = {
    page: -1,
    pageSize: -1,
    filter: `userCode  in (${val})`,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}

function saverelation(user, zz) {
  let url = `${mesUrl}/orchestration/submit/PM_BP_EQUIP_USER_RELATION_SAVE`;
  let bodys = {
    asyn: false,
    properties: {
      userGid: user.userId,
      userCode: user.userCode,
      userName: user.userName,
      EquipmentRequestList: [
        {
          equipCode: zz.serialNumber,
          equipName: zz.name,
          type: "INSPECT",
        },
        {
          equipCode: zz.serialNumber,
          equipName: zz.name,
          type: "MAINTAIN",
        },
        {
          equipCode: zz.serialNumber,
          equipName: zz.name,
          type: "REPAIR",
        },
      ],
    },
  };
  return bodys;
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}

function getProductionLine(val) {
  let url = `${mesUrl}/mdgeneric/md/bmProductionLine/findAllWithPage`;
  let bodys = {
    page: -1,
    pageSize: -1,
    filter: `status eq 1 and workshopCode eq '${val}'`,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}

function getEquipFa(item) {
  let url = `${mesUrl}/mdgeneric/md/bmEquipment/findAllWithPage`;
  let bodys = {
    removePermission: true,
    page: 1,
    pageSize: 100000,
    filter: "(positionInfoCode eq '" + item.key + "') ",
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        item.value = response.data;
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getEquipRequire(val, op) {
  let url = `${mesUrl}/mdgeneric/md/bmEquipment/getEquipRequire`;
  let bodys = { workCenterCode: val, operationCode: op };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getProduct(val) {
  let url = `${mesUrl}/mdgeneric/md/material/findAllWithPage`;
  let bodys = {
    page: -1,
    pageSize: -1,
    filter: ` materialTypeCode eq '${val}'`,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getProductType() {
  let url = `${mesUrl}/mdgeneric/md/materialtype/findAllWithPage`;
  let bodys = {
    removePermission: true,
    page: 1,
    pageSize: 10000000,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getXmr(params) {
  let urltmp = "http://10.10.21.8/gateway";
  urltmp = mesUrl;
  let url = `${urltmp}/quality/quality/spc/findSpcData`;
  let bodys = params;
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve(response.message);
    });
  });
}

function getDailyBoard(date) {
  let mesUrldev = "http://10.10.21.73/gateway";
  let url = `${mesUrldev}/prodexec/prodexec/planScreen/getDailyBoard`;
  let bodys = {
    date: date,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getShotImgByProductBatchNo(num) {
  //  mesUrl =  "http://10.10.21.73/gateway";
  let url = `${mesUrl}/quality/quality/simpling/getShotImgByProductBatchNo`;
  let bodys = {
    productBatchNo: num,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}

function getShotImg(url) {
  //  mesUrl =  "http://120.202.38.15:8081/gateway";
  let bodys = {};
  return  requestfile({ url, bodys });
}

function getIotUrl(workcenterCode, iotUrl) {
  let url = `${mesUrl}/mdgeneric/md/datadict/findAllWithPage`;
  let bodys = {
    page: "-1",
    pageSize: "-1",
    filter: `groupCode eq '${workcenterCode}' and itemCode eq '${iotUrl}'`,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (
        response.success &&
        response.data &&
        response.data.length > 0 &&
        response.data[0].itemName
      ) {
        resolve(response.data[0].itemName.replace("nginx", window.host));
      }
      resolve("");
    });
  });
}
function getEquipType() {
  let url = `${mesUrl}/mdgeneric/md/bmEquipmentType/findAllWithPage`;
  let bodys = {
    removePermission: true,
    page: 1,
    pageSize: 100000,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getEquip(param = {}) {
  let url = `${mesUrl}/mdgeneric/md/bmEquipment/findAllWithPage`;
  let bodys = {
    removePermission: true,
    page: 1,
    pageSize: 100000,
    filter: "identification eq '0' and monitorDisplay eq 1 ",
  };
  if (param.equipTypeValue) {
    bodys.filter += ` and (equipTypeGid eq '${param.equipTypeValue}') `;
  }
  if (param.factoryValue) {
    bodys.filter += ` and (positionInfoCode eq '${param.factoryValue}') `;
  }
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getEquipmentLocation(equipCode) {
  let url = `${mesUrl}/mdgeneric/md/bmEquipment/getCurveReportIotFieldsByEquipCode`;
  let bodys = {
    equipCode: equipCode,
  };
  return new Promise((resolve, reject) => {
    request({ url, bodys }).then((response = {}) => {
      if (response.success) {
        resolve(response.data);
      }
      resolve([]);
    });
  });
}
function getEquipmentLocationEchart(params) {
  let edgeQueryUrl1 = "";
  return getIotUrl(params.workCenterCode, "iotUrl").then((aa) => {
    console.log("aaa",aa)
    edgeQueryUrl1 = aa;
    let url = `${edgeQueryUrl1}/equip/query`;

    let bodys = {
      startTime: params.startTime,
      endTime: params.endTime,
      interval: "1m",
      equipCode: params.equipCode,
      fields: params.fields,
      page: 1,
      pageSize: 100000,
      sorted: "time desc",
    };
    return new Promise((resolve, reject) => {
      request({ url, bodys }).then((response = {}) => {
        if (response.success) {
          resolve(response.data);
        }
        resolve([]);
      });
    });
  });
}
export {
  getEquipmentLocationEchart,
  getEquipmentLocation,
  getEquip,
  getEquipType,
  getFactory,
  getDailyBoard,
  getProductionLine,
  getOperation,
  getEquipRequire,
  getProduct,
  getProductType,
  getuserList,
  saverelation,
  getEquipFa,
  getXmr,
  getShotImg,
  getShotImgByProductBatchNo,
  getIotUrl,
};
