| New file |
| | |
| | | package com.example.erp.service.sd; |
| | | |
| | | import com.aspose.cad.Image; |
| | | import com.aspose.cad.ImageOptionsBase; |
| | | import com.aspose.cad.License; |
| | | import com.aspose.cad.imageoptions.CadRasterizationOptions; |
| | | import com.aspose.cad.imageoptions.PngOptions; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.example.erp.common.Result; |
| | | import com.example.erp.entity.sd.OrderDetail; |
| | | import com.example.erp.entity.sd.OrderFile; |
| | | import com.example.erp.mapper.sd.OrderFileMapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class OrderFileService { |
| | | private final OrderFileMapper orderFileMapper; |
| | | public List<OrderFile> getOrderFilePicture(List<Map<String,Object>> orderDetails) throws NoSuchFieldException { |
| | | Set<String> seenKeys = new HashSet<>(); |
| | | List<Map<String,Object>> result = new ArrayList<>(); |
| | | // 遍历订单详情列表去重 |
| | | result = orderDetails.stream() |
| | | .collect(Collectors.collectingAndThen( |
| | | Collectors.toMap( |
| | | map -> map.get("order_id"), |
| | | map -> map, |
| | | (existing, replacement) -> existing // 保留第一个出现的 |
| | | ), |
| | | map -> new ArrayList<>(map.values()) |
| | | )); |
| | | //循环获取图片 |
| | | List<OrderFile> orderFiles = new ArrayList<>(); |
| | | for (Map<String,Object> obj : result) { |
| | | List<OrderFile> orderFile = orderFileMapper.selectList(new QueryWrapper<OrderFile>() |
| | | .select("order_id, order_number, image_base64") |
| | | .eq("order_id", obj.get("order_id")) |
| | | ); |
| | | if (orderFile != null){ |
| | | orderFiles.addAll(orderFile); |
| | | } |
| | | } |
| | | return orderFiles; |
| | | } |
| | | |
| | | public Object updateOrderFileByOrderNumber(MultipartFile file,String orderId,String orderNumber) throws IOException { |
| | | |
| | | try (InputStream is = License.class.getResourceAsStream("/lisence.xml")) { |
| | | License license = new License(); |
| | | license.setLicense(is); |
| | | |
| | | |
| | | // 调用Image类的Load方法来加载输入的DWG文件。 |
| | | Image image = Image.load(file.getInputStream()); |
| | | // 创建CadRasterizationOptions实例以启用CAD栅格化选项。 |
| | | CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions(); |
| | | // 设置宽度 |
| | | rasterizationOptions.setPageWidth(1000); |
| | | // 设置高度 |
| | | rasterizationOptions.setPageHeight(700); |
| | | // 调用这个setEmbedBackground方法来设置背景色是否不等于输出格式的默认背景色 |
| | | //rasterizationOptions.setEmbedBackground(true); |
| | | // 为生成的图像创建一个PngOptions的实例,并将其分配给ImageOptionsBase类的实例。 |
| | | ImageOptionsBase options = new PngOptions(); |
| | | // 调用 setVectorRasterizationOptions 方法来定义光栅化选项 |
| | | options.setVectorRasterizationOptions(rasterizationOptions); |
| | | |
| | | // 保存到字节流 |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | image.save(outputStream, options); |
| | | byte[] imageBytes = outputStream.toByteArray(); |
| | | String base64 = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes); |
| | | OrderFile orderFile = new OrderFile(); |
| | | orderFile.setImageBase64(base64); |
| | | orderFile.setFileName(file.getName()); |
| | | orderFile.setOrderId(orderId); |
| | | orderFile.setOrderNumber(orderNumber); |
| | | orderFile.setFileData(Arrays.toString(file.getBytes())); |
| | | |
| | | OrderFile orderFileExist = orderFileMapper |
| | | .selectOne(new LambdaQueryWrapper<OrderFile>() |
| | | .eq(OrderFile::getOrderId, orderId) |
| | | .eq(OrderFile::getOrderNumber, orderNumber) |
| | | ); |
| | | if (orderFileExist == null) { |
| | | orderFileMapper.insert(orderFile); |
| | | }else { |
| | | orderFileMapper.update(orderFile,new LambdaUpdateWrapper<OrderFile>() |
| | | .eq(OrderFile::getOrderId, orderId) |
| | | .eq(OrderFile::getOrderNumber, orderNumber) |
| | | ); |
| | | } |
| | | |
| | | |
| | | return orderFile; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public Object getOrderNumberFile(String orderId, String orderNumber) { |
| | | return orderFileMapper.selectOne(new QueryWrapper<OrderFile>() |
| | | .eq("order_id", orderId) |
| | | .eq("order_number", orderNumber) |
| | | ); |
| | | } |
| | | |
| | | public Boolean deleteOrderNumberFile(String orderId, String orderNumber) { |
| | | return orderFileMapper.delete(new QueryWrapper<OrderFile>() |
| | | .eq("order_id", orderId) |
| | | .eq("order_number", orderNumber) |
| | | ) > 0; |
| | | } |
| | | } |