import request from '@/config/axios'

/**
 * 产品BOM明细 VO
 */
export interface ProductBomItemVO {
  id?: number
  bomId?: number
  materialId: number
  materialCode?: string
  materialName?: string
  materialType?: string
  unit?: string
  unitQuantity: number
  lossRate?: number
  purchaseLeadTime?: number
  unitPrice?: number
  required?: number
  sort?: number
  remark?: string
}

/**
 * 产品BOM VO
 */
export interface ProductBomVO {
  id?: number
  bomCode?: string
  bomName?: string
  productId: number
  productCode?: string
  productName?: string
  version?: string
  isDefault?: number
  remark?: string
  status?: number
  createTime?: string
  items?: ProductBomItemVO[]
}

/**
 * 产品BOM 分页查询参数
 */
export interface ProductBomPageReqVO {
  pageNo: number
  pageSize: number
  productId?: number
  productName?: string
  bomCode?: string
  bomName?: string
  status?: number
}

/**
 * 创建产品BOM
 */
export function createProductBom(data: ProductBomVO) {
  return request.post({ url: '/mes/product-bom/create', data })
}

/**
 * 更新产品BOM
 */
export function updateProductBom(data: ProductBomVO) {
  return request.put({ url: '/mes/product-bom/update', data })
}

/**
 * 删除产品BOM
 */
export function deleteProductBom(id: number) {
  return request.delete({ url: '/mes/product-bom/delete', params: { id } })
}

/**
 * 获得产品BOM（含明细）
 */
export function getProductBom(id: number) {
  return request.get<ProductBomVO>({ url: '/mes/product-bom/get', params: { id } })
}

/**
 * 获得产品BOM分页
 */
export function getProductBomPage(params: ProductBomPageReqVO) {
  return request.get({ url: '/mes/product-bom/page', params })
}

/**
 * 根据产品ID获取BOM列表
 */
export function getBomListByProductId(productId: number) {
  return request.get<ProductBomVO[]>({ url: '/mes/product-bom/list-by-product', params: { productId } })
}

/**
 * 获取BOM明细列表
 */
export function getBomItems(bomId: number) {
  return request.get<ProductBomItemVO[]>({ url: '/mes/product-bom/items', params: { bomId } })
}

/**
 * 获取产品的默认BOM
 */
export function getDefaultBom(productId: number) {
  return request.get<ProductBomVO>({ url: '/mes/product-bom/default', params: { productId } })
}
