184 lines
6.0 KiB
JavaScript
184 lines
6.0 KiB
JavaScript
const ORDER_WRITABLE_FIELDS = Object.freeze([
|
||
'orderDate',
|
||
'motherName',
|
||
'expectedDeliveryDate',
|
||
'regionId',
|
||
'hospitalId',
|
||
'collectorUserId',
|
||
'regionManagerUserId',
|
||
'sampleCollectTime',
|
||
'payMode',
|
||
'contractAmount',
|
||
'orderStatus',
|
||
'sampleTypeIds',
|
||
'remark'
|
||
])
|
||
|
||
const TRANSFER_WRITABLE_FIELDS = Object.freeze([
|
||
'transferAmount',
|
||
'transferTime',
|
||
'transferType',
|
||
'transferAttachments',
|
||
'remark'
|
||
])
|
||
|
||
function clone(value) {
|
||
if (Array.isArray(value)) return value.map(clone)
|
||
if (!value || typeof value !== 'object') return value
|
||
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, clone(item)]))
|
||
}
|
||
|
||
function todayYmd() {
|
||
const date = new Date()
|
||
const year = date.getFullYear()
|
||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||
const day = String(date.getDate()).padStart(2, '0')
|
||
return `${year}${month}${day}`
|
||
}
|
||
|
||
function stringValue(value, fallback = '') {
|
||
return value == null ? fallback : String(value)
|
||
}
|
||
|
||
export function createPresignForm(value = {}, options = {}) {
|
||
const source = value && typeof value === 'object' ? value : {}
|
||
const defaultCollector = stringValue(options.currentUserId)
|
||
return {
|
||
id: stringValue(source.id),
|
||
orderDate: stringValue(source.orderDate, stringValue(options.today, todayYmd())),
|
||
protocolNo: stringValue(source.protocolNo),
|
||
motherName: stringValue(source.motherName),
|
||
expectedDeliveryDate: stringValue(source.expectedDeliveryDate),
|
||
regionId: stringValue(source.regionId),
|
||
hospitalId: stringValue(source.hospitalId),
|
||
collectorUserId: stringValue(source.collectorUserId, defaultCollector),
|
||
regionManagerUserId: stringValue(source.regionManagerUserId),
|
||
sampleCollectTime: stringValue(source.sampleCollectTime),
|
||
payMode: stringValue(source.payMode, 'dj'),
|
||
contractAmount: source.contractAmount ?? null,
|
||
orderStatus: stringValue(source.orderStatus, 'active'),
|
||
sampleTypeIds: clone(Array.isArray(source.sampleTypeIds) ? source.sampleTypeIds : []),
|
||
remark: stringValue(source.remark),
|
||
transfers: clone(Array.isArray(source.transfers) ? source.transfers : []),
|
||
confirmedDepositAmount: source.confirmedDepositAmount ?? 0
|
||
}
|
||
}
|
||
|
||
export function changePresignRegion(form, regionId) {
|
||
const next = {
|
||
...(form && typeof form === 'object' ? form : {}),
|
||
regionId: stringValue(regionId),
|
||
hospitalId: ''
|
||
}
|
||
if (Object.prototype.hasOwnProperty.call(next, 'regionManagerUserId')) {
|
||
next.regionManagerUserId = ''
|
||
}
|
||
return next
|
||
}
|
||
|
||
export function mergePersistedTransfer(transfers, target, persisted) {
|
||
const rows = Array.isArray(transfers) ? transfers : []
|
||
const targetId = String(target?.id || '')
|
||
const targetKey = String(target?._localKey || '')
|
||
return rows.map((row) => {
|
||
const sameId = targetId && String(row?.id || '') === targetId
|
||
const sameKey = targetKey && String(row?._localKey || '') === targetKey
|
||
if (row !== target && !sameId && !sameKey) return row
|
||
return {
|
||
...row,
|
||
...(persisted && typeof persisted === 'object' ? persisted : {}),
|
||
_localKey: row?._localKey || targetKey
|
||
}
|
||
})
|
||
}
|
||
|
||
export function isPresignLocked(form) {
|
||
return String(form?.orderStatus || '').trim() === 'converted'
|
||
}
|
||
|
||
export function isPresignTransferLocked(transfer, orderOrStatus) {
|
||
const orderStatus = String(
|
||
typeof orderOrStatus === 'string'
|
||
? orderOrStatus
|
||
: orderOrStatus?.orderStatus ?? transfer?.orderStatus ?? ''
|
||
).trim()
|
||
if (!['', 'draft', 'active'].includes(orderStatus)) return true
|
||
return String(transfer?.payStatus || '').trim() !== 'dfj'
|
||
}
|
||
|
||
function attachmentUrls(transfer) {
|
||
if (Array.isArray(transfer?.attachments)) {
|
||
return transfer.attachments
|
||
.filter((file) => !file?._uploading && !file?.failed && file?.status !== 'failed')
|
||
.map((file) => String(file?.fileUrl || file?.url || '').trim())
|
||
.filter(Boolean)
|
||
.join(',')
|
||
}
|
||
if (typeof transfer?.transferAttachments === 'string') {
|
||
return transfer.transferAttachments
|
||
}
|
||
return ''
|
||
}
|
||
|
||
export function buildPresignTransferPayload(transfer = {}) {
|
||
const source = transfer && typeof transfer === 'object' ? transfer : {}
|
||
const normalized = {
|
||
...source,
|
||
transferAttachments: attachmentUrls(source)
|
||
}
|
||
return Object.fromEntries(
|
||
TRANSFER_WRITABLE_FIELDS
|
||
.filter((key) => Object.prototype.hasOwnProperty.call(normalized, key))
|
||
.map((key) => [key, clone(normalized[key])])
|
||
)
|
||
}
|
||
|
||
export function buildPresignOrderPayload(form = {}, { includeTransfers = true } = {}) {
|
||
const source = form && typeof form === 'object' ? form : {}
|
||
const payload = Object.fromEntries(
|
||
ORDER_WRITABLE_FIELDS
|
||
.filter((key) => Object.prototype.hasOwnProperty.call(source, key))
|
||
.map((key) => [key, clone(source[key])])
|
||
)
|
||
if (includeTransfers && Array.isArray(source.transfers)) {
|
||
payload.transfers = source.transfers.map(buildPresignTransferPayload)
|
||
}
|
||
return payload
|
||
}
|
||
|
||
export function collectDfjTransferIds(transfers) {
|
||
const rows = Array.isArray(transfers) ? transfers : []
|
||
return rows
|
||
.filter((row) => {
|
||
const id = String(row?.id || '').trim()
|
||
const status = String(row?.payStatus || 'dfj').trim() || 'dfj'
|
||
return Boolean(id) && status === 'dfj'
|
||
})
|
||
.map((row) => String(row.id).trim())
|
||
}
|
||
|
||
export function formatAutoSubmitFinanceFailureMessage(failures) {
|
||
const list = Array.isArray(failures) ? failures.filter(Boolean) : []
|
||
if (!list.length) return ''
|
||
const reasons = list
|
||
.map((item) => String(item?.message || item || '提交失败').trim())
|
||
.filter(Boolean)
|
||
return `订单已保存,但有 ${list.length} 笔提交财务确认失败:${reasons.join(';')}`
|
||
}
|
||
|
||
export async function submitDfjTransfersOnSave(transfers, submitOne) {
|
||
const ids = collectDfjTransferIds(transfers)
|
||
const failures = []
|
||
for (const id of ids) {
|
||
try {
|
||
await submitOne(id)
|
||
} catch (error) {
|
||
failures.push({
|
||
id,
|
||
message: String(error?.message || error || '提交失败').trim() || '提交失败'
|
||
})
|
||
}
|
||
}
|
||
return failures
|
||
}
|