24282 před 1 rokem
rodič
revize
e9d7c5a311

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/work/po/WorkOrder.java

@@ -95,4 +95,9 @@ public class WorkOrder extends BasePo {
      */
     private Integer syncProduction;
 
+    /**
+     * pdf排版文件路径
+     */
+    private String pdfUrl;
+
 }

+ 1 - 1
sd-business/src/main/java/com/sd/business/service/upload/impl/ObsUploadServiceImpl.java

@@ -75,7 +75,7 @@ public class ObsUploadServiceImpl implements ObsUploadService {
             IoUtil.close(bis);
         }
 
-        return objectKey;
+        return url + objectKey;
     }
 
     /**

+ 40 - 34
sd-business/src/main/java/com/sd/business/service/work/impl/WorkOrderServiceImpl.java

@@ -6,6 +6,7 @@ import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.collect.HashBasedTable;
@@ -44,6 +45,7 @@ import com.sd.business.service.order.OrderInfoService;
 import com.sd.business.service.order.OrderSkuBomService;
 import com.sd.business.service.order.OrderSkuService;
 import com.sd.business.service.sku.SkuSpecService;
+import com.sd.business.service.upload.ObsUploadService;
 import com.sd.business.service.work.WorkOrderDetailService;
 import com.sd.business.service.work.WorkOrderService;
 import com.sd.business.util.code.CodeEnum;
@@ -71,7 +73,8 @@ import java.awt.Image;
 import java.awt.geom.AffineTransform;
 import java.awt.image.AffineTransformOp;
 import java.awt.image.BufferedImage;
-import java.io.FileOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.net.URL;
@@ -129,6 +132,9 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
     @Autowired
     private OrderSkuBomService orderSkuBomService;
 
+    @Autowired
+    private ObsUploadService obsUploadService;
+
     private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
             8,
             16,
@@ -249,6 +255,7 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
             workOrder.setType(2);
             workOrder.setMasterBomSpecId(bomSpecId);
             workOrder.setPrintingPaperBomSpecId(printingPaperBomSpecId);
+            workOrder.setPdfUrl(StringPool.EMPTY);
 
             notFixationScheduling(workOrder, notFixationList, skuSpecMap, false);
         });
@@ -310,6 +317,7 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
                 }
             }
 
+            dto.setPdfUrl(StringPool.EMPTY);
             updateById(dto);
             workOrderDetailService.editLinked(newList, WorkOrderDetail::getWorkOrderId, id);
             if (!map.values().isEmpty()) {
@@ -414,20 +422,21 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
     /**
      * 缩放图片
      */
