import { CommonDynamicSelect } from "@/api/modules/common";
import { f_checkpz } from "./f_check";

interface getMtrlWidgetDefauleValues {
  net_weight: number;
  gross_weight: number;
  cubage: number;
  packqty: number;
}

/**
 * @description 获取物料重量
 * @param mtrlid 物料id
 * @param status 配置
 * @param statusflag
 * @param defauleValues 物料的默认值， net_weight,gross_weight,cubage,packqty
 * @returns net_weight,gross_weight,cubage,packqty,
 */
export const f_get_mtrl_weight = (
  mtrlid: string | number,
  status: string,
  statusflag: number,
  defauleValues: getMtrlWidgetDefauleValues
) => {
  let result = {
    net_weight: 0,
    gross_weight: 0,
    cubage: 0,
    packqty: 0
  };
  if (statusflag == 2 && status != "") {
    let _params = {
      dsname: "web_f_get_mtrl_weight",
      queryparams: {
        mtrlid: mtrlid,
        status: status
      }
    };
    let s_pz = f_checkpz(status);
    s_pz.map(async (item: any) => {
      _params.queryparams.status = item.cfgname;
      let ret = await CommonDynamicSelect(_params);

      if (ret.datatable.length) {
        result.net_weight += Number(ret.datatable[0].net_weight) * Number(item.qty);
        result.gross_weight += Number(ret.datatable[0].gross_weight) * Number(item.qty);
        result.cubage += Number(ret.datatable[0].cubage) * Number(item.qty);
        result.packqty += Number(ret.datatable[0].packqty) * Number(item.qty);
      }
    });

    let hasZero = Object.values(result).some(value => value === 0);
    if (hasZero) {
      result.net_weight == 0 && (result.net_weight = defauleValues.net_weight);
      result.gross_weight == 0 && (result.gross_weight = defauleValues.gross_weight);
      result.cubage == 0 && (result.cubage = defauleValues.cubage);
      result.packqty == 0 && (result.packqty = defauleValues.packqty);
    }
  } else {
    result = defauleValues;
  }
  return result;
};

/**
 * @description 获取物料计划价和调拨价
 * @param mtrlid 物料id
 * @param status 配置
 * @param woodcode 配置1
 * @param pcode 配置2
 * @param pricepztype
 * @param statusflag
 * @returns planprice,dbprice
 */
export const f_get_planprice_dbprice_mtrl = async (
  mtrlid: number,
  status: string,
  woodcode: string,
  pcode: string,
  pricepztype: number,
  statusflag: number
) => {
  let ls_status, ls_woodcode, ls_pcode;
  let result = {
    planprice: 0,
    dbprice: 0
  };
  switch (pricepztype) {
    case 0:
      ls_status = "";
      ls_woodcode = "";
      ls_pcode = "";
      break;
    case 1:
      ls_status = status;
      ls_woodcode = "";
      ls_pcode = "";
      break;
    case 2:
      ls_status = "";
      ls_woodcode = woodcode;
      ls_pcode = "";
      break;
    case 3:
      ls_status = "";
      ls_woodcode = "";
      ls_pcode = pcode;
      break;
    case 4:
      ls_status = status;
      ls_woodcode = woodcode;
      ls_pcode = "";
      break;
    case 5:
      ls_status = "";
      ls_woodcode = woodcode;
      ls_pcode = pcode;
      break;
    case 6:
      ls_status = status;
      ls_woodcode = "";
      ls_pcode = pcode;
      break;
    case 7:
      ls_status = status;
      ls_woodcode = woodcode;
      ls_pcode = pcode;
      break;
  }

  let _params = {
    dsname: "web_mtrldef_planprice",
    queryparams: {
      mtrlid: mtrlid,
      status: ls_status,
      woodcode: ls_woodcode,
      pcode: ls_pcode
    }
  };
  if (statusflag == 2 && !status) {
    _params.queryparams.status = status;
    let ret = await CommonDynamicSelect(_params);
    if (ret.datatable.length) {
      result.planprice = Number(ret.datatable[0].planprice);
      result.dbprice = Number(ret.datatable[0].dbprice);
    }

    if (result.planprice == 0) {
      let s_pz = f_checkpz(status);
      s_pz.map(async (item: any) => {
        _params.queryparams.status = item.cfgname;
        ret = await CommonDynamicSelect(_params);

        if (ret.datatable.length) {
          result.planprice += Number(ret.datatable[0].planprice);
          result.dbprice += Number(ret.datatable[0].dbprice);
        } else {
          return result;
        }
      });
    }
  } else {
    let ret = await CommonDynamicSelect(_params);
    if (ret.datatable.length) {
      result.planprice = Number(ret.datatable[0].planprice);
      result.dbprice = Number(ret.datatable[0].dbprice);
    }
  }

  return result;
};

/**
 * @description 获取系统本币
 * @param typeid 类型id
 * @returns {}
 */
export const f_moneyid_native = async () => {
  let ll_moneyid = -1;

  let newParams = {
    dsname: "_Mapper_moneyid",
    queryparams: {
      native: 1
    }
  };
  let ret = await CommonDynamicSelect(newParams);

  if (ret.datatable.length) {
    ll_moneyid = Number(ret.datatable[0].moneyid);
  }

  return ll_moneyid;
};
