|
@@ -0,0 +1,234 @@
|
|
|
|
+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.ProductCategoryPictureVO;
|
|
|
|
+import cn.cslg.pas.common.model.vo.ProductCategoryVO;
|
|
|
|
+import cn.cslg.pas.common.utils.CacheUtils;
|
|
|
|
+import cn.cslg.pas.common.utils.FileUtils;
|
|
|
|
+import cn.cslg.pas.common.utils.Response;
|
|
|
|
+import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
|
|
|
|
+import cn.cslg.pas.domain.ProductCategory;
|
|
|
|
+import cn.cslg.pas.domain.asso.AssoProductCategoryPicture;
|
|
|
|
+import cn.cslg.pas.exception.XiaoShiException;
|
|
|
|
+import cn.cslg.pas.mapper.AssoProductCategoryPictureMapper;
|
|
|
|
+import cn.cslg.pas.mapper.ProductCategoryMapper;
|
|
|
|
+import cn.cslg.pas.service.IProductCategoryService;
|
|
|
|
+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/7
|
|
|
|
+ */
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+public class ProductCategoryServiceImpl implements IProductCategoryService {
|
|
|
|
+ private final ProductCategoryMapper productCategoryMapper;
|
|
|
|
+ private final AssoProductCategoryPictureMapper assoProductCategoryPictureMapper;
|
|
|
|
+ private final CacheUtils cacheUtils;
|
|
|
|
+ private final LoginUtils loginUtils;
|
|
|
|
+ private final FileUtils fileUtils;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新增产品类别
|
|
|
|
+ *
|
|
|
|
+ * @param productCategoryAddNewDTO 产品类别数据对象
|
|
|
|
+ * @param files 产品类别图片
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void addNew(ProductCategoryAddNewDTO productCategoryAddNewDTO, List<MultipartFile> files) {
|
|
|
|
+ log.info("开始处理【新增产品类别】的业务,参数为:{}, {}", productCategoryAddNewDTO, files);
|
|
|
|
+
|
|
|
|
+ //检查产品类别名称是否被占用
|
|
|
|
+ String productCategoryName = productCategoryAddNewDTO.getProductCategoryName();
|
|
|
|
+ log.info("检查产品类别名称是否被占用");
|
|
|
|
+ int count = productCategoryMapper.countByProductCategoryName(productCategoryName);
|
|
|
|
+ if (count > 0) {
|
|
|
|
+ String message = "新增产品类别失败,产品类别名称【" + productCategoryName + "】已存在,请尝试更换名称";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //新增产品类别DTO对象赋值给产品类别表实体类
|
|
|
|
+ ProductCategory productCategory = new ProductCategory();
|
|
|
|
+ BeanUtils.copyProperties(productCategoryAddNewDTO, productCategory);
|
|
|
|
+ //获取当前登陆人信息
|
|
|
|
+ PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
|
|
|
|
+ productCategory
|
|
|
|
+ .setCreatePersonId(personnelVO.getId())
|
|
|
|
+ .setCreatePersonName(personnelVO.getName());
|
|
|
|
+ //数据入产品类别表
|
|
|
|
+ log.info("数据入产品类别表");
|
|
|
|
+ int rows = productCategoryMapper.insert(productCategory);
|
|
|
|
+ if (rows != 1) {
|
|
|
|
+ String message = "新增产品类别失败,数据入产品类别表失败,服务器忙请稍后再次尝试";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //如果有图片,图片文件上传至服务器,后获取url地址,赋值给产品类别图片关联表实体类
|
|
|
|
+ if (files != null) {
|
|
|
|
+ ArrayList<AssoProductCategoryPicture> assoProductCategoryPictures = new ArrayList<>();
|
|
|
|
+ for (MultipartFile file : files) {
|
|
|
|
+ //图片文件上传至服务器
|
|
|
|
+ UploadFileDTO fileDTO = fileUtils.uploadFile(file);
|
|
|
|
+ //获取图片文件的url地址
|
|
|
|
+ String picturePath = fileDTO.getPath();
|
|
|
|
+ AssoProductCategoryPicture assoProductCategoryPicture = new AssoProductCategoryPicture()
|
|
|
|
+ .setProductCategoryId(productCategory.getId())
|
|
|
|
+ .setPicture(picturePath);
|
|
|
|
+ assoProductCategoryPictures.add(assoProductCategoryPicture);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("数据入产品类别图片关联表");
|
|
|
|
+ rows = assoProductCategoryPictureMapper.insertBatch(assoProductCategoryPictures);
|
|
|
|
+ if (rows != assoProductCategoryPictures.size()) {
|
|
|
|
+ String message = "新增产品类别失败,数据入产品类别图片关联表失败,服务器忙请稍后再次尝试";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("新增产品类别完成");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JsonPage query(ProductCategoryQueryPageDTO productCategoryQueryPageDTO) {
|
|
|
|
+ log.info("开始处理【分页查询产品类别】的业务,参数为:{}", productCategoryQueryPageDTO);
|
|
|
|
+
|
|
|
|
+ Integer current = productCategoryQueryPageDTO.getCurrent();
|
|
|
|
+ Integer size = productCategoryQueryPageDTO.getSize();
|
|
|
|
+ if (current != null && size != null) {
|
|
|
|
+ PageHelper.startPage(current, size);
|
|
|
|
+ }
|
|
|
|
+ List<ProductCategoryVO> queryResults = productCategoryMapper.query(productCategoryQueryPageDTO);
|
|
|
|
+ return JsonPage.restPage(new PageInfo<>(queryResults));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 修改产品类别
|
|
|
|
+ *
|
|
|
|
+ * @param productCategoryUpdateDTO 产品类别数据对象
|
|
|
|
+ * @param files 产品类别图片
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void update(ProductCategoryUpdateDTO productCategoryUpdateDTO, List<MultipartFile> files) {
|
|
|
|
+ log.info("开始处理【修改产品类别】的业务,参数为:{}, {}", productCategoryUpdateDTO, files);
|
|
|
|
+
|
|
|
|
+ //检查尝试修改的数据是否存在
|
|
|
|
+ Integer productCategoryId = productCategoryUpdateDTO.getId();
|
|
|
|
+ log.info("检查尝试修改的数据是否存在");
|
|
|
|
+ int count = productCategoryMapper.countById(productCategoryId);
|
|
|
|
+ if (count == 0) {
|
|
|
|
+ String message = "修改产品类别失败,尝试访问的数据已不存在";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //产品类别DTO对象赋值给实体类
|
|
|
|
+ ProductCategory productCategory = new ProductCategory();
|
|
|
|
+ BeanUtils.copyProperties(productCategoryUpdateDTO, productCategory);
|
|
|
|
+ //修改产品类别
|
|
|
|
+ log.info("修改产品类别");
|
|
|
|
+ int rows = productCategoryMapper.updateById(productCategory);
|
|
|
|
+ if (rows != 1) {
|
|
|
|
+ String message = "修改产品类别失败,服务器忙请稍后再次尝试!";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //先通过产品类别id查询出所有原来的图片文件
|
|
|
|
+ ArrayList<Integer> oldPictureIds = new ArrayList<>();
|
|
|
|
+ List<ProductCategoryPictureVO> productCategoryPictureVOS = assoProductCategoryPictureMapper.queryListByProductCategoryId(productCategoryId);
|
|
|
|
+ for (ProductCategoryPictureVO productCategoryPictureVO : productCategoryPictureVOS) {
|
|
|
|
+ Integer oldPictureId = productCategoryPictureVO.getId();
|
|
|
|
+ oldPictureIds.add(oldPictureId);
|
|
|
|
+ }
|
|
|
|
+ ArrayList<Integer> newOldPictureIds = new ArrayList<>();
|
|
|
|
+ List<ProductCategoryPictureDTO> productCategoryPictureDTOS = productCategoryUpdateDTO.getPictures();
|
|
|
|
+ System.out.println(productCategoryPictureDTOS);
|
|
|
|
+ for (ProductCategoryPictureDTO productCategoryPictureDTO : productCategoryPictureDTOS) {
|
|
|
|
+ Integer newOldPictureId = productCategoryPictureDTO.getId();
|
|
|
|
+ newOldPictureIds.add(newOldPictureId);
|
|
|
|
+ }
|
|
|
|
+ //集合去重,去重后留下的数据即为被删除的原有图片
|
|
|
|
+ oldPictureIds.removeAll(newOldPictureIds);
|
|
|
|
+
|
|
|
|
+ if (oldPictureIds.size() != 0) {
|
|
|
|
+ log.info("产品类别图片关联表删除原有图片");
|
|
|
|
+ assoProductCategoryPictureMapper.deleteByIds(oldPictureIds);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ArrayList<AssoProductCategoryPicture> assoProductCategoryPictures = new ArrayList<>();
|
|
|
|
+ if (files != null) {
|
|
|
|
+ for (MultipartFile file : files) {
|
|
|
|
+ UploadFileDTO fileDTO = fileUtils.uploadFile(file);
|
|
|
|
+ String picturePath = fileDTO.getPath();
|
|
|
|
+ AssoProductCategoryPicture assoProductCategoryPicture = new AssoProductCategoryPicture()
|
|
|
|
+ .setProductCategoryId(productCategoryId)
|
|
|
|
+ .setPicture(picturePath);
|
|
|
|
+ assoProductCategoryPictures.add(assoProductCategoryPicture);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //新数据入产品类别图片关联表
|
|
|
|
+ log.info("新数据入产品类别图片关联表");
|
|
|
|
+ assoProductCategoryPictureMapper.insertBatch(assoProductCategoryPictures);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("修改产品类别完成");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void delete(Integer id) {
|
|
|
|
+ log.info("开始处理【删除产品类别】的业务,参数为:{}", id);
|
|
|
|
+
|
|
|
|
+ //检查尝试删除的数据是否存在
|
|
|
|
+ log.info("检查尝试删除的数据是否存在");
|
|
|
|
+ int count = productCategoryMapper.countById(id);
|
|
|
|
+ if (count == 0) {
|
|
|
|
+ String message = "删除产品类别失败,尝试访问的数据不存在";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //删除产品类别
|
|
|
|
+ log.info("删除产品类别");
|
|
|
|
+ int rows = productCategoryMapper.deleteById(id);
|
|
|
|
+ if (rows != 1) {
|
|
|
|
+ String message = "删除产品类别失败,服务器忙请稍后再试!";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //检查该产品类别是否有图片
|
|
|
|
+ count = assoProductCategoryPictureMapper.countByProductCategoryId(id);
|
|
|
|
+ if (count != 0) {
|
|
|
|
+ log.info("产品类别图片关联表删除数据");
|
|
|
|
+ rows = assoProductCategoryPictureMapper.deleteByProductCategoryId(id);
|
|
|
|
+ if (rows != count) {
|
|
|
|
+ String message = "删除产品类别失败,产品类别图片关联表删除数据失败,服务器忙请稍后再试!";
|
|
|
|
+ log.info("{}", message);
|
|
|
|
+ throw new XiaoShiException(message);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info("删除产品类别完成");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|