import request from '@/utils/http'
|
|
function normalizeText(value) {
|
return String(value ?? '').trim()
|
}
|
|
function normalizeIds(ids) {
|
if (Array.isArray(ids)) {
|
return ids
|
.map((item) => Number(item))
|
.filter((item) => Number.isFinite(item))
|
.join(',')
|
}
|
return String(ids ?? '')
|
.split(',')
|
.map((item) => Number(item))
|
.filter((item) => Number.isFinite(item))
|
.join(',')
|
}
|
|
function normalizeIdList(ids) {
|
if (Array.isArray(ids)) {
|
return ids.map((item) => Number(item)).filter((item) => Number.isFinite(item))
|
}
|
return String(ids ?? '')
|
.split(',')
|
.map((item) => Number(item))
|
.filter((item) => Number.isFinite(item))
|
}
|
|
function normalizeQueryParams(params = {}) {
|
const allowKeys = new Set([
|
'condition',
|
'code',
|
'name',
|
'spec',
|
'model',
|
'color',
|
'size',
|
'barcode',
|
'groupId',
|
'unit',
|
'status',
|
'flagCheck',
|
'validWarn'
|
])
|
const result = {
|
current: params.current || 1,
|
pageSize: params.pageSize || params.size || 20,
|
orderBy: normalizeText(params.orderBy) || 'create_time desc'
|
}
|
|
Array.from(allowKeys).forEach((key) => {
|
const value = params[key]
|
if (value === undefined || value === null || value === '') {
|
return
|
}
|
if (typeof value === 'string') {
|
const trimmed = normalizeText(value)
|
if (trimmed) {
|
result[key] = trimmed
|
}
|
return
|
}
|
result[key] = value
|
})
|
|
Object.entries(params).forEach(([key, value]) => {
|
if (['current', 'pageSize', 'size', 'orderBy'].includes(key) || allowKeys.has(key)) {
|
return
|
}
|
if (value === undefined || value === null || value === '') {
|
return
|
}
|
if (typeof value === 'string') {
|
const trimmed = normalizeText(value)
|
if (trimmed) {
|
result[key] = trimmed
|
}
|
return
|
}
|
if (Array.isArray(value)) {
|
if (value.length) {
|
result[key] = value
|
}
|
return
|
}
|
result[key] = value
|
})
|
|
return result
|
}
|
|
function normalizeGroupTreeParams(params = {}) {
|
return {
|
condition: normalizeText(params.condition)
|
}
|
}
|
|
export function fetchMatnrPage(params = {}) {
|
return request.post({
|
url: '/matnr/page',
|
params: normalizeQueryParams(params)
|
})
|
}
|
|
export function fetchMatnrDetail(id) {
|
return request.get({ url: `/matnr/${id}` })
|
}
|
|
export function fetchGetMatnrMany(ids) {
|
return request.post({ url: `/matnr/many/${normalizeIds(ids)}` })
|
}
|
|
export function fetchEnabledFields() {
|
return request.get({ url: '/fields/enable/list' })
|
}
|
|
export function fetchSaveMatnr(params = {}) {
|
return request.post({ url: '/matnr/save', params })
|
}
|
|
export function fetchUpdateMatnr(params = {}) {
|
return request.post({ url: '/matnr/update', params })
|
}
|
|
export function fetchDeleteMatnr(ids) {
|
return request.post({ url: `/matnr/remove/${normalizeIds(ids)}` })
|
}
|
|
export function fetchBindMatnrGroup(payload = {}) {
|
return request.post({
|
url: '/matnr/group/bind',
|
params: {
|
ids: normalizeIdList(payload.ids),
|
...(payload.groupId !== undefined && payload.groupId !== null && payload.groupId !== ''
|
? { groupId: Number(payload.groupId) }
|
: {})
|
}
|
})
|
}
|
|
export function fetchBatchUpdateMatnr(payload = {}) {
|
const matnr = payload.matnr && typeof payload.matnr === 'object' ? payload.matnr : {}
|
return request.post({
|
url: '/matnr/batch/update',
|
params: {
|
ids: normalizeIdList(payload.ids),
|
matnr: Object.fromEntries(
|
Object.entries(matnr).filter(
|
([, value]) => value !== undefined && value !== null && value !== ''
|
)
|
)
|
}
|
})
|
}
|
|
export function fetchMatnrGroupTree(params = {}) {
|
return request.post({
|
url: '/matnrGroup/tree',
|
params: normalizeGroupTreeParams(params)
|
})
|
}
|
|
export async function fetchExportMatnrReport(payload = {}, options = {}) {
|
return fetch(`${import.meta.env.VITE_API_URL}/matnr/export`, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
...(options.headers || {})
|
},
|
body: JSON.stringify(payload)
|
})
|
}
|
|
export async function fetchDownloadMatnrTemplate(payload = {}, options = {}) {
|
return fetch(`${import.meta.env.VITE_API_URL}/matnr/template/download`, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
...(options.headers || {})
|
},
|
body: JSON.stringify(payload)
|
})
|
}
|
|
export function fetchImportMatnr(file) {
|
const formData = new FormData()
|
formData.append('file', file)
|
return request.post({
|
url: '/matnr/import',
|
data: formData,
|
headers: {
|
'Content-Type': 'multipart/form-data'
|
}
|
})
|
}
|