|
|
@@ -0,0 +1,254 @@
|
|
|
+package nckd.fi.er.common.utils;
|
|
|
+
|
|
|
+import kd.bos.dataentity.entity.DynamicObject;
|
|
|
+import kd.bos.dataentity.serialization.SerializationUtils;
|
|
|
+import kd.bos.dataentity.utils.StringUtils;
|
|
|
+import kd.bos.form.CloseCallBack;
|
|
|
+import kd.bos.form.FormShowParameter;
|
|
|
+import kd.bos.form.ShowType;
|
|
|
+import kd.bos.form.events.ClosedCallBackEvent;
|
|
|
+import kd.bos.form.plugin.AbstractFormPlugin;
|
|
|
+import kd.bos.entity.datamodel.IDataModel;
|
|
|
+import nckd.base.common.constant.BaseFieldConst;
|
|
|
+import nckd.fi.er.common.constant.InttempConfigConstant;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通用表达式弹窗工具类
|
|
|
+ * @author: Custom
|
|
|
+ */
|
|
|
+public class ExpressionPopupUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 常量定义 - 严格参考凭证定义中的常量
|
|
|
+ */
|
|
|
+ public static class Constants {
|
|
|
+ // 表达式表单ID
|
|
|
+ public static final String FORMULA_FORM_ID = "ai_formula";
|
|
|
+
|
|
|
+ // 参数名称
|
|
|
+ public static final String IS_EVENT = "isevent";
|
|
|
+ public static final String BILL_ENTITY_NUMBER = "billEntityNumber";
|
|
|
+ public static final String ENTITY_NUMBER = "entitynumber";
|
|
|
+ public static final String SOURCE_BILL = "sourcebill";
|
|
|
+ public static final String BUILD = "build";
|
|
|
+ public static final String SRC_MAIN_ENTITY_TYPE = "srcMainEntityType";
|
|
|
+ public static final String AI_BUTTON_KEY = "ai_button_key";
|
|
|
+ public static final String FORMULA = "formula";
|
|
|
+ public static final String AI_EXPRESSION = "ai_expression";
|
|
|
+ public static final String AI_VCH_TEMPLATE_CACHE_KEY = "ai_vchtemplatecachekey";
|
|
|
+ public static final String EVENT = "event";
|
|
|
+ public static final String BILL = "bill";
|
|
|
+
|
|
|
+ // 引用字段编辑标识
|
|
|
+ public static final String REFERENCE_FIELD_EDIT = "referencefieldedit";
|
|
|
+ }
|
|
|
+
|
|
|
+ private AbstractFormPlugin plugin;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造方法
|
|
|
+ * @param plugin 表单插件实例,用于调用框架API
|
|
|
+ */
|
|
|
+ public ExpressionPopupUtil(AbstractFormPlugin plugin) {
|
|
|
+ this.plugin = plugin;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打开表达式弹窗 - 严格参考凭证定义中的showFormulaForm方法
|
|
|
+ * @param model 数据模型,用于获取和设置字段值
|
|
|
+ * @param fieldKey 字段标识
|
|
|
+ * @param sourceBill 来源单据对象
|
|
|
+ * @param entryEntityName 单据体名称(如果字段在单据体中,否则为null)
|
|
|
+ * @param currentRow 当前行索引(如果字段在单据体中,否则为-1)
|
|
|
+ */
|
|
|
+ public void showExpressionPopup(IDataModel model, String fieldKey, DynamicObject sourceBill,
|
|
|
+ String entryEntityName, int currentRow) {
|
|
|
+ // 校验来源单据是否为空
|
|
|
+ if (sourceBill == null) {
|
|
|
+ plugin.getView().showErrorNotification("请先选择来源单据!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取来源单据编码
|
|
|
+ String bizObjectNumber = sourceBill.getString(BaseFieldConst.NUMBER);
|
|
|
+
|
|
|
+ // 获取当前字段的表达式值
|
|
|
+ String currentExpression = getCurrentExpression(model, fieldKey, entryEntityName, currentRow);
|
|
|
+
|
|
|
+ // 创建表单显示参数
|
|
|
+ FormShowParameter showParam = new FormShowParameter();
|
|
|
+
|
|
|
+ // 设置弹窗基础参数
|
|
|
+ showParam.setFormId(Constants.FORMULA_FORM_ID);
|
|
|
+ showParam.getOpenStyle().setShowType(ShowType.Modal);
|
|
|
+ showParam.setCloseCallBack(new CloseCallBack(plugin, fieldKey));
|
|
|
+
|
|
|
+ // 严格按照参考代码设置自定义参数
|
|
|
+ showParam.setCustomParam(Constants.IS_EVENT, false);
|
|
|
+ showParam.setCustomParam(Constants.BILL_ENTITY_NUMBER, bizObjectNumber);
|
|
|
+ showParam.setCustomParam(Constants.ENTITY_NUMBER, bizObjectNumber);
|
|
|
+ showParam.setCustomParam(Constants.SOURCE_BILL, bizObjectNumber);
|
|
|
+ showParam.setCustomParam(Constants.BUILD, bizObjectNumber);
|
|
|
+ showParam.setCustomParam(Constants.SRC_MAIN_ENTITY_TYPE, Constants.REFERENCE_FIELD_EDIT);
|
|
|
+
|
|
|
+ // 设置按钮标识
|
|
|
+ String buttonKey = Constants.AI_EXPRESSION + "-" + fieldKey;
|
|
|
+ showParam.setCustomParam(Constants.AI_BUTTON_KEY, buttonKey);
|
|
|
+
|
|
|
+ // 按照参考代码构建表达式对象并序列化
|
|
|
+ VchTplExpression exprObj = new VchTplExpression();
|
|
|
+ exprObj.setExprType(ExpressionType.Formula);
|
|
|
+ exprObj.setExpression(currentExpression);
|
|
|
+ String formulaJson = SerializationUtils.toJsonString(exprObj);
|
|
|
+ showParam.getCustomParams().put(Constants.FORMULA, formulaJson);
|
|
|
+
|
|
|
+ // 获取缓存信息(严格参考代码)
|
|
|
+ String cacheKey = Constants.AI_VCH_TEMPLATE_CACHE_KEY + "-" + currentRow;
|
|
|
+ String isEvent = plugin.getPageCache().get(Constants.EVENT);
|
|
|
+
|
|
|
+ // 设置缓存参数
|
|
|
+ showParam.setCustomParam(Constants.AI_VCH_TEMPLATE_CACHE_KEY, plugin.getPageCache().get(cacheKey));
|
|
|
+
|
|
|
+ // 打开弹窗
|
|
|
+ plugin.getView().showForm(showParam);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理表达式弹窗回调 - 严格参考凭证定义中的closedCallBack方法
|
|
|
+ * @param model 数据模型,用于设置字段值
|
|
|
+ * @param event 关闭回调事件
|
|
|
+ * @param entryEntityName 单据体名称(如果字段在单据体中,否则为null)
|
|
|
+ * @param currentRow 当前行索引(如果字段在单据体中,否则为-1)
|
|
|
+ */
|
|
|
+ public void handleExpressionCallback(IDataModel model, ClosedCallBackEvent event,
|
|
|
+ String entryEntityName, int currentRow) {
|
|
|
+ String actionId = event.getActionId();
|
|
|
+ Object returnData = event.getReturnData();
|
|
|
+
|
|
|
+ // 严格按照参考代码处理返回数据
|
|
|
+ if (returnData != null) {
|
|
|
+ String returnStr = returnData.toString();
|
|
|
+ if (StringUtils.isBlank(returnStr)) {
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPR, "", entryEntityName, currentRow);
|
|
|
+ // 同时设置译文字段
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPRNAME, "", entryEntityName, currentRow);
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ // 使用系统提供的SerializationUtils解析外层数据
|
|
|
+ VchReturnData retData = kd.bos.dataentity.serialization.SerializationUtils.fromJsonString(returnStr, VchReturnData.class);
|
|
|
+ String dataStr = retData.getDataStr();
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(dataStr)) {
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPR, "", entryEntityName, currentRow);
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPRNAME, "", entryEntityName, currentRow);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析内层表达式数据 - 现在会包含exprTran等字段
|
|
|
+ VchTplExpression filterObj = kd.bos.dataentity.serialization.SerializationUtils.fromJsonString(dataStr, VchTplExpression.class);
|
|
|
+
|
|
|
+ // 设置表达式字段值
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPR, filterObj.getExpression(), entryEntityName, currentRow);
|
|
|
+ // 设置表达式译文字段值
|
|
|
+ setFieldValue(model, InttempConfigConstant.NCKD_BUSINESSEXPRNAME, filterObj.getExprTran(), entryEntityName, currentRow);
|
|
|
+ } catch (Exception e) {
|
|
|
+ plugin.getView().showErrorNotification("解析表达式返回数据失败:" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前字段的表达式值
|
|
|
+ * @param model 数据模型
|
|
|
+ * @param fieldKey 字段标识
|
|
|
+ * @param entryEntityName 单据体名称
|
|
|
+ * @param currentRow 当前行索引
|
|
|
+ * @return 当前表达式值
|
|
|
+ */
|
|
|
+ private String getCurrentExpression(IDataModel model, String fieldKey,
|
|
|
+ String entryEntityName, int currentRow) {
|
|
|
+ if (StringUtils.isNotBlank(entryEntityName) && currentRow >= 0) {
|
|
|
+ // 单据体字段
|
|
|
+ Object value = model.getValue(fieldKey, currentRow);
|
|
|
+ return value != null ? value.toString() : "";
|
|
|
+ } else {
|
|
|
+ // 表头字段
|
|
|
+ Object value = model.getValue(fieldKey);
|
|
|
+ return value != null ? value.toString() : "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置字段值
|
|
|
+ * @param model 数据模型
|
|
|
+ * @param fieldKey 字段标识
|
|
|
+ * @param value 字段值
|
|
|
+ * @param entryEntityName 单据体名称
|
|
|
+ * @param currentRow 当前行索引
|
|
|
+ */
|
|
|
+ private void setFieldValue(IDataModel model, String fieldKey, Object value,
|
|
|
+ String entryEntityName, int currentRow) {
|
|
|
+ if (StringUtils.isNotBlank(entryEntityName) && currentRow >= 0) {
|
|
|
+ // 单据体字段
|
|
|
+ model.setValue(fieldKey, value, currentRow);
|
|
|
+ } else {
|
|
|
+ // 表头字段
|
|
|
+ model.setValue(fieldKey, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 内部类定义 - 严格参考凭证定义中的相关类 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表达式类型枚举 - 严格参考凭证定义
|
|
|
+ */
|
|
|
+ public enum ExpressionType {
|
|
|
+ Formula, Condition
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * VchTplExpression类 - 严格参考凭证定义
|
|
|
+ */
|
|
|
+ public static class VchTplExpression {
|
|
|
+ private ExpressionType exprType;
|
|
|
+ private String expression;
|
|
|
+ private String exprTran; // 添加表达式译文字段
|
|
|
+ private Object description; // 描述字段
|
|
|
+ private Object localeExprTran; // 本地化表达式译文
|
|
|
+ private String exprDesc; // 表达式描述
|
|
|
+
|
|
|
+ // getter and setter methods
|
|
|
+ public ExpressionType getExprType() { return exprType; }
|
|
|
+ public void setExprType(ExpressionType exprType) { this.exprType = exprType; }
|
|
|
+ public String getExpression() { return expression; }
|
|
|
+ public void setExpression(String expression) { this.expression = expression; }
|
|
|
+ public String getExprTran() { return exprTran; } // 添加getter
|
|
|
+ public void setExprTran(String exprTran) { this.exprTran = exprTran; } // 添加setter
|
|
|
+ public Object getDescription() { return description; }
|
|
|
+ public void setDescription(Object description) { this.description = description; }
|
|
|
+ public Object getLocaleExprTran() { return localeExprTran; }
|
|
|
+ public void setLocaleExprTran(Object localeExprTran) { this.localeExprTran = localeExprTran; }
|
|
|
+ public String getExprDesc() { return exprDesc; }
|
|
|
+ public void setExprDesc(String exprDesc) { this.exprDesc = exprDesc; }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * VchReturnData类 - 严格参考凭证定义
|
|
|
+ */
|
|
|
+ public static class VchReturnData {
|
|
|
+ private String dataStr;
|
|
|
+ private String formId;
|
|
|
+ private String entryName;
|
|
|
+
|
|
|
+ // getter and setter methods
|
|
|
+ public String getDataStr() { return dataStr; }
|
|
|
+ public void setDataStr(String dataStr) { this.dataStr = dataStr; }
|
|
|
+ public String getFormId() { return formId; }
|
|
|
+ public void setFormId(String formId) { this.formId = formId; }
|
|
|
+ public String getEntryName() { return entryName; }
|
|
|
+ public void setEntryName(String entryName) { this.entryName = entryName; }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|