From d7c69d76e9c81464c698199b90ec4a339a18b257 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 27 三月 2026 16:06:17 +0800
Subject: [PATCH] feat: 销售订单标签打印

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |  331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 331 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 320c362..04f3a3a 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -13,6 +13,8 @@
 import com.ruoyi.basic.mapper.ProductMapper;
 import com.ruoyi.basic.mapper.ProductModelMapper;
 import com.ruoyi.basic.pojo.Customer;
+import com.ruoyi.basic.pojo.CustomerRegions;
+import com.ruoyi.basic.service.ICustomerRegionsService;
 import com.ruoyi.common.enums.FileNameType;
 import com.ruoyi.common.enums.SaleEnum;
 import com.ruoyi.common.exception.ServiceException;
@@ -141,6 +143,8 @@
     ;
     @Autowired
     private SysUserMapper sysUserMapper;
+
+    private final ICustomerRegionsService customerRegionsService;
 
     @Override
     public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
@@ -1031,6 +1035,333 @@
         return dto;
     }
 
+    @Override
+    public SalesOrdersDto salesOrders(Long salesLedgerId) {
+        if (salesLedgerId == null) {
+            throw new ServiceException("鎵撳嵃閿�鍞鍗曞け璐�,閿�鍞鍗旾D涓嶈兘涓虹┖");
+        }
+        SalesLedger salesLedger = baseMapper.selectById(salesLedgerId);
+        if (salesLedger == null) {
+            throw new ServiceException("鎵撳嵃閿�鍞鍗曞け璐�,閿�鍞鍗曚笉瀛樺湪");
+        }
+
+        SalesOrdersDto dto = new SalesOrdersDto();
+        dto.setSalesContractNo(salesLedger.getSalesContractNo());
+        dto.setCustomerName(salesLedger.getCustomerName());
+        dto.setProjectName(salesLedger.getProjectName());
+        dto.setSalesman(salesLedger.getSalesman());
+        dto.setExecutionDate(salesLedger.getExecutionDate() != null ? salesLedger.getExecutionDate().atStartOfDay() : null);
+        dto.setDeliveryDate(salesLedger.getDeliveryDate());
+        dto.setRemakes(salesLedger.getRemarks());
+        dto.setCompanyName("楣ゅ澶╂矏閽㈠寲鐜荤拑鍘�");
+
+        // 閫佽揣鍦板潃
+        if (salesLedger.getCustomerId() != null) {
+            Customer customer = customerMapper.selectById(salesLedger.getCustomerId());
+            if (customer != null) {
+                StringBuilder address = new StringBuilder();
+                if (customer.getRegionsId() != null) {
+                    CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId());
+                    if (regions != null) {
+                        address.append(regions.getRegionsName());
+                    }
+                }
+                if (StringUtils.isNotEmpty(customer.getCompanyAddress())) {
+                    address.append(customer.getCompanyAddress());
+                }
+                dto.setCompanyAddress(address.toString());
+            }
+        }
+
+        // 鍒跺崟鍛�
+        if (StringUtils.isNotEmpty(salesLedger.getEntryPerson())) {
+            try {
+                SysUser user = sysUserMapper.selectUserById(Long.parseLong(salesLedger.getEntryPerson()));
+                if (user != null) {
+                    dto.setOrderMaker(user.getNickName());
+                }
+            } catch (Exception e) {
+                log.error("鑾峰彇鍒跺崟鍛樹俊鎭け璐�: {}", e.getMessage());
+            }
+        }
+        // 鍒跺崟鏃ユ湡 (搴曢儴)
+        dto.setOrderMakerDate(salesLedger.getExecutionDate() != null ? salesLedger.getExecutionDate().atStartOfDay() : null);
+
+        // 鎵撳嵃淇℃伅
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        if (loginUser != null && loginUser.getUser() != null) {
+            dto.setPrintPeople(loginUser.getUser().getNickName());
+        }
+        dto.setPrintTime(LocalDateTime.now());
+
+        // 鏌ヨ浜у搧鍒楄〃
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
+                new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId));
+
+        if (CollectionUtils.isNotEmpty(products)) {
+            SalesLedgerProduct firstProduct = products.get(0);
+            dto.setProductName(firstProduct.getProductCategory() != null ? firstProduct.getProductCategory() : "");
+        }
+
+        List<SalesOrdersDto.SalesOrderItemDto> itemDtos = new ArrayList<>();
+        BigDecimal subtotalQuantity = BigDecimal.ZERO;
+        BigDecimal subtotalArea = BigDecimal.ZERO;
+        BigDecimal subtotalAmount = BigDecimal.ZERO;
+
+        for (SalesLedgerProduct p : products) {
+            SalesOrdersDto.SalesOrderItemDto itemDto = new SalesOrdersDto.SalesOrderItemDto();
+            itemDto.setFloorCode(p.getFloorCode());
+            String desc = (p.getProductCategory() != null ? p.getProductCategory() : "") +
+                    (StringUtils.isNotBlank(p.getSpecificationModel()) ? " " + p.getSpecificationModel() : "");
+            itemDto.setProductDescription(desc.trim());
+            itemDto.setWidth(p.getWidth());
+            itemDto.setHeight(p.getHeight());
+            itemDto.setQuantity(p.getQuantity());
+
+            // 闈㈢Н璁$畻
+            BigDecimal area = p.getSettleTotalArea() != null ? p.getSettleTotalArea() : p.getActualTotalArea();
+            if (area == null && p.getWidth() != null && p.getHeight() != null && p.getQuantity() != null) {
+                area = p.getWidth().multiply(p.getHeight()).multiply(p.getQuantity()).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP);
+            }
+            itemDto.setArea(area);
+            itemDto.setUnitPrice(p.getTaxInclusiveUnitPrice());
+            itemDto.setAmount(p.getTaxInclusiveTotalPrice());
+            itemDto.setRemark(p.getRemark());
+            itemDto.setProcessRequirement(p.getProcessRequirement());
+
+            subtotalQuantity = subtotalQuantity.add(p.getQuantity() != null ? p.getQuantity() : BigDecimal.ZERO);
+            subtotalArea = subtotalArea.add(area != null ? area : BigDecimal.ZERO);
+            subtotalAmount = subtotalAmount.add(p.getTaxInclusiveTotalPrice() != null ? p.getTaxInclusiveTotalPrice() : BigDecimal.ZERO);
+
+            itemDtos.add(itemDto);
+        }
+        dto.setItems(itemDtos);
+        dto.setSubtotalQuantity(subtotalQuantity);
+        dto.setSubtotalArea(subtotalArea.setScale(2, RoundingMode.HALF_UP));
+        dto.setSubtotalAmount(subtotalAmount);
+
+        // 澶勭悊鍏朵粬璐圭敤
+        List<Long> productIds = products.stream().map(SalesLedgerProduct::getId).collect(Collectors.toList());
+        BigDecimal otherFeesTotal = BigDecimal.ZERO;
+        if (CollectionUtils.isNotEmpty(productIds)) {
+            List<SalesLedgerProductProcessBind> binds = salesLedgerProductProcessBindService.list(
+                    new LambdaQueryWrapper<SalesLedgerProductProcessBind>().in(SalesLedgerProductProcessBind::getSalesLedgerProductId, productIds));
+
+            if (CollectionUtils.isNotEmpty(binds)) {
+                Map<Integer, Integer> processQuantityMap = binds.stream()
+                        .collect(Collectors.groupingBy(SalesLedgerProductProcessBind::getSalesLedgerProductProcessId,
+                                Collectors.summingInt(b -> b.getQuantity() != null ? b.getQuantity() : 0)));
+
+                List<Integer> processIds = new ArrayList<>(processQuantityMap.keySet());
+                List<SalesLedgerProductProcess> processes = salesLedgerProductProcessService.listByIds(processIds);
+
+                List<SalesOrdersDto.OtherFeeDto> otherFeeDtos = new ArrayList<>();
+
+                for (SalesLedgerProductProcess proc : processes) {
+                    SalesOrdersDto.OtherFeeDto feeDto = new SalesOrdersDto.OtherFeeDto();
+                    feeDto.setFeeName(proc.getProcessName());
+                    feeDto.setUnitPrice(proc.getUnitPrice());
+                    Integer qty = processQuantityMap.get(proc.getId());
+                    feeDto.setQuantity(new BigDecimal(qty != null ? qty : 0));
+                    BigDecimal amount = proc.getUnitPrice() != null ? proc.getUnitPrice().multiply(feeDto.getQuantity()) : BigDecimal.ZERO;
+                    feeDto.setAmount(amount);
+                    otherFeeDtos.add(feeDto);
+                    otherFeesTotal = otherFeesTotal.add(amount);
+                }
+                dto.setOtherFees(otherFeeDtos);
+            }
+        }
+
+        dto.setTotalQuantity(subtotalQuantity);
+        dto.setTotalArea(dto.getSubtotalArea());
+        dto.setTotalAmount(subtotalAmount.add(otherFeesTotal));
+        dto.setTotalAmountDisplay(dto.getTotalAmount().setScale(2, RoundingMode.HALF_UP).toString() + "鍏�");
+
+        return dto;
+    }
+
+    @Override
+    public SalesInvoicesDto salesInvoices(List<Long> salesLedgerIds) {
+        if (CollectionUtils.isEmpty(salesLedgerIds)) {
+            throw new ServiceException("閿�鍞彂璐у崟鎵撳嵃澶辫触,閿�鍞鍗曚笉鑳戒负绌�");
+        }
+
+        List<SalesLedger> ledgers = salesLedgerMapper.selectBatchIds(salesLedgerIds);
+        if (CollectionUtils.isEmpty(ledgers)) {
+            throw new ServiceException("閿�鍞彂璐у崟鎵撳嵃澶辫触,鏈壘鍒板搴斿彴璐﹁褰�");
+        }
+
+        Long customerId = ledgers.get(0).getCustomerId();
+        for (SalesLedger ledger : ledgers) {
+            if (!Objects.equals(customerId, ledger.getCustomerId())) {
+                throw new ServiceException("閿�鍞彂璐у崟鍚堝苟鎵撳嵃鍙兘鏄悓涓�涓鎴�");
+            }
+        }
+
+        SalesInvoicesDto dto = new SalesInvoicesDto();
+
+        Customer customer = customerMapper.selectById(customerId);
+        if (customer != null) {
+            dto.setCustomerName(customer.getCustomerName());
+            dto.setContactPerson(customer.getContactPerson());
+            dto.setContactPhone(customer.getContactPhone());
+
+            StringBuilder address = new StringBuilder();
+            if (customer.getRegionsId() != null) {
+                CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId());
+                if (regions != null) {
+                    address.append(regions.getRegionsName());
+                }
+            }
+            if (StringUtils.isNotEmpty(customer.getCompanyAddress())) {
+                address.append(customer.getCompanyAddress());
+            }
+            dto.setCompanyAddress(address.toString());
+        }
+
+        //  鍙戣揣鍗曞彿 (XF + 鏃ユ湡 + 搴忓垪)
+        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
+        String redisKey = "sales:delivery:seq:" + dateStr;
+        Long seq = redisTemplate.opsForValue().increment(redisKey);
+        if (seq != null && seq == 1) {
+            redisTemplate.expire(redisKey, 48, TimeUnit.HOURS);
+        }
+        dto.setDeliveryNo("XF" + dateStr + String.format("%03d", seq != null ? seq : 1));
+
+        // 瀵规柟鍗曞彿
+//        dto.setExternalOrderNo(ledgers.get(0).getCustomerContractNo());
+
+        //  鏌ヨ鎵�鏈変骇鍝�
+        List<SalesLedgerProduct> allProducts = salesLedgerProductMapper.selectList(
+                new LambdaQueryWrapper<SalesLedgerProduct>().in(SalesLedgerProduct::getSalesLedgerId, salesLedgerIds));
+
+        if (CollectionUtils.isNotEmpty(allProducts)) {
+            Map<Long, SalesLedger> ledgerMap = ledgers.stream()
+                    .collect(Collectors.toMap(SalesLedger::getId, Function.identity()));
+
+            Map<Long, List<SalesLedgerProduct>> groupedData = new LinkedHashMap<>();
+            for (SalesLedgerProduct p : allProducts) {
+                groupedData.computeIfAbsent(p.getSalesLedgerId(), k -> new ArrayList<>()).add(p);
+            }
+
+            List<SalesInvoicesDto.InvoiceOrderGroupDto> groups = new ArrayList<>();
+            BigDecimal totalQty = BigDecimal.ZERO;
+            BigDecimal totalArea = BigDecimal.ZERO;
+
+            for (Map.Entry<Long, List<SalesLedgerProduct>> ledgerEntry : groupedData.entrySet()) {
+                SalesLedger ledger = ledgerMap.get(ledgerEntry.getKey());
+                String orderNo = ledger != null ? ledger.getSalesContractNo() : "";
+                List<SalesLedgerProduct> products = ledgerEntry.getValue();
+
+                SalesInvoicesDto.InvoiceOrderGroupDto group = new SalesInvoicesDto.InvoiceOrderGroupDto();
+                group.setSalesContractNo(orderNo);
+                if (CollectionUtils.isNotEmpty(products)) {
+                    group.setProductName(products.get(0).getProductCategory());
+                }
+
+                List<SalesInvoicesDto.InvoiceItemDto> itemDtos = new ArrayList<>();
+                BigDecimal groupQty = BigDecimal.ZERO;
+                BigDecimal groupArea = BigDecimal.ZERO;
+
+                for (SalesLedgerProduct p : products) {
+                    SalesInvoicesDto.InvoiceItemDto item = new SalesInvoicesDto.InvoiceItemDto();
+                    item.setFloorCode(p.getFloorCode());
+                    item.setWidthHeight((p.getWidth() != null ? p.getWidth().stripTrailingZeros().toPlainString() : "0") +
+                            " * " + (p.getHeight() != null ? p.getHeight().stripTrailingZeros().toPlainString() : "0"));
+                    item.setQuantity(p.getQuantity());
+
+                    // 闈㈢Н
+                    BigDecimal area = p.getSettleTotalArea() != null ? p.getSettleTotalArea() : p.getActualTotalArea();
+                    if (area == null && p.getWidth() != null && p.getHeight() != null && p.getQuantity() != null) {
+                        area = p.getWidth().multiply(p.getHeight()).multiply(p.getQuantity()).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP);
+                    }
+                    item.setArea(area);
+                    item.setRemark(p.getRemark());
+                    item.setProcessRequirement(p.getProcessRequirement());
+
+                    itemDtos.add(item);
+                    groupQty = groupQty.add(p.getQuantity() != null ? p.getQuantity() : BigDecimal.ZERO);
+                    groupArea = groupArea.add(area != null ? area : BigDecimal.ZERO);
+                }
+
+                group.setItems(itemDtos);
+                group.setGroupTotalQuantity(groupQty);
+                group.setGroupTotalArea(groupArea.setScale(2, RoundingMode.HALF_UP));
+                groups.add(group);
+
+                totalQty = totalQty.add(groupQty);
+                totalArea = totalArea.add(groupArea);
+            }
+            dto.setGroups(groups);
+            dto.setTotalQuantity(totalQty);
+            dto.setTotalArea(totalArea.setScale(2, RoundingMode.HALF_UP));
+        }
+
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        if (loginUser != null && loginUser.getUser() != null) {
+            dto.setOrderMaker(loginUser.getUser().getNickName());
+        }
+        dto.setExecutionDate(LocalDateTime.now());
+
+        return dto;
+    }
+
+    @Override
+    public List<SalesLabelDto> salesLabel(Long salesLedgerId) {
+        if (salesLedgerId == null) {
+            throw new ServiceException("鎵撳嵃鏍囩澶辫触,鏁版嵁涓嶈兘涓虹┖");
+        }
+        SalesLedger salesLedger = baseMapper.selectById(salesLedgerId);
+        if (salesLedger == null) {
+            throw new ServiceException("鎵撳嵃澶辫触,閿�鍞鍗曚笉瀛樺湪");
+        }
+
+        // 鏌ヨ浜у搧鍒楄〃
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
+                new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId));
+
+        // 鏌ヨ瀹㈡埛鍦板潃
+        String fullAddress = "";
+        if (salesLedger.getCustomerId() != null) {
+            Customer customer = customerMapper.selectById(salesLedger.getCustomerId());
+            if (customer != null) {
+                StringBuilder addressSb = new StringBuilder();
+                if (customer.getRegionsId() != null) {
+                    CustomerRegions regions = customerRegionsService.getById(customer.getRegionsId());
+                    if (regions != null) {
+                        addressSb.append(regions.getRegionsName());
+                    }
+                }
+                if (StringUtils.isNotEmpty(customer.getCompanyAddress())) {
+                    addressSb.append(customer.getCompanyAddress());
+                }
+                fullAddress = addressSb.toString();
+            }
+        }
+
+        List<SalesLabelDto> list = new ArrayList<>();
+        if (CollectionUtils.isNotEmpty(products)) {
+            for (SalesLedgerProduct p : products) {
+                SalesLabelDto dto = new SalesLabelDto();
+                dto.setCustomerName(salesLedger.getCustomerName());
+                dto.setSalesContractNo(salesLedger.getSalesContractNo());
+                dto.setProductName(p.getProductCategory());
+
+                // 瀹�*楂�=鏁伴噺
+                String specification = (p.getWidth() != null ? p.getWidth().stripTrailingZeros().toPlainString() : "0") + "*" +
+                        (p.getHeight() != null ? p.getHeight().stripTrailingZeros().toPlainString() : "0") + "=" +
+                        (p.getQuantity() != null ? p.getQuantity().stripTrailingZeros().toPlainString() : "0");
+                dto.setSpecification(specification);
+
+                // 瀹㈡埛鍦板潃 + 妤煎眰缂栧彿
+                dto.setFloorCode(fullAddress + (StringUtils.isNotEmpty(p.getFloorCode()) ? " " + p.getFloorCode() : ""));
+                list.add(dto);
+            }
+        }
+
+        return list;
+    }
+
     private int findFirstMissingSequence(List<Integer> sequences) {
         if (sequences.isEmpty()) {
             return 1;

--
Gitblit v1.9.3