import { get, post, put, del } from '@/utils/request' import request from '@/utils/request' export interface ProductItem { id: number principal_id: number /** 所属货主(下属公司ID),为空表示未指定 */ subsidiary_id: number | null /** 所属货主名称(列表/详情接口返回) */ subsidiary_name?: string /** 所属货主明细(列表/详情接口返回) */ subsidiary?: { id: number; name: string } | null item_code: string goods_code: string | null barcode: string | null item_name: string sku_property: string | null stock_unit: string | null /** 库存(来自 product_stocks):正品/次品的可用与锁定数量 */ zc_available?: number zc_locked?: number cc_available?: number cc_locked?: number item_type: number category_id: number | null category_name: string | null brand_id: number | null brand_name: string | null unit_id: number | null length: number | string | null // 长度,单位 cm width: number | string | null // 宽度,单位 cm height: number | string | null // 高度,单位 cm volume: number | string | null // 体积,单位 cm³ gross_weight: number | null // 毛重,单位 g net_weight: number | null // 净重,单位 g is_area_sale: string is_sn_mgmt: string is_shelf_life_mgmt: string is_batch_mgmt: string tag_price: number | string | null retail_price: number | string cost_price: number | string | null image: string | null // 主图,取 images 首张 images: string[] | null // 图片组(URL 数组),首张为主图 status: number remark: string | null create_from?: string | null stock_id: number | null warehouse_id: number | null store_id: number | null created_at?: string updated_at?: string deleted_at?: string | null } /** 商品快速搜索返回(聚合:商品编码/货物编码/条码/商品名称) */ export interface ProductSearchItem { id: number /** 所属货主(下属公司ID);null 表示未指定 */ subsidiary_id?: number | null /** 所属货主名称(接口返回,未指定为空串) */ subsidiary_name?: string item_code: string goods_code: string | null barcode: string | null item_name: string sku_property: string | null stock_unit: string | null /** 主图(搜索接口返回,用于选择列表缩略图) */ image?: string | null status: number } export interface ProductPayload { item_code: string item_name: string /** 所属货主(下属公司ID),必填 */ subsidiary_id: number goods_code?: string | null barcode?: string | null item_type?: number category_id?: number | null category_name?: string brand_id?: number | null brand_name?: string unit_id?: number | null sku_property?: string stock_unit?: string length?: number | null // 长度,单位 cm width?: number | null // 宽度,单位 cm height?: number | null // 高度,单位 cm volume?: number | null // 体积,单位 cm³ gross_weight?: number | null // 毛重,单位 g net_weight?: number | null // 净重,单位 g is_area_sale?: string is_sn_mgmt?: string is_shelf_life_mgmt?: string is_batch_mgmt?: string tag_price?: number | null retail_price: number cost_price?: number | null image?: string // 主图,取 images 首张 images?: string[] // 图片组(URL 数组),首张作为主图写入 image status?: number remark?: string } /** 当前主体下的商品列表(principal_id 由请求拦截器自动注入) */ export function getProducts(params?: object) { return get<{ list: ProductItem[]; total: number; page: number; pageSize: number }>( '/products', params, ) } /** 商品快速搜索:商品编码/货物编码/条码/商品名称 聚合匹配,返回启用商品 */ export function searchProducts(keyword?: string, params?: { subsidiary_id?: number }) { const query: Record = { ...(params || {}) } if (keyword) query.keyword = keyword return get('/products/search', Object.keys(query).length ? query : undefined) } /** 新增商品(同一主体下 item_code 唯一,已删除的不影响) */ export function createProduct(data: ProductPayload) { return post<{ id: number }>('/products', data) } /** 更新商品 */ export function updateProduct(id: number, data: Partial) { return put<{ id: number }>(`/products/${id}`, data) } /** 删除商品(软删除) */ export function deleteProduct(id: number) { return del<{ id: number }>(`/products/${id}`) } /** 批量导入:单行失败明细 */ export interface ProductImportError { row: number item_code: string message: string } /** 批量导入结果 */ export interface ProductImportResult { total: number created: number updated: number restored: number failed: number duplicated: number errors: ProductImportError[] ignored_columns: string[] } /** 下载批量导入模板(xlsx,仅表头):走鉴权拦截器,由调用方触发浏览器下载 */ export function downloadImportTemplate() { return request.get('/products/import-template', { responseType: 'blob', }) as unknown as Promise } /** 批量导入商品(xlsx;同一主体下商品编码已存在则更新,空单元格不覆盖原值) */ export function importProducts(file: File) { const form = new FormData() form.append('file', file) return post('/products/import', form) }