import * as api from '@/api';
|
/**
|
*
|
* @param structure target :{ name:属性名称,field:字段名称 } entity: {name:dataName,field} type 'object' || 'list'
|
* @returns {[{}]}
|
*/
|
const useStructureData = (structure) => {
|
const actionStructure = async (data) => {
|
structure.forEach(e => {
|
const target = data[e.target.name];
|
if (typeof target === 'object') {
|
if (target instanceof Array) {
|
e.filter = Array.from(new Set(target.map(t => t[e.target.field]).filter(Boolean)));
|
} else {
|
e.filter = [target[e.target.field]];
|
}
|
}
|
});
|
|
const result = await Promise.all(
|
structure.map(e =>
|
e.filter.length ?
|
api.entity.getEntitySet({
|
dataName: e.entity.name,
|
attachMeta: true,
|
filter: `${e.entity.field} in (${e.filter.reduce((prev, cur) => `${prev},'${cur}'`, '').substring(1)})`
|
}) :
|
Promise.resolve({data: {entityset: []}}))
|
);
|
|
structure.forEach((e, i) => {
|
const target = data[e.target.name];
|
if (typeof target === 'object') {
|
const {type} = e;
|
if (target instanceof Array) {
|
target.forEach(t => {
|
|
if (type === 'object') {
|
t[`$${e.target.field}`] = result[i].data.entityset.find(d => d[e.entity.field] === t[e.target.field]);
|
}
|
|
if (type === 'list') {
|
t[`$${e.target.field}`] = result[i].data.entityset.filter(d => d[e.entity.field] === t[e.target.field]);
|
}
|
|
});
|
} else {
|
|
if (type === 'object') {
|
target[`$${e.target.field}`] = result[i].data.entityset[0];
|
}
|
|
if (type === 'list') {
|
target[`$${e.target.field}`] = result[i].data.entityset.filter(d => d[e.entity.field] === t[e.target.field]);
|
}
|
|
}
|
}
|
});
|
|
return data;
|
}
|
|
return [actionStructure]
|
}
|
|
export default {
|
useStructureData
|
};
|