-    public void convert(BufferedImage imageNew, String target) {
+    public void convert(BufferedImage imageNew, WorkOrder workOrder) {
         // 宽度保持不变
         int subImageWidth = imageNew.getWidth();
         int subImageHeight = 14400;
 
-        FileOutputStream fos = null;
+        ByteArrayOutputStream os = null;
+        ByteArrayInputStream is = null;
 
         //设置文档页边距
         Document document = new Document();
         document.setMargins(0, 0, 0, 0);
 
         try {
-            fos = new FileOutputStream(target);
-            PdfWriter.getInstance(document, fos);
+            os = new ByteArrayOutputStream();
+            PdfWriter.getInstance(document, os);
 
             //打开文档
             document.open();
@@ -460,12 +469,18 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
             //关闭文档
             document.close();
 
+            is = new ByteArrayInputStream(os.toByteArray());
+            String fileUrl = obsUploadService.uploadFileByte(is, "【" + workOrder.getCode() + "】排版.pdf");
+            workOrder.setPdfUrl(fileUrl);
+            updateById(workOrder);
+
         } catch (Exception e) {
             log.error("生成pdf失败", e);
             throw new ServiceException("生成pdf失败");
         } finally {
-            IoUtil.flush(fos);
-            IoUtil.close(fos);
+            IoUtil.flush(os);
+            IoUtil.close(os);
+            IoUtil.close(is);
         }
     }
 
@@ -509,12 +524,16 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
     @Override
     public String pdfExport(Long id) {
         WorkOrder workOrder = getById(id);
+        if (ObjectUtil.isNotEmpty(workOrder.getPdfUrl())) {
+            return workOrder.getPdfUrl();
+        }
+
         List<WorkOrderDetail> list = workOrderDetailService.list(q -> q.eq(WorkOrderDetail::getWorkOrderId, id));
         Set<Long> orderSkuIdList = list.stream().map(WorkOrderDetail::getOrderSkuId).collect(Collectors.toSet());
         Map<Long, String> map = orderSkuService.mapKV(BaseIdPo::getId, OrderSku::getBlueprint, q -> q.in(BaseIdPo::getId, orderSkuIdList));
 
-        int canvasWidth = mmToPx(MaterialsConstant.MASTER_WIDTH);
-        int canvasHeight = mmToPx(workOrder.getMaxLength().doubleValue());
+        int canvasWidth = mmToPx(MaterialsConstant.MASTER_WIDTH) + 10;
+        int canvasHeight = mmToPx(workOrder.getMaxLength().doubleValue()) + 10;
 
         // 创建一个画布,并设置白色背景
         BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
@@ -527,26 +546,17 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
         for (WorkOrderDetail workOrderDetail : list) {
 
             executor.execute(() -> {
-
                 try {
-
-                    // 外边框填充黑色
-                    int marginWidth = mmToPx(workOrderDetail.getWidth().doubleValue() + MaterialsConstant.RESERVE);
-                    int marginHeight = mmToPx(workOrderDetail.getLength().doubleValue() + MaterialsConstant.RESERVE);
+                    // 获取xy轴以及图片长框
                     int x = mmToPx(workOrderDetail.getX().doubleValue());
                     int y = mmToPx(workOrderDetail.getY().doubleValue());
+                    int width = mmToPx(workOrderDetail.getWidth().doubleValue() + MaterialsConstant.RESERVE);
+                    int height = mmToPx(workOrderDetail.getLength().doubleValue() + MaterialsConstant.RESERVE);
 
-                    // 设置画笔颜色
-                    graphics.setColor(Color.BLACK);
-                    // 填充图片背景色为黑色
-                    graphics.fillRect(x, y, marginWidth, marginHeight);
-
-                    // 中间填充图片
+                    // 读取图片
                     String filePath = map.get(workOrderDetail.getOrderSkuId());
-
                     BufferedImage bi = bufferedImageMap.computeIfAbsent(filePath, item -> {
                         try {
-                            // 读取图片
                             URL url = new URL(filePath);
                             return ImageIO.read(url);
                         } catch (IOException e) {
@@ -554,29 +564,23 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
                         }
                     });
 
+                    // 旋转图片
                     if (Objects.equals(workOrderDetail.getRotate(), StatusConstant.YES)) {
                         bi = rotateImage(bi);
                     }
 
-                    int width = mmToPx(workOrderDetail.getWidth().doubleValue() + MaterialsConstant.SHRINK);
-                    int height = mmToPx(workOrderDetail.getLength().doubleValue() + MaterialsConstant.SHRINK);
+                    // 缩放图片
                     bi = scale(bi, width, height);
 
+                    // 把图片写入画布
                     int[] ImageArrays = bi.getRGB(0, 0, width, height, new int[width * height], 0, width);
-
-                    image.setRGB(
-                            x + (marginWidth - width) / 2,
-                            y + (marginHeight - height) / 2,
-                            width,
-                            height,
-                            ImageArrays, 0, width);
+                    image.setRGB(x, y, width, height, ImageArrays, 0, width);
 
                 } catch (Exception e) {
                     throw new RuntimeException(e);
                 } finally {
                     downLatch.countDown();
                 }
-
             });
 
         }
@@ -588,9 +592,9 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
             throw new ServiceException("生成pdf失败");
         }
 
-        convert(image, "E:\\test2.pdf");
+        convert(image, workOrder);
 
-        return null;
+        return workOrder.getPdfUrl();
     }
 
     /**
@@ -630,6 +634,7 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
         workOrder.setType(2);
         workOrder.setMasterBomSpecId(bomSpecId);
         workOrder.setPrintingPaperBomSpecId(printingPaperBomSpecId);
+        workOrder.setPdfUrl(StringPool.EMPTY);
 
         // 非固定排版
         notFixationScheduling(workOrder, notFixationList, skuSpecMap, true);
@@ -1107,6 +1112,7 @@ public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder
             workOrder.setUsedArea(fixationStrategy.getUsedArea());
             workOrder.setUseRatio(fixationStrategy.getUseRatio());
             workOrder.setMaxLength(fixationStrategy.getMaxLength());
+            workOrder.setPdfUrl(StringPool.EMPTY);
 
             TransactionUtil.execute(() -> {
                 if (!notSchedulingSku.isEmpty()) {

+ 6 - 0
sd-framework/pom.xml

@@ -73,6 +73,12 @@
             <version>4.2.2</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.twelvemonkeys.imageio</groupId>
+            <artifactId>imageio-jpeg</artifactId>
+            <version>3.10.1</version>
+        </dependency>
+
     </dependencies>
 
 </project>