| | |
| | | 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.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 com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | if (productModelDto.getId() == null) { |
| | | ProductModel productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelDto,productModel); |
| | | productModelMapper.insert(productModel); |
| | | |
| | | String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | String idStr = String.format("%06d", productModel.getId()); // 六位补0 |
| | | String newProductCode = "BM" + dateStr + idStr; |
| | | // 更新数据库中的productCode |
| | | productModel.setProductCode(newProductCode); |
| | | return productModelMapper.updateById(productModel); |
| | | BeanUtils.copyProperties(productModelDto, productModel); |
| | | return productModelMapper.insert(productModel); |
| | | } else { |
| | | return productModelMapper.updateById(productModelDto); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | |
| | | |
| | | /** |
| | | * 根据id查询产品规格分页查询 |
| | | * |
| | | * @param page |
| | | * @param productDto |
| | | * @return |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Boolean importProduct(MultipartFile file) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AjaxResult importProductModel(MultipartFile file, Integer productId) { |
| | | if (productId == null) { |
| | | return AjaxResult.error("请先选择产品再导入规格型号"); |
| | | } |
| | | |
| | | Product product = productMapper.selectById(productId); |
| | | if (product == null) { |
| | | return AjaxResult.error("选择的产品不存在"); |
| | | } |
| | | |
| | | try { |
| | | ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class); |
| | | List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream()); |
| | | Map<String, List<ProductModel>> collect = productModelList.stream().collect(Collectors.groupingBy(ProductModel::getProductName)); |
| | | collect.forEach((k,v)->{ |
| | | Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getProductName, k).last("LIMIT 1")); |
| | | if (product != null) { |
| | | v.forEach(productModel -> { |
| | | productModel.setProductId(product.getId()); |
| | | }); |
| | | this.saveOrUpdateBatch(v); |
| | | |
| | | 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); |
| | | int rowNum = i + 2; |
| | | |
| | | if (StringUtils.isEmpty(item.getModel())) { |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [规格型号] 不能为空"); |
| | | } |
| | | }); |
| | | return true; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | 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()); |
| | | } |
| | | |
| | | 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); |
| | | throw new ServiceException("导入失败"); |
| | | } |
| | | return false; |
| | | } |
| | | } |