2025-09-21 20:05:13 +00:00
|
|
|
import { dynamicFetch } from '@/services/form.service'
|
|
|
|
|
import { UiLookupDataSourceTypeEnum } from '@/proxy/form/models'
|
|
|
|
|
import CustomStore from 'devextreme/data/custom_store'
|
|
|
|
|
import { useCallback } from 'react'
|
|
|
|
|
|
|
|
|
|
const createLookupStaticDataSource = (
|
|
|
|
|
load: () => any,
|
|
|
|
|
filter: any = null,
|
|
|
|
|
key: any = 'key',
|
|
|
|
|
sort: any = 'name',
|
|
|
|
|
) => ({
|
|
|
|
|
store: new CustomStore({
|
|
|
|
|
key,
|
|
|
|
|
loadMode: 'raw',
|
|
|
|
|
load,
|
|
|
|
|
}),
|
|
|
|
|
sort,
|
|
|
|
|
filter,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const createLookupQueryDataSource = (
|
|
|
|
|
listFormCode?: string,
|
|
|
|
|
listFormFieldName?: string,
|
|
|
|
|
filters?: any[],
|
|
|
|
|
isSubForm?: boolean,
|
|
|
|
|
) => {
|
|
|
|
|
return new CustomStore({
|
|
|
|
|
loadMode: 'raw',
|
|
|
|
|
load: async () => {
|
|
|
|
|
if (!isSubForm && listFormCode && !window.location.pathname.includes(listFormCode)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await dynamicFetch('list-form-select/lookup', 'POST', null, {
|
|
|
|
|
listFormCode,
|
|
|
|
|
listFormFieldName,
|
|
|
|
|
filters,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return response.data.map((a: any) => ({
|
|
|
|
|
key: a.Key,
|
|
|
|
|
name: a.Name,
|
|
|
|
|
group: a.Group,
|
|
|
|
|
}))
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createLookupApiDataSource = (
|
|
|
|
|
listFormCode?: string,
|
|
|
|
|
lookupQuery?: string,
|
|
|
|
|
filters?: any[],
|
|
|
|
|
keyName?: string,
|
|
|
|
|
isSubForm?: boolean,
|
|
|
|
|
) => {
|
|
|
|
|
return new CustomStore({
|
|
|
|
|
key: keyName,
|
|
|
|
|
loadMode: 'raw',
|
|
|
|
|
load: async () => {
|
|
|
|
|
if (!isSubForm && listFormCode && !window.location.pathname.includes(listFormCode)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!lookupQuery) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [method, url, body, keySelector, nameSelector, groupSelector] = lookupQuery.split(';')
|
|
|
|
|
|
|
|
|
|
if (filters?.length) {
|
|
|
|
|
for (let i = 0; i < filters.length; i++) {
|
|
|
|
|
body.replace(`@param${i}`, filters[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await dynamicFetch(url, method, null, body)
|
|
|
|
|
let { data } = response
|
|
|
|
|
if (!data) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
|
data = [data]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data.map((a: any) => ({
|
|
|
|
|
key: eval(keySelector),
|
|
|
|
|
name: eval(nameSelector),
|
|
|
|
|
group: eval(groupSelector),
|
|
|
|
|
}))
|
|
|
|
|
} catch {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useLookupDataSource = ({
|
|
|
|
|
listFormCode,
|
|
|
|
|
isSubForm,
|
|
|
|
|
}: {
|
|
|
|
|
listFormCode: string
|
|
|
|
|
isSubForm?: boolean
|
|
|
|
|
}) => {
|
|
|
|
|
const getLookupDataSource = useCallback(
|
|
|
|
|
(options: any, colData: any) => {
|
|
|
|
|
const { lookupDto } = colData
|
|
|
|
|
const filters = []
|
|
|
|
|
if (lookupDto.cascadeParentFields) {
|
|
|
|
|
if (lookupDto.dataSourceType == UiLookupDataSourceTypeEnum.StaticData) {
|
|
|
|
|
filters.push([
|
|
|
|
|
lookupDto?.cascadeRelationField,
|
|
|
|
|
lookupDto?.cascadeFilterOperator,
|
|
|
|
|
options?.data[lookupDto?.cascadeParentField],
|
|
|
|
|
])
|
|
|
|
|
} else {
|
|
|
|
|
const data = options?.data ?? options
|
|
|
|
|
for (const cascadeParentField of lookupDto.cascadeParentFields.split(',')) {
|
2025-10-02 07:47:14 +00:00
|
|
|
filters.push(data?.[cascadeParentField] ?? null)
|
2025-09-21 20:05:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lookupDto.dataSourceType == UiLookupDataSourceTypeEnum.StaticData) {
|
|
|
|
|
return createLookupStaticDataSource(
|
|
|
|
|
() => JSON.parse(lookupDto?.lookupQuery),
|
|
|
|
|
filters.length ? filters : null,
|
|
|
|
|
)
|
|
|
|
|
} else if (lookupDto.dataSourceType == UiLookupDataSourceTypeEnum.Query) {
|
|
|
|
|
return createLookupQueryDataSource(listFormCode, colData.fieldName, filters, isSubForm)
|
|
|
|
|
} else if (lookupDto.dataSourceType == UiLookupDataSourceTypeEnum.WebService) {
|
|
|
|
|
return createLookupApiDataSource(
|
|
|
|
|
listFormCode,
|
|
|
|
|
lookupDto?.lookupQuery,
|
|
|
|
|
filters,
|
|
|
|
|
colData.lookupDto?.valueExpr?.toLowerCase(),
|
|
|
|
|
isSubForm,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
store: [],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[listFormCode, isSubForm],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return { getLookupDataSource }
|
|
|
|
|
}
|