import { Resolver } from '../Entity'; import { config, vertexsMap, dataTypesMap, Entity, EntityProperty, Enum, View, LEVEL_ENUM, utils, } from '../..'; import { NameGroup, filterProperty, getFirstDisplayedProperty, genUniqueQueryNameGroup, getParamFromResolver, genEnumSelectBlock, genCallComponentLogic, genCallInterface, genLoadSelectLogic, } from '.'; export function genUpdateFormTemplate( entity: Entity, nameGroup: NameGroup, selectNameGroupMap: Map, ) { const propertyList = entity.propertyList .filter(filterProperty('inForm')); return ` ${propertyList.map((property) => { const label = property.label || property.name; const required = !!property.required; const rules = []; if (property.rules && property.rules.length) { property.rules.forEach((rule) => rules.push(rule)); } if (required) rules.push('required'); const rulesStr = rules.join(' | '); let formItem = ` \n`; if (property.$relationEntity) { // 有外键关联 const relationEntity = vertexsMap.get(property.$relationEntity) as Entity; if (relationEntity) { const relationProperty = vertexsMap.get(property.$relationProperty) as EntityProperty; const displayedProperty = getFirstDisplayedProperty(relationEntity, 'inForm'); if (displayedProperty) { const lowerEntityName = utils.firstLowerCase(relationEntity.name); formItem += ` `; } else return ''; } else return ''; } else if (property.typeKey === '#/basicTypes/Boolean') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Integer' || property.typeKey === '#/basicTypes/Long') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Decimal') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Text') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Date') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Time') { formItem += ``; } else if (property.typeKey === '#/basicTypes/DateTime') { formItem += ``; } else if (dataTypesMap[property.typeKey].type === 'enum') { formItem += genEnumSelectBlock(dataTypesMap[property.typeKey] as Enum, `${nameGroup.model}.${property.name}`, '', `请选择${label}`); } else { formItem += ``; } formItem += ` \n`; return formItem; }).join('')} 提交修改 `; } export function genH5UpdateFormTemplate(entity: Entity, nameGroup: NameGroup, selectNameGroupMap: Map) { const propertyList = entity.propertyList .filter(filterProperty('inForm')); return ` ${propertyList.map((property) => { const label = property.label || property.name; const required = !!property.required; const rules = []; if (property.rules && property.rules.length) { property.rules.forEach((rule) => rules.push(rule)); } if (required) rules.push('required'); const rulesStr = rules.join(' | '); let formItem = ` \n`; if (property.$relationEntity) { // 有外键关联 const relationEntity = vertexsMap.get(property.$relationEntity) as Entity; if (relationEntity) { const relationProperty = vertexsMap.get(property.$relationProperty) as EntityProperty; const displayedProperty = getFirstDisplayedProperty(relationEntity, 'inForm'); if (displayedProperty) { const lowerEntityName = utils.firstLowerCase(relationEntity.name); formItem += ` `; } else return ''; } else return ''; } else if (property.typeKey === '#/basicTypes/Boolean') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Integer' || property.typeKey === '#/basicTypes/Long') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Decimal') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Text') { formItem = formItem.replace(/other/g, ''); formItem += ``; // @TODO } else if (property.typeKey === '#/basicTypes/Date') { formItem += ``; } else if (property.typeKey === '#/basicTypes/Time') { formItem += ``; } else if (property.typeKey === '#/basicTypes/DateTime') { formItem += ``; } else if (dataTypesMap[property.typeKey].type === 'enum') { formItem += ''; } else { formItem = formItem.replace(/other/g, ''); formItem += ``; } formItem += ` \n`; formItem = formItem.replace(/>\n/g, ''); return formItem; }).join('')}
`; } function genSubmitLogic(resolver: Resolver, nameGroup: NameGroup) { const bodyParam = getParamFromResolver(resolver, 'body'); return { level: 'logic', name: nameGroup.submit, params: [] as any, returns: [] as any, variables: [ { level: 'variable', type: 'Identifier', name: 'validateResult', description: '', schema: { $ref: '#/systemTypes/ValidateEvent', }, isLeaf: true, }, ], body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'logicNode', type: 'AssignmentExpression', label: '赋值', operator: '=', left: { level: 'expressionNode', type: 'Identifier', name: 'validateResult', }, right: genCallComponentLogic('form1', 'validate'), }, { level: 'logicNode', type: 'IfStatement', label: '条件分支', test: { level: 'expressionNode', type: 'MemberExpression', name: 'name', hide: false, object: { level: 'expressionNode', type: 'Identifier', name: 'validateResult', }, property: { level: 'expressionNode', type: 'Identifier', name: 'valid', }, }, consequent: [ genCallInterface(resolver.interface, [ { parentAttr: 'params', level: LEVEL_ENUM.param, type: 'CallInterParam', name: '', callInterParam: `#/${bodyParam.id}`, callInterParamValue: { parentAttr: 'callInterParamValue', level: 'expressionNode', type: 'Identifier', name: nameGroup.model, }, }, ]), { level: 'logicNode', parentAttr: 'consequent', label: '弹出消息', folded: false, type: 'CallMessageShow', arguments: [ { level: 'expressionNode', parentAttr: 'arguments', label: '原子项', folded: false, type: 'StringLiteral', value: '修改成功!', }, ], }, ], alternate: [] as any, }, { level: 'logicNode', type: 'End', label: '结束', }, ], }; } function genLoadLogic(resolver: Resolver, nameGroup: NameGroup) { const getResolver = resolver.entity.resolvers.find((item) => item.name === 'get'); const idParam = getParamFromResolver(getResolver, 'id'); return { level: 'logic', expanded: false, name: nameGroup.load, description: '', params: [] as any, returns: [] as any, body: [ { level: 'logicNode', type: 'Start', label: '开始', }, { level: 'logicNode', type: 'AssignmentExpression', label: '赋值', operator: '=', left: { level: 'expressionNode', type: 'Identifier', name: nameGroup.model, }, right: genCallInterface(getResolver.interface, [ { parentAttr: 'params', level: LEVEL_ENUM.param, type: 'CallInterParam', name: '', callInterParam: `#/${idParam.id}`, callInterParamValue: { parentAttr: 'callInterParamValue', level: 'expressionNode', type: 'Identifier', name: 'id', }, }, ]), }, { level: 'logicNode', type: 'End', label: '结束', }, ], playground: [] as any, serviceType: 'web', }; } export function genUpdateBlock(entity: Entity, view: View) { const resolver = entity.resolvers.find((item) => item.name.startsWith('update')); const modelSchema = { $ref: entity.schemaRef, }; const existingNameSets = { viewLogic: new Set(view.$def.logics.map((logic) => logic.name)), interface: new Set(entity.dataNode.service.interfaces.map((itface) => itface.name)), structure: new Set(entity.dataNode.structures.map((structure) => structure.name)), }; const nameGroup: NameGroup = { model: utils.firstLowerCase(entity.name), }; nameGroup.load = utils.unique('load', existingNameSets.viewLogic); nameGroup.submit = utils.unique('submit', existingNameSets.viewLogic); // 收集所有和本实体关联的实体 const allEntities = [entity]; entity.propertyList.forEach((property) => { if (property.$relationEntity) { // 有外键关联 const relationEntity = vertexsMap.get(property.$relationEntity) as Entity; if (relationEntity) { const displayedProperty = getFirstDisplayedProperty(relationEntity); if (displayedProperty) allEntities.push(relationEntity); } } }); const newStructures: Array = []; const newInterfaces: Array = []; const selectNameGroupMap: Map = new Map(); const loadSelectLogics = allEntities.slice(1).map((entity) => { const selectNameGroup = genUniqueQueryNameGroup(existingNameSets, view.name, 'select', false, entity.name); selectNameGroupMap.set(entity.id, selectNameGroup); return genLoadSelectLogic(entity, selectNameGroup, newStructures, newInterfaces); }); return ` { "params": [ { "level": "param", "type": "Identifier", "name": "id", "description": "", "schema": { "type": "integer", "format": "long", "isLeaf": true }, "isLeaf": true } ], "variables": [{ "level": "variable", "type": "Identifier", "name": "${nameGroup.model}", "schema": ${JSON.stringify(modelSchema)} }], "lifecycles": [ { "level": "lifecycle", "name": "created", "logicId": "${nameGroup.load}" } ], "logics": [ ${JSON.stringify(genLoadLogic(resolver, nameGroup))}, ${JSON.stringify(genSubmitLogic(resolver, nameGroup))} ${loadSelectLogics.map((logic) => ',' + JSON.stringify(logic)).join('\n')} ], "interfaces": ${JSON.stringify(newInterfaces)}, "structures": ${JSON.stringify(newStructures)} } `; } export default genUpdateBlock;