package com.changhong.epc.bean.form;
|
|
import com.changhong.epc.constter.parsing.journey.JourneyKey;
|
import com.iemsoft.framework.cloud.core.tools.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.util.*;
|
import java.util.Map.Entry;
|
|
/**
|
* 自定义表单转换实体
|
* @author wangZX
|
*
|
*/
|
@Data
|
@Slf4j
|
public class Fieldsets implements JourneyKey {
|
|
|
/**
|
* 主控件
|
*/
|
List<CpnVal> mainCpn = new ArrayList<>();
|
|
/**
|
* 子表单 包含的控件
|
*/
|
Map<CpnVal,List<CpnVal>> subform = new HashMap<>();
|
|
//关联表单控件,在使用关联表单时,所关联表单ID
|
String assoFormId ;
|
|
//所有控件的tagAttribute
|
Map<String, Map<String,Object>> allTagArr = new HashMap<>();
|
|
public Fieldsets(){
|
}
|
|
public Fieldsets(FormBaseEntity formBaseEntity){
|
Fieldsets formData = formData(formBaseEntity);
|
// System.err.println(JSONTool.toJson(formData));
|
mainCpn = formData.getMainCpn();
|
subform = formData.getSubform();
|
// System.err.println(JSONTool.toJson(mainCpn));
|
// System.err.println(JSONTool.toJson(subform));
|
}
|
|
/**
|
*
|
* @param formBaseEntity
|
* @return
|
*/
|
|
public Fieldsets formData(FormBaseEntity formBaseEntity){
|
|
Fieldsets fieldsets = new Fieldsets();
|
|
String fieldsetJson = formBaseEntity.getFieldset();
|
Map<String, Map<String,Object>> Fieldset = JSONTool.toObj(fieldsetJson, Map.class);
|
|
List<CpnVal> subformCpns = new ArrayList<>();
|
if(ObjectUtil.notEmpty(Fieldset.values())) {
|
for (Map<String, Object> eachCpn : Fieldset.values()) {
|
CpnVal cpnVal = mapToDaom(eachCpn);
|
|
// 找到一个子表单
|
if (Objects.equals(cpnVal.getTagType(), SUBFORM) &&
|
Objects.equals(cpnVal.getParentsubFormNum(), ROOT_ID)) {
|
fieldsets.getSubform().put(cpnVal, new ArrayList<>());
|
|
// 找到一个根控件
|
} else if (Objects.equals(cpnVal.getParentsubFormNum(), ROOT_ID)) {
|
fieldsets.getMainCpn().add(cpnVal);
|
} else {
|
// 找到一个子表单里的控件
|
subformCpns.add(cpnVal);
|
}
|
}
|
}
|
// 将子表单控件添加到 自己的子表单
|
for (CpnVal cpnVal : subformCpns) {
|
CpnVal cpnValKey = getCpnValKey(fieldsets, fieldsets.getSubform(), cpnVal);
|
List<CpnVal> cpns = fieldsets.getSubform().get(cpnValKey);
|
if(cpns!= null)
|
cpns.add(cpnVal);
|
}
|
|
return fieldsets;
|
}
|
|
/**
|
* 将数据封装到 表结构中
|
* @param data
|
* @return
|
*/
|
@SuppressWarnings("unchecked")
|
public Map<CpnVal,List<List<CpnVal>>> fillCpnVal(Map<CpnVal,List<Map>> data){
|
Map<CpnVal,List<List<CpnVal>>> result = new HashMap<>();
|
|
// 添加字表单里 的值
|
for (Entry<CpnVal, List<Map>> formDatas : data.entrySet()) {
|
List<List<CpnVal>> subformVals = result.get(formDatas.getKey());
|
if(subformVals == null){
|
subformVals = new ArrayList<>();
|
result.put(formDatas.getKey(), subformVals);
|
}
|
List<CpnVal> cnpValStruct = null;
|
// 主控件
|
if(formDatas.getKey() == null){
|
cnpValStruct = copyCpnVal(getMainCpn());
|
}else{
|
cnpValStruct = copyCpnVal(getSubform().get(formDatas.getKey()));
|
}
|
//出错
|
for(Map<String, Object> s :formDatas.getValue()){
|
List<CpnVal> temp = copyCpnVal(fillCpnVals(cnpValStruct,s));
|
subformVals.add(temp);
|
}
|
}
|
|
return result;
|
|
}
|
|
/**
|
* 向控件里赋值
|
* @param cnpValStruct
|
* @param
|
* @return
|
*/
|
private List<CpnVal> fillCpnVals(List<CpnVal> cnpValStruct, Map<String, Object> data) {
|
cnpValStruct.forEach(s->s.setValCode(getKey(data,s.getAlias())));
|
return cnpValStruct;
|
}
|
|
public String getKey(Map<String, Object> data, String key){
|
|
for (String k : data.keySet()) {
|
if(StringUtils.equalsIgnoreCase(k, key)){
|
return Objects.toString(data.get(k),"");
|
}
|
}
|
return "";
|
}
|
|
/**
|
*
|
* @param cpnVals
|
* @return
|
*/
|
private List<CpnVal> copyCpnVal(List<CpnVal> cpnVals){
|
return JSONTool.toList(JSONTool.toJson(cpnVals), CpnVal.class);
|
}
|
|
/**
|
* 将控件Map 转换成实体 CpnVal
|
* @param eachCpn
|
* @return
|
*/
|
private CpnVal mapToDaom(Map<String, Object> eachCpn){
|
CpnVal cpnVal = new CpnVal();
|
String tagId = Objects.toString(eachCpn.get(TAG_ID), "") ;
|
String tagName = Objects.toString(eachCpn.get(TAG_NAME), "") ;
|
String tagType = Objects.toString(eachCpn.get(TAG_TYPE), "") ;
|
String columnOrderNum = Objects.toString(eachCpn.get(COLUMN_ORDERNUM), "") ;
|
String parentsubFormNum = Objects.toString(eachCpn.get(PARENT_SUBFOR_MNUM), "");
|
String chooseForm = "";
|
String mdCode = "";
|
String alias = "";
|
// formdata fundata
|
String relevanCesourceType = "";
|
String fundata = "";
|
String singleSetValue = "";
|
Map<String,Object> tagAttribute;
|
try {
|
tagAttribute = (Map<String, Object>) eachCpn.get(TAG_ATTRIBUTE);
|
} catch (Exception e) {
|
tagAttribute = null;
|
}
|
if(tagAttribute != null){
|
alias = Objects.toString(tagAttribute.get(ALIAS), "");
|
this.allTagArr.put(alias, tagAttribute);
|
if(StringUtils.isNoneBlank(tagType)){
|
relevanCesourceType = Objects.toString(tagAttribute.get(RELEVAN_CESOURCE_TYPE), "");
|
//11.23 yuezhw
|
// Object fun_data = !Objects.isNull(relevanCesourceType) ? tagAttribute.get(relevanCesourceType) : null;
|
//原先
|
Object fun_data = tagAttribute.get(FUN_DATA);
|
if(fun_data != null && !"".equals(fun_data.toString()) && FUN_DATA.equals(relevanCesourceType)){
|
Object funkey = JSONTool.toObj((String)fun_data, Map.class).get("key");
|
fundata = Objects.toString(funkey, "");
|
}
|
}
|
chooseForm = Objects.toString(tagAttribute.get(CHOOSEFORM), "");
|
try{
|
if(!"".equals(chooseForm)){
|
Map map = JSONTool.toObj(chooseForm, Map.class);
|
this.assoFormId = JSONTool.toObj(map.get("formID").toString(), Map.class).get("formID").toString();
|
}
|
}catch(Exception e){
|
|
}
|
|
Map obj;
|
if(tagAttribute.get(SINGLE_SET_VALUE) != null
|
&& (obj = JSONTool.toObj(Objects.toString(
|
tagAttribute.get(SINGLE_SET_VALUE)), Map.class)) != null) {
|
singleSetValue = Objects.toString(obj.get(ALIAS), "");
|
}
|
|
String maste = "";
|
try {
|
maste = tagAttribute.get("masterData").toString();
|
} catch (Exception e) {
|
|
}
|
if(StringUtils.isNoneBlank(maste)){
|
mdCode = Objects.toString(JSONTool.toObj(maste, Map.class).get("defindCode"), "") ;
|
}
|
}
|
|
cpnVal.setTagId(tagId);
|
cpnVal.setTagName(tagName);
|
cpnVal.setTagType(tagType);
|
cpnVal.setColumnOrderNum(columnOrderNum);
|
cpnVal.setParentsubFormNum(parentsubFormNum);
|
|
cpnVal.setChooseForm(chooseForm);
|
|
cpnVal.setMdCode(mdCode);
|
cpnVal.setAlias(alias);
|
cpnVal.setFundata(fundata);
|
cpnVal.setRelevanCesourceType(relevanCesourceType);
|
cpnVal.setSingleSetValue(singleSetValue);
|
return cpnVal;
|
}
|
|
/**
|
* 寻找字表单Key
|
* @param subform
|
* @param eachCpnVal
|
* @return
|
*/
|
private CpnVal getCpnValKey(Fieldsets fieldsets, Map<CpnVal,List<CpnVal>> subform, CpnVal eachCpnVal){
|
Set<CpnVal> keySet = fieldsets.getSubform().keySet();
|
for (CpnVal cpnVal : keySet) {
|
if(Objects.equals(cpnVal.getColumnOrderNum(), eachCpnVal.getParentsubFormNum())){
|
return cpnVal;
|
}
|
}
|
return new CpnVal();
|
}
|
|
|
}
|