package com.changhong.epc.count.service.system.impl;
|
|
import com.changhong.epc.bean.count.format.CpnInfo;
|
import com.changhong.epc.bean.count.format.FormInfo;
|
import com.changhong.epc.bean.count.format.ItemInfo;
|
import com.changhong.epc.bean.count.format.SystemFunFormInfo;
|
import com.changhong.epc.bean.tenant.system.ErrSystemMsg;
|
import com.changhong.epc.count.service.count.data.paramiter.impl.ParamIter;
|
import com.changhong.epc.count.service.count.model.CountParamMax;
|
import com.changhong.epc.count.service.count.model.CountResultLast;
|
import com.changhong.epc.count.service.format.FormatFormInfo;
|
import com.changhong.epc.count.service.system.IExpendSystemCount;
|
import com.changhong.epc.count.service.system.performer.IFormulaHandle;
|
import com.changhong.epc.count.service.system.performer.impl.FunctionParam;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* 系统变量计算
|
*/
|
@Service
|
@Slf4j
|
public class IExpendSystemCountImpl implements IExpendSystemCount {
|
|
@Resource
|
private FormatFormInfo<CountParamMax, CountResultLast> formatFormInfoCount;
|
|
@Override
|
public FormInfo systemCount(SystemFunFormInfo systemFunFormInfo) {
|
//格式化参数
|
CountParamMax p = formatFormInfoCount.formatParam(systemFunFormInfo.getFormInfo());
|
ParamIter paramIter = SpringUtil.getBean(ParamIter.class).setDataSource(p, Collections.EMPTY_LIST);
|
List<ErrSystemMsg> errSystemMsgs = new ArrayList<>();
|
// 循环计算系统变量
|
systemFunFormInfo.getData().stream().forEach(data -> {
|
if(ObjectUtil.empty(data)){
|
data.setVal(null);
|
return;
|
}
|
if(data.getJourneyIndex() != null && data.getJourneyIndex() < paramIter.getOldUnitInfos().size()) {
|
paramIter.setIndex(data.getJourneyIndex());
|
}
|
FunctionParam functionParam = new FunctionParam();
|
functionParam.initData(paramIter);
|
functionParam.prepare(data.getId());
|
if(functionParam.getSystemMath() == null){
|
return;
|
}
|
IFormulaHandle iFormulaHandle = SpringUtil.getBean(IFormulaHandle.class);
|
try{
|
data.setVal(iFormulaHandle.execute(functionParam));
|
}catch (Exception e){
|
log.error("系统变量计算错误, id:{}", data.getId());
|
log.error(e.getMessage(), e);
|
}
|
// 记录主数据缺失错误
|
if(ObjectUtil.notEmpty(iFormulaHandle.getErrSystemMsgs())) {
|
errSystemMsgs.addAll(iFormulaHandle.getErrSystemMsgs());
|
}
|
});
|
// 赋值给表单信息
|
systemFunFormInfo.getData().stream().forEach(data -> {
|
if(ObjectUtil.empty(data) || data.getVal() == null){
|
return;
|
}
|
CpnInfo cpnInfo;
|
if(isSubCpnInfo(data.getFieldKey())){
|
cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo(), data.getJourneyIndex());
|
}else{
|
cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo());
|
}
|
if(cpnInfo == null)
|
cpnInfo.setValue(data.getVal());
|
});
|
// 添加主数据缺失信息
|
systemFunFormInfo.getFormInfo().setErrSystemMsgs(errSystemMsgs);
|
return systemFunFormInfo.getFormInfo();
|
}
|
|
protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo){
|
return getCpnInfo(fieldKey, formInfo.getData());
|
}
|
|
protected CpnInfo getCpnInfo(String fieldKey, List<CpnInfo> cpnInfoList){
|
if(ObjectUtil.empty(cpnInfoList)){
|
return null;
|
}
|
for (CpnInfo cpnInfo: cpnInfoList) {
|
if(Objects.equals(cpnInfo.getFieldKey(), fieldKey)){
|
return cpnInfo;
|
}
|
}
|
return null;
|
}
|
|
protected boolean isSubCpnInfo(String fieldKey){
|
return fieldKey.indexOf('.') >= 0;
|
}
|
|
protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo, Integer index){
|
String[] fieldKeys = fieldKey.split("[.]");
|
if(fieldKeys.length != 2){
|
return null;
|
}
|
CpnInfo subCpnInfo = getCpnInfo(fieldKeys[0], formInfo);
|
ItemInfo itemInfo = subCpnInfo.getChild().get(index);
|
return getCpnInfo(fieldKeys[1], itemInfo.getItems());
|
}
|
|
}
|