dobe_comic8/main/java/shkd/repc/rebm/formplugin/CustomFieldReplacePlugin.java

148 lines
6.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package shkd.repc.rebm.formplugin;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.ValueMapItem;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.property.ComboProp;
import kd.bos.form.control.RichTextEditor;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.scm.bid.common.constant.entity.BidAnnouncementConstant;
import kd.scm.bid.common.constant.entity.BidTemplateMangeEntity;
import kd.scm.bid.common.constant.entity.BidTemplateTypeEntity;
import kd.sdk.plugin.Plugin;
import shkd.repc.rebm.formplugin.fieldinsert.entity.ModelType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 函件模板生成
* @author 李贵强
*/
public class CustomFieldReplacePlugin extends AbstractFormPlugin implements Plugin {
@Override
public void propertyChanged(PropertyChangedArgs e) {
super.propertyChanged(e);
String fieldKey = e.getProperty().getName();
if (StringUtils.equals(fieldKey, BidAnnouncementConstant.BIDANNTEMPLATE)) {
//获取上游单据所需字段
//父页面id
Map<String, Object> customParams = this.getView().getFormShowParameter().getCustomParams();
Long pkValue = (Long)customParams.get("pkid");
//公告模板
DynamicObject announcement = (DynamicObject) this.getModel().getValue(BidAnnouncementConstant.BIDANNTEMPLATE);
//函件模板
DynamicObject correspondence = announcement.getDynamicObject(BidTemplateMangeEntity.TYPE);
//模板类型名称
String name = correspondence.getString(BidTemplateTypeEntity.NAME);
ModelType type = ModelType.fromString(name);
//获取扩展字段上添加字段
DynamicObject extendedDynamic = BusinessDataServiceHelper.loadSingle("qeug_insertfield", (new QFilter("number", QCP.equals, type.toString())).toArray());
if (null != extendedDynamic) {
DynamicObjectCollection insertEntry = extendedDynamic.getDynamicObjectCollection("qeug_entryentity");
if (null != insertEntry && insertEntry.size() != 0) {
Map<String, String> replacements = new HashMap<>();
for (DynamicObject entry : insertEntry) {
//父页面单据标识
String sourceId = entry.getString("qeug_sourceid");
//父页面字段类型
String fieldType = entry.getString("qeug_fieldtype");
//父页面字段标识
String fieldId = entry.getString("qeug_fieldid");
//扩展字段名称
String fieldName = entry.getString("qeug_fieldname");
String fatherParam = this.getFatherParam(pkValue, sourceId, fieldId,fieldType);
replacements.put(fieldName,fatherParam);
}
RichTextEditor editor = (RichTextEditor) this.getView().getControl(BidAnnouncementConstant.RICHTEXTEDITORAP);
String text = editor.getText();
if (text!=null){
String newText = replacePlaceholders(text, replacements);
editor.setText(newText);
}
}
}
}
}
/**
* 获取父页面字段值
* @param pkValue 单据id
* @param sourceId 单据标识
* @param fieldId 字段标识
* @return 字段值
*/
public String getFatherParam(Long pkValue,String sourceId,String fieldId,String fieldType){
String result="";
DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(sourceId, (new QFilter("id", QCP.equals, pkValue)).toArray());
if (dynamicObject!=null){
String param = dynamicObject.getString(fieldId);
if (fieldType.equals("下拉列表") || fieldType.equals("单据状态")){
result = getParamFromMap(param, sourceId, fieldId);
}else{
return param;
}
}
return result;
}
/**
* 获取下拉项的名称
* @param param 下拉项value
* @param sourceId 单据标识
* @param fieldId 字段标识
* @return name
*/
public String getParamFromMap(String param,String sourceId,String fieldId){
String name="";
List<ValueMapItem> comboItems = ((ComboProp) EntityMetadataCache.getDataEntityType(sourceId).getProperty(fieldId)).getComboItems();
if (comboItems!=null&&comboItems.size()!=0){
for (ValueMapItem comboItem : comboItems) {
String value = comboItem.getValue();
if (value.equals(param)){
name = String.valueOf(comboItem.getName());
break;
}
}
}
return name;
}
/**
* 自定义的replace方法通过传入一个Map来替换对应的占位符
* @param input 文本
* @param replacements 替换值
* @return 替换后的文本
*/
public static String replacePlaceholders(String input, Map<String, String> replacements) {
// 正则表达式匹配{xxx}的模式
Pattern pattern = Pattern.compile("\\{([^}]+)\\}");
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();
// 查找所有匹配的占位符并替换
while (matcher.find()) {
String placeholder = matcher.group(1); // 获取占位符名称
String replacement = replacements.get(placeholder); // 获取替换值
if (replacement == null) {
replacement = ""; // 如果Map中没有替换值默认替换为空
}
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
return result.toString();
}
}