package cn.autoform.web.service.formula.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.alibaba.fastjson.TypeReference; import cn.autoform.bean.form.CpnType; import cn.autoform.db.entity.FormBaseEntity; import cn.autoform.db.entity.form.FormCpnInfo; import cn.autoform.util.tool.JSONTool; import cn.autoform.web.formula.entity.FormulaParam; import cn.autoform.web.formula.entity.FormulaParam.TriggerCondition; import cn.autoform.web.formula.entity.FormulaResult; import cn.autoform.web.formula.info.Form; import cn.autoform.web.formula.info.FormCpn; import cn.autoform.web.formula.info.FormulaInfo; import cn.autoform.web.formula.info.impl.SimpleForm; import cn.autoform.web.formula.info.impl.cpn.ChildCpn; import cn.autoform.web.formula.info.impl.cpn.FormCpnFactory; import cn.autoform.web.service.formbase.FormBaseService; import cn.autoform.web.service.formula.FormulaService; @Service public class FormulaServiceImpl implements FormulaService { @Resource private FormBaseService formBaseService; @Override public FormulaResult execute(FormulaParam formulaParam) { System.err.println(JSONTool.toJson(formulaParam)); FormBaseEntity formBaseEntity = formBaseService.getFormBaseInfo(formulaParam.getTenantID(), formulaParam.getFormID(),null); // 前端公式 List formulas = JSONTool.toList(formBaseEntity.getLinkage(), FormulaInfo.class); /* 初始化创建表单数据 */ Form form = new SimpleForm(); /* 添加控件 */ setCpn(formBaseEntity, form); /* 更新所有触发事件 */ form.setAllFormulas(formulas); /* 添加数据 */ form.setData(formulaParam.getData()); /* 表单加载 */ form.loading(); /* 为表单添加变更信息 */ setValue(form, formulaParam.getTriggers()); /* 表单提交 */ if(formulaParam.getFormCommit() != null && formulaParam.getFormCommit()){ form.commit(); } FormulaResult fr = new FormulaResult(); fr.setData(form.getData()); fr.setFormMsgs(form.getMsgs()); fr.setCpnMsgs(form.getCpnMsgs()); return fr; } /** * 为表单赋值 * @param form * @param trigList */ protected void setValue(Form form, List trigList) { if(trigList == null || trigList.isEmpty()) return; trigList.stream() .forEach(t->{ FormCpn cpn = (FormCpn) form.get(t.getAlias()); if(cpn.getCpnType() != CpnType.subform) cpn.put(FormCpn.CHILD_CPN_VAL, t.getValue()); else cpn.put(FormCpn.CHILD_CPN_VAL, t.getValue(), t.getIndex()); }); } /** * 添加控件 * @param formBaseEntity * @param form */ protected void setCpn(FormBaseEntity formBaseEntity, Form form) { Map cpns = new HashMap<>(); // 控件信息 Map fields = JSONTool.toObj(formBaseEntity.getFieldset(), new TypeReference>(){}); fields.entrySet().stream() .sorted((c1, c2)->c1.getValue().getParentsubFormNum() - c2.getValue().getParentsubFormNum()) .forEach(en->{ FormCpn cpn = FormCpnFactory.getFormCpn(en.getValue()); cpns.put(en.getValue().getColumnOrderNum(), cpn); if(en.getValue().getParentsubFormNum() == null || en.getValue().getParentsubFormNum() == 0){ form.addFormCpn(cpn); return; } ((ChildCpn)cpns.get(en.getValue().getParentsubFormNum())) .put(FormCpn.CHILD_CPN, cpn); }); } }