|
@@ -0,0 +1,235 @@
|
|
|
+package cn.cslg.pas.service.impl;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.JsonPage;
|
|
|
+import cn.cslg.pas.common.model.PersonnelVO;
|
|
|
+import cn.cslg.pas.common.model.dto.*;
|
|
|
+import cn.cslg.pas.common.model.vo.ProductPictureVO;
|
|
|
+import cn.cslg.pas.common.model.vo.ProductVO;
|
|
|
+import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
+import cn.cslg.pas.common.utils.FileUtils;
|
|
|
+import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
|
|
|
+import cn.cslg.pas.domain.Product;
|
|
|
+import cn.cslg.pas.domain.asso.AssoProductPicture;
|
|
|
+import cn.cslg.pas.exception.XiaoShiException;
|
|
|
+import cn.cslg.pas.mapper.AssoProductPictureMapper;
|
|
|
+import cn.cslg.pas.mapper.ProductMapper;
|
|
|
+import cn.cslg.pas.service.IProductService;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 产品的Service层接口实现类
|
|
|
+ *
|
|
|
+ * @Author chenyu
|
|
|
+ * @Date 2023/3/8
|
|
|
+ */
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ProductServiceImpl implements IProductService {
|
|
|
+ private final ProductMapper productMapper;
|
|
|
+ private final AssoProductPictureMapper assoProductPictureMapper;
|
|
|
+ private final CacheUtils cacheUtils;
|
|
|
+ private final LoginUtils loginUtils;
|
|
|
+ private final FileUtils fileUtils;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增产品
|
|
|
+ *
|
|
|
+ * @param productAddNewDTO 新增产品的DTO对象
|
|
|
+ * @param files 产品图片
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void addNew(ProductAddNewDTO productAddNewDTO, List<MultipartFile> files) {
|
|
|
+ log.info("开始处理【新增产品】的业务,参数为:{}, {}", productAddNewDTO, files);
|
|
|
+
|
|
|
+ //检查产品名称是否被占用
|
|
|
+ String productName = productAddNewDTO.getProductName();
|
|
|
+ log.info("检查产品名称是否被占用");
|
|
|
+ int count = productMapper.countByProductName(productName);
|
|
|
+ if (count > 0) {
|
|
|
+ String message = "新增产品失败,产品名称【" + productName + "】已存在,请尝试更换名称";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ Double licenseRate = productAddNewDTO.getLicenseRate();
|
|
|
+ if (licenseRate < 0 || licenseRate > 1) {
|
|
|
+ String message = "新增产品失败,许可费率不合法,请填写正确数值";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //产品新增DTO对象赋值给实体类
|
|
|
+ Product product = new Product();
|
|
|
+ BeanUtils.copyProperties(productAddNewDTO, product);
|
|
|
+ //获取当前登陆人信息并赋值给实体类
|
|
|
+ PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
|
|
|
+ product
|
|
|
+ .setCreatePersonId(personnelVO.getId())
|
|
|
+ .setCreatePersonName(personnelVO.getName());
|
|
|
+ //数据入产品表
|
|
|
+ log.info("数据入产品表");
|
|
|
+ int rows = productMapper.insert(product);
|
|
|
+ if (rows != 1) {
|
|
|
+ String message = "新增产品失败,服务器忙请稍后再试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果有图片,图片文件上传至服务器,后获取图片文件的名称、后缀名、url地址,赋值给产品类别图片关联表实体类
|
|
|
+ if (files != null && files.size() != 0) {
|
|
|
+ ArrayList<AssoProductPicture> assoProductPictures = new ArrayList<>();
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ UploadFileDTO fileDTO = fileUtils.uploadFile(file);
|
|
|
+ //获取并将产品id、图片文件的名称、后缀名、url赋值给产品图片关联表实体类
|
|
|
+ AssoProductPicture assoProductPicture = new AssoProductPicture()
|
|
|
+ .setProductId(product.getId())
|
|
|
+ .setName(fileDTO.getName())
|
|
|
+ .setSuffix(fileDTO.getExtName())
|
|
|
+ .setUrl(fileDTO.getPath());
|
|
|
+ assoProductPictures.add(assoProductPicture);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("数据入产品图片关联表");
|
|
|
+ rows = assoProductPictureMapper.insertBatch(assoProductPictures);
|
|
|
+ if (rows != assoProductPictures.size()) {
|
|
|
+ String message = "新增产品失败,数据入产品图片关联表失败,服务器忙请稍后再试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("新增产品完成");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改产品
|
|
|
+ *
|
|
|
+ * @param productUpdateDTO 修改产品的DTO对象
|
|
|
+ * @param files 产品图片
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void update(ProductUpdateDTO productUpdateDTO, List<MultipartFile> files) {
|
|
|
+ log.info("开始处理【修改产品】的业务,参数为:{}, {}", productUpdateDTO, files);
|
|
|
+
|
|
|
+ //检查尝试修改的数据是否存在
|
|
|
+ Integer productId = productUpdateDTO.getId();
|
|
|
+ log.info("检查尝试修改的数据是否存在");
|
|
|
+ int count = productMapper.countById(productId);
|
|
|
+ if (count == 0) {
|
|
|
+ String message = "修改产品失败,尝试访问的数据已不存在";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //产品DTO对象赋值给实体类
|
|
|
+ Product product = new Product();
|
|
|
+ BeanUtils.copyProperties(productUpdateDTO, product);
|
|
|
+ //修改产品
|
|
|
+ log.info("修改产品");
|
|
|
+ int rows = productMapper.updateById(product);
|
|
|
+ if (rows != 1) {
|
|
|
+ String message = "修改产品失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //先通过产品id查询出所有原来的图片文件id
|
|
|
+ List<ProductPictureVO> productPictureVOs = assoProductPictureMapper.selectByProductId(productId);
|
|
|
+ ArrayList<Integer> oldPictureIds = new ArrayList<>();
|
|
|
+ for (ProductPictureVO productPictureVO : productPictureVOs) {
|
|
|
+ Integer pictureId = productPictureVO.getId();
|
|
|
+ oldPictureIds.add(pictureId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //再从DTO中取出传过来的图片id
|
|
|
+ List<ProductPictureUpdateDTO> pictures = productUpdateDTO.getPictures();
|
|
|
+ ArrayList<Integer> oldNewPictureIds = new ArrayList<>();
|
|
|
+ for (ProductPictureUpdateDTO picture : pictures) {
|
|
|
+ Integer oldNewPictureId = picture.getId();
|
|
|
+ oldNewPictureIds.add(oldNewPictureId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //图片id集合去重,保留下来的即被删除的图片id
|
|
|
+ oldPictureIds.removeAll(oldNewPictureIds);
|
|
|
+ if (oldPictureIds.size() > 0) {
|
|
|
+ log.info("产品图片关联表删除被删除的图片");
|
|
|
+ assoProductPictureMapper.deleteByIds(oldPictureIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (files != null && files.size() != 0) {
|
|
|
+ ArrayList<AssoProductPicture> assoProductPictures = new ArrayList<>();
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ UploadFileDTO fileDTO = fileUtils.uploadFile(file);
|
|
|
+ AssoProductPicture assoProductPicture = new AssoProductPicture()
|
|
|
+ .setProductId(productId)
|
|
|
+ .setName(fileDTO.getName())
|
|
|
+ .setSuffix(fileDTO.getExtName())
|
|
|
+ .setUrl(fileDTO.getPath());
|
|
|
+ assoProductPictures.add(assoProductPicture);
|
|
|
+ }
|
|
|
+
|
|
|
+ //新数据入产品类别图片关联表
|
|
|
+ log.info("新数据入产品类别图片关联表");
|
|
|
+ assoProductPictureMapper.insertBatch(assoProductPictures);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("修改产品完成");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JsonPage query(ProductQueryPageDTO productQueryPageDTO) {
|
|
|
+ log.info("开始处理【查询产品】的业务,参数为:{}", productQueryPageDTO);
|
|
|
+
|
|
|
+ Integer current = productQueryPageDTO.getCurrent();
|
|
|
+ Integer size = productQueryPageDTO.getSize();
|
|
|
+ if (current != null && size != null) {
|
|
|
+ PageHelper.startPage(current, size);
|
|
|
+ }
|
|
|
+ List<ProductVO> productVOs = productMapper.query(productQueryPageDTO);
|
|
|
+ return JsonPage.restPage(new PageInfo<>(productVOs));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Integer id) {
|
|
|
+ log.info("开始处理【删除产品】的业务,参数为:{}", id);
|
|
|
+
|
|
|
+ //检查尝试访问的数据是否存在
|
|
|
+ int count = productMapper.countById(id);
|
|
|
+ if (count == 0) {
|
|
|
+ String message = "删除产品失败,尝试访问的数据已不存在";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除产品图片数据
|
|
|
+ log.info("产品图片关联表删除数据");
|
|
|
+ assoProductPictureMapper.deleteByProductId(id);
|
|
|
+
|
|
|
+ //删除产品数据
|
|
|
+ log.info("产品表删除数据");
|
|
|
+ int rows = productMapper.deleteById(id);
|
|
|
+ if (rows != 1) {
|
|
|
+ String message = "删除产品失败,服务器忙请稍后再次尝试!";
|
|
|
+ log.info("{}", message);
|
|
|
+ throw new XiaoShiException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("删除产品完成");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|