LiuHB 附件工具类

This commit is contained in:
LHB-rabbit 2025-01-14 15:13:18 +08:00
parent 5b65962537
commit 3ebe4fc66d

View File

@ -0,0 +1,295 @@
package yem.base.common.utils;
/**
* @author lipan
* @date 2024/1/18 9:49
* @className TwoOpenCommFuncUtil
* @description
*/
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.OrmLocaleValue;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.field.LargeTextEdit;
import kd.bos.ksql.util.StringUtil;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author xwudd
* @Description 二开单据共用方法
*/
public class TwoOpenCommFuncUtil {
/**
* @param model
* @Description 二开单据商品分录第一行商品带出品名
*/
public static void setProductName(IDataModel model) {
DynamicObjectCollection materialentrys = model.getEntryEntity("yem_materialentry");
if (materialentrys.size() == 0) {
model.setValue("yem_productname", null);
}
for (DynamicObject materialentry : materialentrys) {
DynamicObject material = materialentry.getDynamicObject("yem_material");
if (material != null) {
model.setValue("yem_productname", material.getString("simplepinyin"));
break;
} else {
model.setValue("yem_productname", null);
}
}
}
/**
* @param form
* @param largeText
* @Description 增加/更新附件
*/
public static void addAttachmentContext(AbstractBillPlugIn form, String largeText, List<String> attachmentpanelKey) {
for (String s : attachmentpanelKey) {
attachmentForm(form, largeText, "yem_attachmentdetail", s);
}
}
/**
* @param form
* @param largeText
* @Description 删除附件
*/
public static void removeAttachmentContext(AbstractBillPlugIn form, String largeText, List<String> attachmentpanelKey) {
Set<String> urlSet = new HashSet<>();
for (String s : attachmentpanelKey) {
AttachmentPanel attachmentpanel = form.getView().getControl(s);
List<Map<String, Object>> attachmentDatas = attachmentpanel.getAttachmentData();
for (Map<String, Object> attachmentData : attachmentDatas) {
String uid = (String) attachmentData.get("uid");
urlSet.add(uid);
}
}
//附件分录
DynamicObjectCollection attachmentDetail = form.getView().getModel()
.getDataEntity(true).
getDynamicObjectCollection("yem_attachmentdetail");
// 找出需要删除的记录在urld中有但在urls中没有的记录
List<Integer> toDelete = new ArrayList<>();
for (int i = 0; i < attachmentDetail.size(); i++) {
DynamicObject attachment = attachmentDetail.get(i);
String url = attachment.getString("yem_uid");
if (!urlSet.contains(url)) {
toDelete.add(i);
}
}
if (!toDelete.isEmpty()) {
int[] array = toDelete.stream().mapToInt(Integer::intValue).toArray();
form.getView().getModel().deleteEntryRows("yem_attachmentdetail", array);
}
}
/**
* @param form
* @param largeText
* @param entityName
* @Description 附件表格赋值
*/
public static void attachmentForm(AbstractBillPlugIn form, String largeText, String entityName, String attachmentpanelKey) {
Map<String, Map<String, Object>> attachmentMap = buildAttachmentMap(form, attachmentpanelKey);
DynamicObjectCollection attachmentDetail = form.getView().getModel().getDataEntity(true).getDynamicObjectCollection(entityName);
String billno = (String) form.getView().getModel().getValue("billno");
// 获取现有的 UID 列表
Set<String> existingUids = attachmentDetail.stream()
.map(detail -> detail.getString("yem_uid"))
.collect(Collectors.toSet());
// 处理每个附件
attachmentMap.forEach((uid, value) -> {
if (existingUids.contains(uid)) {
updateExistingAttachment(form, attachmentDetail, uid, value, billno, attachmentpanelKey);
} else {
createNewAttachment(form, entityName, uid, value, billno, attachmentpanelKey);
}
});
}
/**
* 构建附件map
*
* @param form
* @param attachmentpanelKey
* @return
*/
private static Map<String, Map<String, Object>> buildAttachmentMap(AbstractBillPlugIn form, String attachmentpanelKey) {
Map<String, Map<String, Object>> attachmentMap = new HashMap<>();
AttachmentPanel attachmentPanel = form.getView().getControl(attachmentpanelKey);
List<Map<String, Object>> attachmentDatas = attachmentPanel.getAttachmentData();
for (Map<String, Object> attachmentData : attachmentDatas) {
String uid = (String) attachmentData.get("uid");
String name = (String) attachmentData.get("name");
String url = (String) attachmentData.get("url");
Long createTime = (Long) attachmentData.get("createdate");
// 格式化时间
String formattedTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(createTime));
// 获取创建人
@SuppressWarnings("unchecked")
Map<String, Object> creator = (Map<String, Object>) attachmentData.get("creator");
String creatorName = creator != null ? (String) creator.get("zh_CN") : null;
// 存储数据
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("name", name);
dataMap.put("url", url);
dataMap.put("createTime", formattedTime);
dataMap.put("creator", creatorName);
attachmentMap.put(uid, dataMap);
}
return attachmentMap;
}
/**
* 更新附件行
*
* @param form
* @param attachmentDetail
* @param uid
* @param data
* @param billno
* @param attachmentpanelKey
*/
private static void updateExistingAttachment(AbstractBillPlugIn form, DynamicObjectCollection attachmentDetail,
String uid, Map<String, Object> data, String billno,
String attachmentpanelKey) {
for (DynamicObject attachment : attachmentDetail) {
if (uid.equals(attachment.getString("yem_uid"))) {
int rowIndex = attachmentDetail.indexOf(attachment);
setAttachmentValues(form, rowIndex, uid, data, billno, attachmentpanelKey);
break;
}
}
}
/**
* 增加新附件行
*
* @param form
* @param entityName
* @param uid
* @param data
* @param billno
* @param attachmentpanelKey
*/
private static void createNewAttachment(AbstractBillPlugIn form, String entityName, String uid,
Map<String, Object> data, String billno,
String attachmentpanelKey) {
int newRow = form.getView().getModel().createNewEntryRow(entityName);
setAttachmentValues(form, newRow, uid, data, billno, attachmentpanelKey);
}
/**
* 附件分录赋值
* @param form
* @param row
* @param uid
* @param data
* @param billno
* @param attachmentpanelKey
*/
private static void setAttachmentValues(AbstractBillPlugIn form, int row, String uid,
Map<String, Object> data, String billno,
String attachmentpanelKey) {
form.getView().getModel().setValue("yem_attachmentname", data.get("name"), row);
form.getView().getModel().setValue("yem_url", data.get("url"), row);
form.getView().getModel().setValue("yem_panel", attachmentpanelKey, row);
form.getView().getModel().setValue("yem_billno", billno, row);
form.getView().getModel().setValue("yem_uid", uid, row);
form.getView().getModel().setValue("yem_uploaddate", data.get("createTime"), row);
form.getView().getModel().setValue("yem_creator", data.get("creator"), row);
}
/**
* @param form
* @param largeText
* @param entityName
* @Description 附件表格RPA比对结果赋值
*/
public static void attachmentFormRPA(AbstractBillPlugIn form, String largeText, String entityName, String tarFilesName, int type) {
//获取存储内容
LargeTextEdit largeTextEdit = form.getView().getControl(largeText);
//add by zhangh 20230508 其他单据为添加rpa对接字段
if (largeTextEdit != null) {
String tagFieldKey = largeTextEdit.getTagFieldKey();
String context = (String) form.getView().getModel().getValue(tagFieldKey);
JSONObject json = null;
if (!StringUtil.isEmpty(context)) {
json = JSONObject.parseObject(context);
}
// 获取附件表格的值
DynamicObjectCollection entry = form.getView().getModel().getEntryEntity("yem_attachmentdetail");
if (json == null || entry == null || entry.size() == 0) return;
Set<String> keySets = json.keySet();
// 比对结果赋值
for (int i = 0; i < entry.size(); i++) {
String name = entry.get(i).getString("yem_attachmentname");
if (keySets.contains(name)) {
// if (type == 1)form.getView().getModel().setValue(tarFilesName, (String)json.get(name),i);
// if (type == 2)form.getView().getModel().setValue(tarFilesName, (Boolean)json.get(name),i);
// 3.1 金亮修改(之前保错)
Object objName = json.get(name);
if (objName instanceof Long) {
if (type == 1) form.getView().getModel().setValue(tarFilesName, (Long) json.get(name), i);
} else if (objName instanceof String) {
if (type == 1) form.getView().getModel().setValue(tarFilesName, (String) json.get(name), i);
} else if (objName instanceof Boolean) {
if (type == 2) form.getView().getModel().setValue(tarFilesName, (Boolean) json.get(name), i);
}
}
}
}
}
/**
* @param form
* @param largeText
* @param row
* @Description设置附件类型
*/
public static void setAttachmentType(AbstractBillPlugIn form, String largeText, int row) {
LargeTextEdit largeTextEdit = form.getView().getControl(largeText);
String tagFieldKey = largeTextEdit.getTagFieldKey();
String context = (String) form.getView().getModel().getValue(tagFieldKey);
JSONObject json = null;
if (!StringUtil.isEmpty(context)) {
json = JSONObject.parseObject(context);
}
if (json == null) {
json = new JSONObject();
}
String key = "";
OrmLocaleValue keyLocal = (OrmLocaleValue) form.getView().getModel().getValue("yem_attachmentname", row);
if (keyLocal != null) {
key = keyLocal.getLocaleValue();
}
DynamicObject value = (DynamicObject) form.getView().getModel().getValue("yem_attachmenttype", row);
if (value != null) {
json.put(key, value.getPkValue());
} else {
json.put(key, "");
}
form.getView().getModel().setValue(tagFieldKey, json.toJSONString());
}
}