feat(打印):js解析获取不到栏位问题

This commit is contained in:
syngna_qiao 2024-11-26 18:07:08 +08:00
parent f2ebfb636a
commit 16de7dc372
2 changed files with 28 additions and 24 deletions

View File

@ -217,26 +217,26 @@ public class PrintServiceImpl implements PrintService {
Map<String, Object> baseData = operationPrintRequest.getBaseData();
if(baseData == null) {
baseData = new HashMap<>();
// 将operationPrintRequest中的字段逐一拷贝到baseData
String shopOrderHandle = ShopOrder.genHandle(site, operationPrintRequest.getShopOrder());
MapUtil.copyPropertiesToMap(operationPrintRequest, baseData);
String lotBasisBo = LotBasis.genHandle(site, lotNo);
LotBasis lotBasis = lotBasisMapper.selectById(lotBasisBo);
LotState lotState = lotStateMapper.selectById(LotState.genHandle(lotBasisBo));
ShopOrder shopOrder = shopOrderMapper.selectById(shopOrderHandle);
Item item = lotBasis == null ? null : itemMapper.selectById(lotBasis.getItemBo());
List<CustomFields> soCustomFieldsList = customFieldsMapper.selectList(Wrappers.<CustomFields>lambdaQuery().eq(CustomFields::getHandle, shopOrderHandle));
// 将customFieldsList转换为Map
Map<String, String> customFieldsMap = new HashMap();
if(soCustomFieldsList != null && soCustomFieldsList.size() > 0) {
customFieldsMap = soCustomFieldsList.stream().collect(Collectors.toMap(CustomFields::getFieldName, CustomFields::getFieldValue));
}
baseData.put("lotBasisData", lotBasis);
baseData.put("lotStateData", lotState);
baseData.put("shopOrderData", shopOrder);
baseData.put("itemData", item);
baseData.put("shopOrderCustomData", customFieldsMap);
}
// 将operationPrintRequest中的字段逐一拷贝到baseData
String shopOrderHandle = ShopOrder.genHandle(site, operationPrintRequest.getShopOrder());
MapUtil.copyPropertiesToMap(operationPrintRequest, baseData);
String lotBasisBo = LotBasis.genHandle(site, lotNo);
LotBasis lotBasis = lotBasisMapper.selectById(lotBasisBo);
LotState lotState = lotStateMapper.selectById(LotState.genHandle(lotBasisBo));
ShopOrder shopOrder = shopOrderMapper.selectById(shopOrderHandle);
Item item = lotBasis == null ? null : itemMapper.selectById(lotBasis.getItemBo());
List<CustomFields> soCustomFieldsList = customFieldsMapper.selectList(Wrappers.<CustomFields>lambdaQuery().eq(CustomFields::getHandle, shopOrderHandle));
// 将customFieldsList转换为Map
Map<String, String> customFieldsMap = new HashMap();
if(soCustomFieldsList != null && soCustomFieldsList.size() > 0) {
customFieldsMap = soCustomFieldsList.stream().collect(Collectors.toMap(CustomFields::getFieldName, CustomFields::getFieldValue));
}
baseData.put("lotBasisData", lotBasis);
baseData.put("lotStateData", lotState);
baseData.put("shopOrderData", shopOrder);
baseData.put("itemData", item);
baseData.put("shopOrderCustomData", customFieldsMap);
List<Map<String, Object>> baseDataList = Arrays.asList(baseData);
try {
//获取打印模板和打印次数
@ -621,9 +621,10 @@ public class PrintServiceImpl implements PrintService {
List<String> fieldValues = systemFieldService.getSystemFieldValue(site, fieldNumber, systemFieldMap, baseData);
System.out.println("system field [" + fieldNumber + "] costs:" + (System.currentTimeMillis() - current1));
String fieldValue = fieldValues == null || fieldValues.size() == 0 ? "" : String.join(",", fieldValues);
SystemField systemFieldById = systemFieldService.getById(SystemField.genHandle(site, detail.getFieldNumber()));
// SystemField systemFieldById = systemFieldService.getById(SystemField.genHandle(site, detail.getFieldNumber()));
if (StrUtil.isBlank(fieldValue)) {
throw BusinessException.build("标签[" + templateName + "]字段[" + templateField + "]" + systemFieldById.getFieldDescription() + "获取为空");
fieldValue = "";
// throw BusinessException.build("标签[" + templateName + "]字段[" + templateField + "]" + systemFieldById.getFieldDescription() + "获取为空");
}
//固定值为EMPTY的特殊处理

View File

@ -275,18 +275,21 @@ public class SystemFieldServiceImpl extends ServiceImpl<SystemFieldMapper, Syste
List<String> systemFieldValue = getSystemFieldValue(TenantContextHolder.getTenantStrId(), param, camelCaseMap);
script=script.replaceFirst("(#\\{)([\\w]+)(\\})",systemFieldValue.get(0));
}
String function = "function parse(data) {\n" + script + "\n}";
String function = "function parse(data) {\ndata = JSON.parse(data);" + script + "\n}";
engine.eval(function);
Invocable invoke = (Invocable) engine;
SerializeConfig config = new SerializeConfig();
config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase; // 下划线转驼峰
Object result = invoke.invokeFunction("parse", camelCaseMap); // 将map数据对象作为js函数参数值,可通过json对象的方式获取
String json = JSONObject.toJSONString(camelCaseMap,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty);
Object result = invoke.invokeFunction("parse", json); // 将map数据对象作为js函数参数值,可通过json对象的方式获取
if (result.toString().indexOf("null")>=0){
throw BusinessException.build(fieldName);
}
return result;
} catch (Exception ex) {
throw BusinessException.build("JS解析失败编号:" + ex.getMessage());
throw BusinessException.build("系统定义字段[" + fieldName + "]JS解析失败编号:" + ex.getMessage());
}
}
}