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/basic/service/impl/CustomerRegionsServiceImpl.java |  216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 216 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/CustomerRegionsServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/CustomerRegionsServiceImpl.java
new file mode 100644
index 0000000..bafb590
--- /dev/null
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerRegionsServiceImpl.java
@@ -0,0 +1,216 @@
+package com.ruoyi.basic.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.dto.CustomerRegionsTreeDto;
+import com.ruoyi.basic.mapper.CustomerMapper;
+import com.ruoyi.basic.mapper.CustomerRegionsMapper;
+import com.ruoyi.basic.pojo.Customer;
+import com.ruoyi.basic.pojo.CustomerRegions;
+import com.ruoyi.basic.service.ICustomerRegionsService;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * <p>
+ * 瀹㈡埛鍦板尯琛� 鏈嶅姟瀹炵幇绫�
+ * </p>
+ *
+ * @author deslrey
+ * @since 2026-03-27
+ */
+@Service
+public class CustomerRegionsServiceImpl extends ServiceImpl<CustomerRegionsMapper, CustomerRegions> implements ICustomerRegionsService {
+
+    @Autowired
+    private CustomerMapper customerMapper;
+
+    /**
+     * 鏌ヨ鏍戝舰鍦板尯鍒楄〃
+     */
+    @Override
+    public List<CustomerRegionsTreeDto> customerRegionsList(CustomerRegions customerRegions) {
+        List<CustomerRegions> allRegions = baseMapper.selectList(null);
+        if (allRegions == null || allRegions.isEmpty()) {
+            return new ArrayList<>();
+        }
+
+        Map<Long, CustomerRegionsTreeDto> nodeMap = new HashMap<>();
+        List<CustomerRegionsTreeDto> allDtoList = new ArrayList<>();
+
+        for (CustomerRegions region : allRegions) {
+            CustomerRegionsTreeDto dto = new CustomerRegionsTreeDto();
+            dto.setId(region.getId());
+            dto.setParentId(region.getParentId());
+            dto.setRegionsName(region.getRegionsName());
+            dto.setLabel(region.getRegionsName()); // 缁熶竴 label 瀛楁
+            dto.setChildren(new ArrayList<>());
+            nodeMap.put(dto.getId(), dto);
+            allDtoList.add(dto);
+        }
+
+        List<CustomerRegionsTreeDto> treeList = new ArrayList<>();
+        for (CustomerRegionsTreeDto node : allDtoList) {
+            Long parentId = node.getParentId();
+            // 濡傛灉鏄牴鑺傜偣 (parentId 涓� 0 鎴� null) 鎴� 鎵句笉鍒扮埗鑺傜偣锛屽垯浣滀负椤剁骇鑺傜偣
+            if (parentId == null || parentId == 0 || !nodeMap.containsKey(parentId)) {
+                treeList.add(node);
+            } else {
+                // 鍚﹀垯灏嗗叾娣诲姞鍒扮埗鑺傜偣鐨� children 涓�
+                nodeMap.get(parentId).getChildren().add(node);
+            }
+        }
+
+        String keyword = customerRegions != null ? customerRegions.getRegionsName() : null;
+        if (StringUtils.isNotEmpty(keyword)) {
+            return filterTree(treeList, keyword);
+        }
+
+        return treeList;
+    }
+
+    /**
+     * 閫掑綊杩囨护鏍戣妭鐐�
+     */
+    private List<CustomerRegionsTreeDto> filterTree(List<CustomerRegionsTreeDto> list, String keyword) {
+        List<CustomerRegionsTreeDto> filteredList = new ArrayList<>();
+        for (CustomerRegionsTreeDto node : list) {
+            // 閫掑綊澶勭悊瀛愯妭鐐�
+            List<CustomerRegionsTreeDto> filteredChildren = filterTree(node.getChildren(), keyword);
+            node.setChildren(filteredChildren);
+
+            // 濡傛灉褰撳墠鑺傜偣鍚嶇О鍖呭惈鍏抽敭瀛楋紝鎴栬�呭瓙鑺傜偣杩囨护鍚庝笉涓虹┖锛屽垯淇濈暀璇ヨ妭鐐�
+            if (node.getRegionsName().contains(keyword) || !filteredChildren.isEmpty()) {
+                filteredList.add(node);
+            }
+        }
+        return filteredList;
+    }
+
+    /**
+     * 娣诲姞鍦板尯
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void addCustomerRegions(CustomerRegions customerRegions) {
+        validateRegion(customerRegions);
+        checkUnique(customerRegions);
+        if (baseMapper.insert(customerRegions) <= 0) {
+            throw new ServiceException("娣诲姞澶辫触");
+        }
+    }
+
+    /**
+     * 淇敼鍦板尯
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void updateCustomerRegions(CustomerRegions customerRegions) {
+        if (customerRegions.getId() == null) {
+            throw new ServiceException("淇敼澶辫触, ID涓嶈兘涓虹┖");
+        }
+        validateRegion(customerRegions);
+        if (customerRegions.getId().equals(customerRegions.getParentId())) {
+            throw new ServiceException("淇敼澶辫触, 涓婄骇鍦板尯涓嶈兘鏄嚜宸�");
+        }
+        checkUnique(customerRegions);
+        if (baseMapper.updateById(customerRegions) <= 0) {
+            throw new ServiceException("淇敼澶辫触");
+        }
+    }
+
+    /**
+     * 鍒犻櫎鍦板尯
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void delCustomerRegions(Long id) {
+        if (id == null) {
+            throw new ServiceException("鍒犻櫎澶辫触, ID涓嶈兘涓虹┖");
+        }
+        boolean hasChildren = baseMapper.selectCount(new LambdaQueryWrapper<CustomerRegions>()
+                .eq(CustomerRegions::getParentId, id)) > 0;
+        if (hasChildren) {
+            throw new ServiceException("鍒犻櫎澶辫触, 璇ュ湴鍖轰笅瀛樺湪瀛愬湴鍖�");
+        }
+
+        boolean isUsedByCustomer = customerMapper.selectCount(new LambdaQueryWrapper<Customer>()
+                .eq(Customer::getRegionsId, id)) > 0;
+        if (isUsedByCustomer) {
+            throw new ServiceException("鍒犻櫎澶辫触, 璇ュ湴鍖哄凡琚鎴锋。妗堝紩鐢�");
+        }
+
+        if (baseMapper.deleteById(id) <= 0) {
+            throw new ServiceException("鍒犻櫎澶辫触, 鏁版嵁涓嶅瓨鍦�");
+        }
+    }
+
+    /**
+     * 鏌ヨ褰撳墠鍦板尯鍙婂叾鎵�鏈夊瓙闆嗙殑 ID 闆嗗悎
+     */
+    @Override
+    public List<Long> regionsChildrenIds(Long regionsId) {
+        List<Long> childIds = new ArrayList<>();
+        if (regionsId == null) {
+            return childIds;
+        }
+
+        List<CustomerRegions> allRegions = baseMapper.selectList(null);
+        if (allRegions == null || allRegions.isEmpty()) {
+            return childIds;
+        }
+        childIds.add(regionsId);
+        findAllChildren(allRegions, regionsId, childIds);
+
+        return childIds;
+    }
+
+    /**
+     * 閫掑綊鍦板尯鏂规硶
+     *
+     * @param allRegions 鎵�鏈夊湴鍖哄垪琛�
+     * @param parentId   褰撳墠鐖剁骇 ID
+     * @param result     瀛樻斁缁撴灉鐨勯泦鍚�
+     */
+    private void findAllChildren(List<CustomerRegions> allRegions, Long parentId, List<Long> result) {
+        for (CustomerRegions region : allRegions) {
+            // 鍒ゆ柇 parentId 鏄惁鍖归厤
+            if (parentId.equals(region.getParentId())) {
+                result.add(region.getId());
+                // 缁х画閫掑綊鏌ユ壘璇ヨ妭鐐圭殑瀛愯妭鐐�
+                findAllChildren(allRegions, region.getId(), result);
+            }
+        }
+    }
+
+    private void validateRegion(CustomerRegions region) {
+        if (region == null) {
+            throw new ServiceException("鎿嶄綔澶辫触, 鏁版嵁涓嶈兘涓虹┖");
+        }
+        if (StringUtils.isEmpty(region.getRegionsName())) {
+            throw new ServiceException("鎿嶄綔澶辫触, 鍦板尯鍚嶇О涓嶈兘涓虹┖");
+        }
+        if (region.getParentId() == null) {
+            region.setParentId(0L);
+        }
+    }
+
+    private void checkUnique(CustomerRegions region) {
+        CustomerRegions existing = baseMapper.selectOne(new LambdaQueryWrapper<CustomerRegions>()
+                .eq(CustomerRegions::getRegionsName, region.getRegionsName())
+                .eq(CustomerRegions::getParentId, region.getParentId())
+                .ne(region.getId() != null, CustomerRegions::getId, region.getId()));
+
+        if (existing != null) {
+            throw new ServiceException("璇ュ眰绾т笅宸插瓨鍦ㄥ悕涓� [" + region.getRegionsName() + "] 鐨勫湴鍖�");
+        }
+    }
+}

--
Gitblit v1.9.3