import { stringify } from 'qs';
import { request } from 'umi';

export async function queryEnergy(projectId) {
  const res = await request(`/api/energy/v1/analy-card/list`, {
    method: 'POST',
    data: {
      project_id: projectId,
    },
  });
  return res;
}

export async function queryProcessSection(projectId) {
  const res = await request(
    `/api/v1/process-section/${projectId}?page_size=999`,
  );
  return { data: res?.data?.list };
}

export async function queryUserList(param) {
  return request(`/api/v1/user/project/${param.projectId}`);
}
/**
 * 最优工况列表
 * @param {*} data.project_id
 * @param {*} data.section_id
 * @returns
 */
export async function queryBaseList(data) {
  let res = await request(`/api/smart/v1/base/list`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

/**
 * 最优工况列表
 * @param {*} data.project_id
 * @param {*} data.section_id
 * @returns
 */
export async function queryOptimum(data) {
  let res = await request(`/api/smart/v1/optimum-work/list`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

/**
 * 新增最优工况
 * @param {*} data.project_id
 * @param {*} data.section_id
 * @param {*} data.bid
 * @param {*} data.control_value
 * @param {*} data.name
 * @returns
 */
export async function addOptimum(data) {
  let res = await request(`/api/smart/v1/optimum-work/add`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

/**
 * 添加基础参数
 * @param {*} data.project_id
 * @param {*} data.section_id
 * @param {*} data.base_value
 * @returns
 */
export async function addBaseParams(data) {
  let res = await request(`/api/smart/v1/base/add`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

/**
 * 添加基础参数
 * @param {*} data.id
 * @param {*} data.is_best
 * @param {*} data.control_value
 * @param {*} data.name
 * @param {*} data.is_delete
 * @returns
 */
export async function editOptimum(data) {
  let res = await request(`/api/smart/v1/optimum-work/edit`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

export async function updateBase(data) {
  let res = await request(`/api/smart/v1/base/edit`, {
    method: 'POST',
    dataType: 'formData',
    data: data,
  });
  return res;
}

/**
 * PAC配置：拉取列表，同一项目中只有一组规则生效
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.page_size
 * @param {*} data.page
 * @returns
 */
export async function queryPacList(data) {
  let res = await request(`/api/simulations/v1/pac/list?${stringify(data)}`);
  return res;
}

/**
 * PAC配置：拉取列表，同一项目中只有一组规则生效
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.page_size
 * @param {*} data.page
 * @param {*} data.s_time
 * @param {*} data.e_time
 * @returns
 */
export async function queryPacDosing(data) {
  let res = await request(`/api/simulations/v1/pac/dosing?${stringify(data)}`);
  return res;
}

/**
 * PAC配置：新建
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.dosh
 * @param {*} data.dosl
 * @param {*} data.tubh
 * @param {*} data.tubl
 * @returns
 */
export async function addPac(data) {
  let res = await request(`/api/simulations/v1/pac`, {
    method: 'POST',
    data: data,
  });
  return res;
}
/**
 * PAC配置：编辑
 * @param {*} data.id
 * @param {*} data.status  0：禁用   1：启用
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.dosh
 * @param {*} data.dosl
 * @param {*} data.tubh
 * @param {*} data.tubl
 * @returns
 */
export async function updatePac(data) {
  let res = await request(`/api/simulations/v1/pac`, {
    method: 'PUT',
    data: data,
  });
  return res;
}

export async function conditionChart(params) {
  let res = await request(`/api/energy/v1/condition/chart`, {
    method: 'POST',
    data: params,
  });
  return res;
}

/**
 * 反洗配置：拉取列表，同一项目中只有一组规则生效
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.page_size
 * @param {*} data.page
 * @returns
 */
export async function queryBackwashList(data) {
  let res = await request(
    `/api/simulations/v1/design/backwash/list?${stringify(data)}`,
  );
  return res;
}

/**
 * 膜工况及优化建议
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.page_size
 * @param {*} data.page
 * @param {*} data.s_time
 * @param {*} data.e_time
 * @returns
 */
export async function queryMembraneConditions(data) {
  let res = await request(
    `/api/simulations/v1/membrane/conditions?${stringify(data)}`,
  );
  return res;
}
/**
 * 反洗记录
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.bw_pattern 查询某种类型的反洗，只能是： PEB ｜ CEB
 * @param {*} data.page_size
 * @param {*} data.page
 * @param {*} data.s_time
 * @param {*} data.e_time
 * @returns
 */
export async function queryBackwash(data) {
  let res = await request(
    `/api/simulations/v1/record/backwash?${stringify(data)}`,
  );
  return res;
}

/**
 * 反洗：新建
 * @param {*} data.project_id
 * @param {*} data.device_code
 * @param {*} data.peb_interval
 * @param {*} data.peb_count
 * @param {*} data.ceb_ppm
 * @returns
 */
export async function addBackwash(data) {
  let res = await request(`/api/simulations/v1/backwash`, {
    method: 'POST',
    data: data,
  });
  return res;
}
/**
 * 反洗：编辑
 * @param {*} data.id
 * @param {*} data.status  0：禁用   1：启用
 * @param {*} data.project_id
 * @param {*} data.peb_interval
 * @param {*} data.peb_count
 * @param {*} data.ceb_ppm
 * @returns
 */
export async function updateBackwash(data) {
  let res = await request(`/api/simulations/v1/pac`, {
    method: 'PUT',
    data: data,
  });
  return res;
}

/**
 * 获取收益信息
 * @param {*} data.project_id
 * @param {*} data.s_day
 * @param {*} data.e_day
 * @returns
 */
export async function querySimulationProfit(data) {
  let res = await request(
    `/api/simulations/v1/profit/summary?${stringify(data)}`,
  );
  return res;
}
export async function conditionEstimate(params) {
  let res = await request(`/api/energy/v1/condition/estimate`, {
    method: 'POST',
    data: params,
  });
  return res;
}

// 项目大水量冲洗设计：拉取设计列表
export async function queryDesignWashList(data) {
  let res = await request(
    `/api/simulations/v1/design/wash/list?${stringify(data)}`,
  );
  return res;
}

// 运行记录：拉取大水量冲洗
export async function queryDesignWash(data) {
  let res = await request(`/api/simulations/v1/record/wash?${stringify(data)}`);
  return res;
}

// 项目拉取非氧化杀菌列表
export async function queryDesignNobList(data) {
  let res = await request(
    `/api/simulations/v1/design/nob/list?${stringify(data)}`,
  );
  return res;
}

// 运行记录：拉取非氧化杀菌运行记录
export async function queryDesignNob(data) {
  let res = await request(`/api/simulations/v1/record/nob?${stringify(data)}`);
  return res;
}

// 项目泵设计：拉取设计列表  type膜类型 mf | uf | nf | ro
export async function queryPumpList(data) {
  let res = await request(
    `/api/simulations/v1/design/pump/list?${stringify(data)}`,
  );
  return res;
}

// 运行记录：拉取泵运行记录  type膜类型 mf | uf | nf | ro
export async function queryPump(data) {
  let res = await request(`/api/simulations/v1/record/pump?${stringify(data)}`);
  return res;
}

// 水厂工况
export async function queryConditionSnapshot(data) {
  let res = await request(
    `/api/energy/v1/condition/snapshot?${stringify(data)}`,
  );
  return res;
}

// 工况评估
export async function queryRealEstimate(project_id) {
  let res = await request(`/api/energy/v1/condition/real-estimate`, {
    method: 'POST',
    data: {
      project_id,
    },
  });
  return res;
}

// 工况图表
export async function queryRealEstimateChart(project_id) {
  let res = await request(`/api/energy/v1/condition/real-chart`, {
    method: 'POST',
    data: {
      project_id,
      hour: 24,
    },
  });
  return res;
}

// 项目配置：获取所有拥有配置的项目
export async function queryProjectConfig(params) {
  let res = await request(`/api/simulations/v1/project?${stringify(params)}`);
  let project_categorys = res.data.info.project_categorys;
  // 全厂优化数
  let optimizationNumber = 0;
  project_categorys.forEach((item) => {
    // 工艺单元优化数
    item.optimizationNumber = 0;
    item.childs.forEach((cItem) => {
      // 工艺系统优化数
      cItem.optimizationNumber = 0;
      Object.entries(cItem.optimizationItems || {}).map(
        ([deviceCode, count]) => {
          if (count > 0) {
            cItem.optimizationNumber++;
          }
        },
      );
      cItem.parentName = item.name;
      // 记录工艺段的优化数量
      item.optimizationNumber += cItem.optimizationNumber;
    });
    // 记录总的优化数量
    optimizationNumber += item.optimizationNumber;
  });
  return { data: { project_categorys, optimizationNumber } };
}

// 项目膜设计：拉取设计列表  type膜类型 mf | uf | nf | ro
export async function queryMembraneList(data) {
  let res = await request(
    `/api/simulations/v1/design/membrane/list?${stringify(data)}`,
  );
  return res;
}

// 运行记录：拉取非氧化杀菌运行记录  type膜类型 mf | uf | nf | ro
export async function queryMembrane(data) {
  let res = await request(
    `/api/simulations/v1/record/membrane?${stringify(data)}`,
  );
  return res;
}

// 项目药剂设计：拉取设计列表  药剂类型 pac|nob|hci|sbs|anti
export async function queryDrugList(data) {
  let res = await request(
    `/api/simulations/v1/design/drug/list?${stringify(data)}`,
  );
  return res;
}

// 运行记录：拉取药剂记录  药剂类型 pac|nob|hci|sbs|anti
export async function queryDrug(data) {
  let res = await request(`/api/simulations/v1/record/drug?${stringify(data)}`);
  return res;
}

// 子任务列表
export async function queryMandateChildList(data) {
  let res = await request(`/api/v1/mandate-child/list?${stringify(data)}`);
  return res;
}

// 任务详情
export async function queryMandate(data) {
  let res = await request(`/api/v1/mandate/info?${stringify(data)}`);
  return res;
}

export async function getDeviceRealDataByTime(params, signal) {
  // params.size = 999999;
  return request(
    `/api/v1/jinke-cloud/db/device/history-data?${stringify(params)}`,
    {
      // return request(`/jinke-cloud/device/history-data?${stringify(params)}`, {
      method: 'GET',
      signal,
    },
  );
}

/**
 * 获取历史数据
 * @param {{project_id: string, start_time: string, end_time: string, page: string, page_size: string}} params
 * @returns
 */
export async function getHistoryRecord(params) {
  return await request(`/api/analysis/v1/io/list`, {
    method: 'POST',
    data: params,
  });
}
