zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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<FormulaInfo> 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<TriggerCondition> 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<Integer, FormCpn> cpns = new HashMap<>();
        // 控件信息
        Map<String, FormCpnInfo> fields = JSONTool.toObj(formBaseEntity.getFieldset(), new TypeReference<Map<String, FormCpnInfo>>(){});
        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);
        });
    }
    
}