| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.framework.web.domain.R; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.sales.dto.LossProductModelDto; |
| | | import com.ruoyi.sales.mapper.SalesLedgerProductMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerProduct; |
| | |
| | | String productCode = StringUtils.trim(productModelDto.getProductCode()); |
| | | productModelDto.setModel(model); |
| | | productModelDto.setProductCode(productCode); |
| | | checkModelAndProductCodeUnique(model, productCode, productModelDto.getId()); |
| | | // 产品编号非必填,规格型号在同一产品下唯一 |
| | | checkModelUnique(productModelDto.getProductId(), model, productModelDto.getId()); |
| | | |
| | | if (productModelDto.getId() == null) { |
| | | ProductModel productModel = new ProductModel(); |
| | |
| | | } |
| | | } |
| | | |
| | | private void checkModelAndProductCodeUnique(String model, String productCode, Long currentId) { |
| | | if (StringUtils.isEmpty(model) || StringUtils.isEmpty(productCode)) { |
| | | private void checkModelUnique(Long productId, String model, Long currentId) { |
| | | if (StringUtils.isEmpty(model)) { |
| | | return; |
| | | } |
| | | LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProductModel::getModel, model) |
| | | .eq(ProductModel::getProductCode, productCode) |
| | | queryWrapper.eq(ProductModel::getProductId, productId) |
| | | .eq(ProductModel::getModel, model) |
| | | .ne(currentId != null, ProductModel::getId, currentId) |
| | | .last("limit 1"); |
| | | ProductModel duplicateProductModel = productModelMapper.selectOne(queryWrapper); |
| | | if (duplicateProductModel != null) { |
| | | throw new ServiceException("对应的型号" + model + "的产品编码" + productCode + "已经存在"); |
| | | throw new ServiceException("对应的型号" + model + "已经存在"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R<?> importProductModel(MultipartFile file, Integer productId) { |
| | | public AjaxResult importProductModel(MultipartFile file, Integer productId) { |
| | | if (productId == null) { |
| | | return R.fail("请先选择产品再导入规格型号"); |
| | | return AjaxResult.error("请先选择产品再导入规格型号"); |
| | | } |
| | | |
| | | Product product = productMapper.selectById(productId); |
| | | if (product == null) { |
| | | return R.fail("选择的产品不存在"); |
| | | return AjaxResult.error("选择的产品不存在"); |
| | | } |
| | | |
| | | try { |
| | |
| | | List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream()); |
| | | |
| | | if (CollectionUtils.isEmpty(productModelList)) { |
| | | return R.fail("导入数据不能为空"); |
| | | return AjaxResult.error("导入数据不能为空"); |
| | | } |
| | | |
| | | // 获取当前产品下所有的规格型号名 |
| | |
| | | int rowNum = i + 2; |
| | | |
| | | if (StringUtils.isEmpty(item.getProductCode())) { |
| | | return R.fail("第 " + rowNum + " 行导入失败: [产品编码] 不能为空"); |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [产品编码] 不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(item.getModel())) { |
| | | return R.fail("第 " + rowNum + " 行导入失败: [规格型号] 不能为空"); |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [规格型号] 不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(item.getUnit())) { |
| | | return R.fail("第 " + rowNum + " 行导入失败: [单位] 不能为空"); |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [单位] 不能为空"); |
| | | } |
| | | |
| | | // 去重,如果已包含该型号,则跳过 |
| | |
| | | } |
| | | |
| | | if (skipCount == 0) { |
| | | return R.ok(null, String.format("成功导入 %d 条数据", waitToSaveList.size())); |
| | | return AjaxResult.success(String.format("成功导入 %d 条数据", waitToSaveList.size())); |
| | | } else { |
| | | return R.ok(null, String.format("成功导入 %d 条,跳过已存在数据 %d 条", waitToSaveList.size(), skipCount)); |
| | | return AjaxResult.success(String.format("成功导入 %d 条,跳过已存在数据 %d 条", waitToSaveList.size(), skipCount)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("导入产品规格异常", e); |