BdAttachmentUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //
  5. package nckd.fi.er.webapi.utils;
  6. import java.security.SecureRandom;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.UUID;
  15. import java.util.stream.Collectors;
  16. import kd.bos.context.RequestContext;
  17. import kd.bos.dataentity.entity.DynamicObject;
  18. import kd.bos.id.ID;
  19. import kd.bos.orm.query.QFilter;
  20. import kd.bos.servicehelper.BusinessDataServiceHelper;
  21. import kd.bos.servicehelper.attachment.AttachmentFieldServiceHelper;
  22. import kd.bos.servicehelper.operation.SaveServiceHelper;
  23. import nckd.fi.er.common.BillStatusEnum;
  24. import nckd.fi.er.webapi.model.BaseDataModel;
  25. import nckd.fi.er.webapi.model.BdAttachmentModel;
  26. import org.apache.commons.collections.CollectionUtils;
  27. import org.apache.commons.lang3.ArrayUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. public class BdAttachmentUtils {
  30. public BdAttachmentUtils() {
  31. }
  32. public static DynamicObject[] getAttachmentDynamicObject(List<String> uidList) {
  33. QFilter filter = new QFilter("uid", "in", uidList);
  34. return BusinessDataServiceHelper.load("bd_attachment", "id,number,name,status,creator,modifier,enable,createtime,modifytime,masterid,uid,previewurl,type,tempfile,pageid,description,url,size,sort", filter.toArray());
  35. }
  36. public static List<BdAttachmentModel> getAttachments(List<String> uidList) {
  37. DynamicObject[] loads = getAttachmentDynamicObject(uidList);
  38. return ArrayUtils.isNotEmpty(loads) ? DoMoConvertUtils.toListModel(loads, new BdAttachmentModel()) : null;
  39. }
  40. public static Object[] saveAttachments(List<BdAttachmentModel> attachmentModelList) {
  41. if (CollectionUtils.isEmpty(attachmentModelList)) {
  42. return null;
  43. } else {
  44. DynamicObject[] oldAttachmentDynamicObject = getAttachmentDynamicObject((List)attachmentModelList.stream().map(BdAttachmentModel::getUid).collect(Collectors.toList()));
  45. Map<String, DynamicObject> oldAttachmentMap = new HashMap(0);
  46. if (ArrayUtils.isNotEmpty(oldAttachmentDynamicObject)) {
  47. oldAttachmentMap = (Map)Arrays.stream(oldAttachmentDynamicObject).collect(Collectors.toMap((d) -> {
  48. return d.getString("uid");
  49. }, (d) -> {
  50. return d;
  51. }));
  52. }
  53. String uidPre = "rc-upload-" + System.currentTimeMillis() + "-";
  54. int uidNum = (new SecureRandom()).nextInt(100);
  55. Date today = new Date();
  56. BaseDataModel userBaseDataModel = new BaseDataModel();
  57. userBaseDataModel.setId(String.valueOf(RequestContext.get().getCurrUserId()));
  58. List<DynamicObject> changeDynamicObjectList = new ArrayList();
  59. List<DynamicObject> addDynamicObjectList = new ArrayList();
  60. ArrayList<String> addUids = new ArrayList();
  61. Iterator var10 = attachmentModelList.iterator();
  62. while(var10.hasNext()) {
  63. BdAttachmentModel attachmentModel = (BdAttachmentModel)var10.next();
  64. String id = attachmentModel.getId();
  65. if (StringUtils.isEmpty(id)) {
  66. id = String.valueOf(ID.genLongId());
  67. attachmentModel.setId(id);
  68. }
  69. String number = attachmentModel.getNumber();
  70. if (StringUtils.isEmpty(number)) {
  71. number = UUID.randomUUID().toString();
  72. attachmentModel.setNumber(number);
  73. }
  74. String name = attachmentModel.getName();
  75. attachmentModel.setCreator(userBaseDataModel);
  76. attachmentModel.setModifier(userBaseDataModel);
  77. attachmentModel.setModifyTime(today);
  78. attachmentModel.setCreateTime(today);
  79. String uid = attachmentModel.getUid();
  80. if (StringUtils.isEmpty(uid)) {
  81. uid = uidPre + uidNum++;
  82. attachmentModel.setUid(uid);
  83. }
  84. attachmentModel.setType(name != null ? name.substring(name.lastIndexOf(46) + 1) : "");
  85. attachmentModel.setTempFile("1");
  86. String url = attachmentModel.getUrl();
  87. if (url.contains("tempfile/download.do?configKey") && StringUtils.isNotEmpty(name)) {
  88. String path = AttachmentFieldServiceHelper.saveTempToFileService(url, id, name);
  89. attachmentModel.setUrl(path);
  90. }
  91. DynamicObject attachmentDynamicObject = (DynamicObject)((Map)oldAttachmentMap).remove(uid);
  92. if (null == attachmentDynamicObject) {
  93. attachmentDynamicObject = BusinessDataServiceHelper.newDynamicObject("bd_attachment");
  94. DoMoConvertUtils.toDynamicObject(attachmentDynamicObject, attachmentModel);
  95. attachmentDynamicObject.set("status", BillStatusEnum.SUBMIT.getValue());
  96. addDynamicObjectList.add(attachmentDynamicObject);
  97. addUids.add(attachmentModel.getUid());
  98. } else {
  99. attachmentModel.setId(attachmentDynamicObject.getString("id"));
  100. BaseDataModel oldUserBaseDataModel = new BaseDataModel();
  101. oldUserBaseDataModel.setId(attachmentDynamicObject.getString("creator.id"));
  102. attachmentModel.setCreator(oldUserBaseDataModel);
  103. attachmentModel.setCreateTime(attachmentDynamicObject.getDate("createtime"));
  104. DoMoConvertUtils.toDynamicObject(attachmentDynamicObject, attachmentModel);
  105. attachmentDynamicObject.set("status", BillStatusEnum.SUBMIT.getValue());
  106. changeDynamicObjectList.add(attachmentDynamicObject);
  107. }
  108. }
  109. if (!CollectionUtils.isEmpty(addDynamicObjectList)) {
  110. SaveServiceHelper.save((DynamicObject[])addDynamicObjectList.toArray(new DynamicObject[0]));
  111. DynamicObject[] newAttachs = getAttachmentDynamicObject(addUids);
  112. changeDynamicObjectList.addAll(Arrays.asList(newAttachs));
  113. }
  114. if (changeDynamicObjectList.size() > 0) {
  115. return SaveServiceHelper.save((DynamicObject[])changeDynamicObjectList.toArray(new DynamicObject[0]));
  116. } else {
  117. return null;
  118. }
  119. }
  120. }
  121. }