| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | 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 com.ruoyi.basic.pojo.Product; |
| | | import com.ruoyi.basic.pojo.ProductModel; |
| | | import com.ruoyi.basic.service.IProductModelService; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class); |
| | | List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream()); |
| | | |
| | | if (productModelList == null || productModelList.isEmpty()) { |
| | | if (CollectionUtils.isEmpty(productModelList)) { |
| | | return AjaxResult.error("导入数据不能为空"); |
| | | } |
| | | |
| | | // 获取当前产品下所有的规格型号名 |
| | | List<ProductModel> existingModels = list(new LambdaQueryWrapper<ProductModel>().eq(ProductModel::getProductId, productId)); |
| | | Set<String> existingModelNames = existingModels.stream().map(ProductModel::getModel).collect(Collectors.toSet()); |
| | | |
| | | List<ProductModel> waitToSaveList = new ArrayList<>(); |
| | | int skipCount = 0; |
| | | |
| | | for (int i = 0; i < productModelList.size(); i++) { |
| | | ProductModel item = productModelList.get(i); |
| | |
| | | if (StringUtils.isEmpty(item.getUnit())) { |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [单位] 不能为空"); |
| | | } |
| | | |
| | | // 去重,如果已包含该型号,则跳过 |
| | | if (existingModelNames.contains(item.getModel())) { |
| | | skipCount++; |
| | | continue; |
| | | } |
| | | |
| | | item.setProductId(product.getId()); |
| | | waitToSaveList.add(item); |
| | | |
| | | existingModelNames.add(item.getModel()); |
| | | } |
| | | |
| | | saveOrUpdateBatch(productModelList); |
| | | return AjaxResult.success("成功导入 " + productModelList.size() + " 条数据"); |
| | | if (!waitToSaveList.isEmpty()) { |
| | | saveBatch(waitToSaveList); |
| | | } |
| | | |
| | | if (skipCount == 0) { |
| | | return AjaxResult.success(String.format("成功导入 %d 条数据", waitToSaveList.size())); |
| | | } else { |
| | | return AjaxResult.success(String.format("成功导入 %d 条,跳过已存在数据 %d 条", waitToSaveList.size(), skipCount)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("导入产品规格异常", e); |
| | | return AjaxResult.error("导入失败"); |
| | | 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(); |
| | | } |
| | | } |