| | |
| | | package com.ruoyi.basic.service.impl; |
| | | |
| | | 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.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.dto.ProductDto; |
| | | 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.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | 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.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 【请填写功能名称】Service业务层处理 |
| | |
| | | @AllArgsConstructor |
| | | public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService { |
| | | |
| | | private final ProductMapper productMapper; |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | private ProductModelMapper productModelMapper; |
| | | |
| | | @Override |
| | |
| | | |
| | | if (productModelDto.getId() == null) { |
| | | ProductModel productModel = new ProductModel(); |
| | | BeanUtils.copyProperties(productModelDto,productModel); |
| | | BeanUtils.copyProperties(productModelDto, productModel); |
| | | return productModelMapper.insert(productModel); |
| | | } else { |
| | | return productModelMapper.updateById(productModelDto); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int delProductModel(Long[] ids) { |
| | | List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>() |
| | | .lambda().in(SalesLedgerProduct::getProductModelId, ids)); |
| | | if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) { |
| | | |
| | | throw new RuntimeException("已经存在该产品的销售台账和采购台账"); |
| | | } |
| | | return productModelMapper.deleteBatchIds(Arrays.asList(ids)); |
| | | } |
| | | |
| | |
| | | |
| | | /** |
| | | * 根据id查询产品规格分页查询 |
| | | * |
| | | * @param page |
| | | * @param productDto |
| | | * @return |
| | |
| | | queryWrapper.eq(ProductModel::getProductId, productDto.getId()); |
| | | return productModelMapper.selectPage(page, queryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | @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()); |
| | | |
| | | if (productModelList == null || productModelList.isEmpty()) { |
| | | return AjaxResult.error("导入数据不能为空"); |
| | | } |
| | | |
| | | 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 + " 行导入失败: [规格型号] 不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(item.getUnit())) { |
| | | return AjaxResult.error("第 " + rowNum + " 行导入失败: [单位] 不能为空"); |
| | | } |
| | | item.setProductId(product.getId()); |
| | | } |
| | | |
| | | saveOrUpdateBatch(productModelList); |
| | | return AjaxResult.success("成功导入 " + productModelList.size() + " 条数据"); |
| | | } catch (Exception e) { |
| | | log.error("导入产品规格异常", e); |
| | | return AjaxResult.error("导入失败"); |
| | | } |
| | | } |
| | | } |