|
@@ -0,0 +1,144 @@
|
|
|
+package cn.cslg.pas.service.business;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.business.ProductDTO;
|
|
|
+import cn.cslg.pas.common.model.cronModel.GroupVO;
|
|
|
+import cn.cslg.pas.common.model.request.GroupRequest;
|
|
|
+import cn.cslg.pas.common.model.request.QueryRequest;
|
|
|
+import cn.cslg.pas.domain.business.AssoProductFile;
|
|
|
+import cn.cslg.pas.domain.business.Product;
|
|
|
+import cn.cslg.pas.exception.XiaoShiException;
|
|
|
+import cn.cslg.pas.factorys.businessFactory.Business;
|
|
|
+import cn.cslg.pas.mapper.ProductMapper;
|
|
|
+import cn.cslg.pas.service.common.FileManagerService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 产品的Service层
|
|
|
+ * @Author xiexiang
|
|
|
+ * @Date 2023/10/26
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ProductService extends ServiceImpl<ProductMapper, Product> implements Business {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileManagerService fileManagerService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AssoProductFileService assoProductFileService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object queryMessage(QueryRequest queryRequest) throws Exception {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer addMessage(Object object, List<MultipartFile> files) {
|
|
|
+ ProductDTO productDTO = (ProductDTO) object;
|
|
|
+ //根据名称查询是否重复
|
|
|
+ productDTO.setName(productDTO.getName().trim());
|
|
|
+ String name = productDTO.getName();
|
|
|
+ LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(Product::getName, name);
|
|
|
+ List<Product> products = this.list(queryWrapper);
|
|
|
+ if (products != null && products.size() != 0) {
|
|
|
+ throw new XiaoShiException("参数错误");
|
|
|
+ }
|
|
|
+ //产品入库
|
|
|
+ Product product = new Product();
|
|
|
+ BeanUtils.copyProperties(productDTO, product);
|
|
|
+ product.insert();
|
|
|
+ if(files != null && files.size() != 0) {
|
|
|
+ try {
|
|
|
+ List<String> guids = fileManagerService.uploadFileGetGuid(files);
|
|
|
+ List<AssoProductFile> assoProductFiles = new ArrayList<>();
|
|
|
+ guids.forEach(item -> {
|
|
|
+ AssoProductFile assoProductFile = new AssoProductFile();
|
|
|
+ assoProductFile.setProductId(product.getId());
|
|
|
+ assoProductFile.setFileGuid(item);
|
|
|
+ assoProductFile.setCreateId(1);
|
|
|
+ assoProductFiles.add(assoProductFile);
|
|
|
+ });
|
|
|
+ if (assoProductFiles != null && assoProductFiles.size() != 0) {
|
|
|
+ assoProductFileService.saveBatch(assoProductFiles);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return product.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Object deleteMessage(List<Integer> ids) {
|
|
|
+ //根据产品id删除产品
|
|
|
+ this.removeBatchByIds(ids);
|
|
|
+ //根据产品id删除产品和文件关联
|
|
|
+ LambdaQueryWrapper<AssoProductFile> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.in(AssoProductFile::getProductId, ids);
|
|
|
+ List<AssoProductFile> assoProductFiles = assoProductFileService.list(queryWrapper);
|
|
|
+ List<String> guids = assoProductFiles.stream().map(AssoProductFile::getFileGuid).collect(Collectors.toList());
|
|
|
+ //TODO 根据guid删除文件
|
|
|
+ try {
|
|
|
+ fileManagerService.deleteFileFromFMS(guids);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new XiaoShiException("调用删除接口错误");
|
|
|
+ }
|
|
|
+ //删除产品和文件关联表
|
|
|
+ assoProductFiles.remove(queryWrapper);
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object updateMessage(Object object, List<MultipartFile> files) {
|
|
|
+ //object to productDTO
|
|
|
+ ProductDTO productDTO = (ProductDTO) object;
|
|
|
+ productDTO.setName(productDTO.getName().trim());
|
|
|
+ String name = productDTO.getName();
|
|
|
+ LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(Product::getName, name);
|
|
|
+ List<Product> products = this.list(queryWrapper);
|
|
|
+ if(products == null || products.size() == 0){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Product product = new Product();
|
|
|
+ BeanUtils.copyProperties(productDTO, product);
|
|
|
+ product.insert();
|
|
|
+ if (files != null && files.size() != 0) {
|
|
|
+ try {
|
|
|
+ List<String> guids = fileManagerService.uploadFileGetGuid(files);
|
|
|
+ List<AssoProductFile> assoProductFiles = new ArrayList<>();
|
|
|
+ guids.forEach(item -> {
|
|
|
+ AssoProductFile assoProductFile = new AssoProductFile();
|
|
|
+ assoProductFile.setProductId(product.getId());
|
|
|
+ assoProductFile.setFileGuid(item);
|
|
|
+ assoProductFile.setCreateId(1);
|
|
|
+ assoProductFiles.add(assoProductFile);
|
|
|
+ });
|
|
|
+ if (assoProductFiles != null && assoProductFiles.size() != 0) {
|
|
|
+ assoProductFileService.saveBatch(assoProductFiles);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return product.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GroupVO getGroup(GroupRequest groupRequest) throws Exception {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|