customerInfo.pagination.js 2.1 KB
/**
 * @file 客户档案弹框分页纯函数,供 CustomerInfo 组件与单测复用。
 */

/** 客户档案弹框每页条数 */
const DEFAULT_PAGE_SIZE = 20

/**
 * 构建按工单查询客户档案的分页参数。
 * lowPriceFlag 仅在调用页显式传 0 或 1 时下发,避免改变共享组件其他消费者的查询口径。
 *
 * @param {Object} options 查询上下文
 * @param {number|string} [options.merchantId] 组件传入的门店 ID
 * @param {number|string} [options.fallbackMerchantId] 当前登录用户的门店 ID
 * @param {number} options.pageNum 当前页码
 * @param {string} [options.keyword] 搜索关键字
 * @param {number|string|null} [options.lowPriceFlag] 低价工单查询标记;null/undefined 不下发
 * @returns {Object} getWorkPackageGoodsList 请求参数
 */
function buildCustomerListRequestParams({
  merchantId,
  fallbackMerchantId,
  pageNum,
  keyword = '',
  lowPriceFlag
}) {
  const params = {
    merchantId: merchantId || fallbackMerchantId,
    pageNum,
    pageSize: DEFAULT_PAGE_SIZE,
    statusList: [1],
    keyword
  }

  if ([0, 1, '0', '1'].includes(lowPriceFlag)) {
    params.lowPriceFlag = Number(lowPriceFlag)
  }

  return params
}

/**
 * 是否还能加载下一页(与工单首页 onReachBottom 判断一致)
 * @param {number} total 总条数
 * @param {number} pageNum 当前已加载页码
 * @param {number} [pageSize=20] 每页条数
 * @returns {boolean}
 */
function canLoadMorePage(total, pageNum, pageSize = DEFAULT_PAGE_SIZE) {
  if (!total || !pageNum) return false
  return Math.ceil(total / pageSize) > pageNum
}

/**
 * 为订单列表项补充车型展示字段
 * @param {Array} list 接口返回的 list
 * @returns {Array}
 */
function mapCustomerListWithCarType(list) {
  if (!list?.length) return []
  return list.map((item) => {
    if (item.customer?.carSeriesName) {
      item.carType = `${item.customer.carBrand || ''}-${item.customer.carChildsBrandName || ''}-${item.customer.carSeriesName || ''}`
    }
    return item
  })
}

export {
  DEFAULT_PAGE_SIZE,
  buildCustomerListRequestParams,
  canLoadMorePage,
  mapCustomerListWithCarType
}