groundingUpgradeList.js 4.82 KB
/**
 * merchantGoodsInfo.rule.selectList 返回的扁平 list(可与 merchantGoodsCode 独立的 commodityCode)
 * → 分账「具体商品」下拉统一结构
 * @param {Array<{upgradeGoodsId?: *, goodsShowName?: *, packageName?: *, merchantGoodsCode?: *, commodityCode?: *}>|undefined} list
 * @returns {Array<{upgradeGoodsId: *, upgradeGoodsName: string, commodityCode: string, merchantGoodsCode: string}>}
 */
export function mapProductSearchListToUpgradeGoodsOptions(list) {
  return (list || [])
    .map((row) => {
      const upgradeGoodsId = row && row.upgradeGoodsId
      const goodsName =
        row && row.goodsShowName != null ? String(row.goodsShowName) : ""
      let pkg = ""
      if (row && row.packageName != null) {
        const t = String(row.packageName).trim()
        if (t !== "" && t !== "null" && t !== "undefined") pkg = t
      }
      const upgradeGoodsName = pkg ? `${pkg}-${goodsName}` : goodsName
      const rawCode = row && row.merchantGoodsCode
      const merchantGoodsCode =
        rawCode != null && String(rawCode).trim() !== ""
          ? String(rawCode).trim()
          : ""
      const commodityCode = trimCommodityCode(row)
      return { upgradeGoodsId, upgradeGoodsName, commodityCode, merchantGoodsCode }
    })
    .filter((row) => row.merchantGoodsCode !== "")
}

/**
 * 仅读取 row.commodityCode(trim),与 merchantGoodsCode 分离,不用商户码回填。
 * @param {object|undefined} row
 * @returns {string} trim 后非空则返回,否则 ""
 */
export function trimCommodityCode(row) {
  if (!row || typeof row !== "object") return ""
  if (row.commodityCode == null) return ""
  const t = String(row.commodityCode).trim()
  if (t === "" || t === "null" || t === "undefined") return ""
  return t
}

/**
 * @param {object|undefined} row
 * @returns {string} trim 后非空则返回,否则 ""
 */
export function trimMerchantGoodsCode(row) {
  if (!row || typeof row !== "object") return ""
  if (row.merchantGoodsCode == null) return ""
  const t = String(row.merchantGoodsCode).trim()
  if (t === "" || t === "null" || t === "undefined") return ""
  return t
}

/**
 * 仅保留含非空 trim 后 merchantGoodsCode 的具体商品行(不依赖 commodityCode)。
 * @param {Array|undefined} arr
 * @returns {Array}
 */
export function filterUpgradeGoodsByMerchantCode(arr) {
  return (arr || []).filter((row) => trimMerchantGoodsCode(row) !== "")
}

/**
 * 具体商品行主键:仅 merchantGoodsCode(trim);无码返回 ""
 * @param {object|undefined} row
 * @returns {string}
 */
export function getUpgradeGoodsRowKey(row) {
  const code = trimMerchantGoodsCode(row)
  if (!code) return ""
  return `c:${code}`
}

/**
 * @param {object|undefined} a
 * @param {object|undefined} b
 * @returns {boolean}
 */
export function sameUpgradeGoodsRow(a, b) {
  const ka = getUpgradeGoodsRowKey(a)
  const kb = getUpgradeGoodsRowKey(b)
  return ka !== "" && ka === kb
}

/**
 * @deprecated 旧版 list 项含 upgradeGoodsList 嵌套;现具体商品下拉已改用 mapProductSearchListToUpgradeGoodsOptions
 */
export function flatMapUpgradeGoodsFromGoodsListPage(list) {
  return (list || []).reduce((arr, val) => {
    ;(val.upgradeGoodsList || []).forEach((el) => {
      const { upgradeGoodsName, id, merchantGoodsCode: mg } = el
      const code = mg != null && String(mg).trim() !== "" ? String(mg).trim() : ""
      if (!code) return
      arr.push({
        upgradeGoodsId: id,
        upgradeGoodsName,
        commodityCode: trimCommodityCode(el),
        merchantGoodsCode: code
      })
    })
    return arr
  }, [])
}

/**
 * selectList 分页:优先用 res.pages;缺失时按本页条数推断,避免 loadmore 死循环。
 * @param {*} res 接口返回
 * @param {number} currentPage 当前请求页(1-based)
 * @param {number} pageSize
 * @returns {number} 总页数(至少为 1)
 */
export function resolveProductSearchPages(res, currentPage, pageSize) {
  if (res == null) return Math.max(1, currentPage || 1)
  const p = res.pages
  if (p != null && p !== "" && !Number.isNaN(Number(p)) && Number(p) >= 1) {
    return Number(p)
  }
  const list = res.list || []
  const cp = currentPage || 1
  const ps = pageSize || 20
  if (list.length === 0) return cp
  if (list.length < ps) return cp
  return cp + 1
}

export function uniqueUpgradeGoodsById(arr) {
  const res = new Map()
  return (arr || []).filter((a) => {
    const k = getUpgradeGoodsRowKey(a)
    if (!k) return false
    return !res.has(k) && res.set(k, 1)
  })
}

/**
 * 套餐商品列表按 packageCode 去重,保留首次出现顺序(grounding 分页与下拉共用)。
 * @param {Array<{packageCode: unknown}>|undefined} arr
 * @returns {Array<{packageCode: unknown}>}
 */
export function uniquePackageByPackageCode(arr) {
  const res = new Map()
  return (arr || []).filter(
    (a) => !res.has(a.packageCode) && res.set(a.packageCode, 1)
  )
}