| | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.dto.ProductDto; |
| | | import com.ruoyi.basic.dto.ProductModelDataExportDto; |
| | | import com.ruoyi.basic.dto.ProductModelDto; |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | throw new ServiceException("瀵煎叆澶辫触"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void exportData(HttpServletResponse response, Long productId) { |
| | | List<Product> allProducts = productMapper.selectList(null); |
| | | Map<Long, Product> productMap = allProducts.stream() |
| | | .filter(product -> product.getId() != null) |
| | | .collect(Collectors.toMap(Product::getId, product -> product)); |
| | | Map<Long, List<Long>> parentChildrenMap = new HashMap<>(); |
| | | for (Product product : allProducts) { |
| | | if (product.getParentId() == null || product.getId() == null) { |
| | | continue; |
| | | } |
| | | parentChildrenMap.computeIfAbsent(product.getParentId(), key -> new ArrayList<>()).add(product.getId()); |
| | | } |
| | | |
| | | LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (productId != null) { |
| | | Set<Long> targetProductIds = new HashSet<>(); |
| | | ArrayDeque<Long> queue = new ArrayDeque<>(); |
| | | queue.offer(productId); |
| | | while (!queue.isEmpty()) { |
| | | Long currentId = queue.poll(); |
| | | if (currentId == null || !targetProductIds.add(currentId)) { |
| | | continue; |
| | | } |
| | | List<Long> children = parentChildrenMap.get(currentId); |
| | | if (children != null && !children.isEmpty()) { |
| | | queue.addAll(children); |
| | | } |
| | | } |
| | | queryWrapper.in(ProductModel::getProductId, targetProductIds); |
| | | } |
| | | List<ProductModel> productModelList = list(queryWrapper); |
| | | List<ProductModelDataExportDto> exportList = productModelList.stream().map(item -> { |
| | | ProductModelDataExportDto dto = new ProductModelDataExportDto(); |
| | | Product currentProduct = productMap.get(item.getProductId()); |
| | | dto.setProductName(currentProduct == null ? "" : currentProduct.getProductName()); |
| | | dto.setProductCategoryName(resolveRootCategoryName(currentProduct, productMap)); |
| | | dto.setModel(item.getModel()); |
| | | dto.setUnit(item.getUnit()); |
| | | return dto; |
| | | }).collect(Collectors.toList()); |
| | | ExcelUtil<ProductModelDataExportDto> excelUtil = new ExcelUtil<>(ProductModelDataExportDto.class); |
| | | excelUtil.exportExcel(response, exportList, "浜у搧瑙勬牸鏁版嵁"); |
| | | } |
| | | |
| | | private String resolveRootCategoryName(Product currentProduct, Map<Long, Product> productMap) { |
| | | if (currentProduct == null) { |
| | | return ""; |
| | | } |
| | | Product node = currentProduct; |
| | | while (node.getParentId() != null) { |
| | | Product parent = productMap.get(node.getParentId()); |
| | | if (parent == null) { |
| | | break; |
| | | } |
| | | node = parent; |
| | | } |
| | | return node.getProductName(); |
| | | } |
| | | } |