import { CommonDynamicSelect } from "@/api/modules/common";
import { f_get_planprice_dbprice_mtrl } from "./f_get";

/**
 * @description 根据物料id查询物料属性
 * @param mtrlid 物料id
 * @returns {Object} 物料属性
 */
export const f_find_mtrl_by_id = async (mtrlid: any) => {
  console.log("f_find_mtrl_by_id param :>> ", mtrlid);
  if (!mtrlid) return;
  let result: any = {};

  let ret = await CommonDynamicSelect({
    dsname: "web_f_find_mtrl_by_id",
    queryparams: {
      mtrlid: mtrlid
    }
  });
  console.log("f_find_mtrl_by_id ret :>> ", ret);

  if (!ret.datatable.length) return result;

  let ret_mtrldef = ret.datatable[0];

  let { planprice, dbprice } = await f_get_planprice_dbprice_mtrl(
    ret_mtrldef.mtrlid,
    "",
    "",
    "",
    Number(ret_mtrldef.pricepztype),
    Number(ret_mtrldef.statusflag)
  );

  result = {
    ...ret_mtrldef,
    ...{
      planprice: planprice,
      dbprice: dbprice,
      rate: 1,
      if_superbom: 0
    }
  };

  if (Number(result.statusflag) == 7) {
    result.status_config = ret_mtrldef.confignew_dft;
    result.statusflag = 4;
    result.if_superbom = 1;
    result.ori_statusflag = 7;
  } else {
    result.ori_statusflag = result.statusflag;
  }

  if (Number(result.woodcodeflag) == 7) {
    result.woodcode_config = ret_mtrldef.confignew_dft;
    result.woodcodeflag = 4;
    result.if_superbom = 1;
  }

  return result;
};
