Browse Source

产品添加导出功能

yzc 1 year ago
parent
commit
c96276018f

+ 2 - 2
hx-admin/src/main/resources/application-dev.yml

@@ -44,8 +44,8 @@ mail:
     abroadUrlPrefix:
 
 hx:
-    httpUrl: http://36.134.91.96:10006/test-api/
-    fileServiceUrl: http://192.168.1.13:8090
+  httpUrl: http://36.134.91.96:10006/test-api/
+    fileServiceUrl: http://127.0.0.1:8090
 
 # token配置
 token:

+ 11 - 0
hx-common/src/main/java/com/fjhx/common/controller/FileServiceController.java

@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 /**
  * FTP文件服务控制器
  */
@@ -34,4 +36,13 @@ public class FileServiceController {
     public JSONObject moveFolder(String sourceFolderPath, String targetFolderPath) {
         return ftpFileService.moveFolder(sourceFolderPath, targetFolderPath);
     }
+
+    /**
+     * 获取文件夹中的文件下载地址列表
+     */
+    @PostMapping("/getFileList")
+    List<String> getFileList(String folderPath) {
+        return ftpFileService.getFileList(folderPath);
+    }
+
 }

+ 7 - 0
hx-common/src/main/java/com/fjhx/common/service/file/FtpFileService.java

@@ -2,6 +2,8 @@ package com.fjhx.common.service.file;
 
 import com.alibaba.fastjson2.JSONObject;
 
+import java.util.List;
+
 
 public interface FtpFileService {
     /**
@@ -15,4 +17,9 @@ public interface FtpFileService {
      * @return
      */
     JSONObject moveFolder(String sourceFolderPath, String targetFolderPath);
+
+    /**
+     * 获取文件夹中的文件下载地址列表
+     */
+    List<String> getFileList(String folderPath);
 }

+ 23 - 2
hx-common/src/main/java/com/fjhx/common/service/file/impl/FtpFileServiceImpl.java

@@ -13,18 +13,21 @@ import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 
 @Service
 public class FtpFileServiceImpl implements FtpFileService {
+
+    public static final String fileServiceUrl = SpringUtil.getProperty("hx.fileServiceUrl");
+
     /**
      * 创建临时文件夹
      */
     @Override
     public JSONObject createTempFolder() {
         try {
-            String fileServiceUrl = SpringUtil.getProperty("hx.fileServiceUrl");
             if (ObjectUtil.isEmpty(fileServiceUrl)) {
                 throw new ServiceException("未配置文件服务控制程序!!!");
             }
@@ -55,7 +58,6 @@ public class FtpFileServiceImpl implements FtpFileService {
         targetFolderPath = String.format("/permanent/%s", targetFolderPath);
 
         try {
-            String fileServiceUrl = SpringUtil.getProperty("hx.fileServiceUrl");
             if (ObjectUtil.isEmpty(fileServiceUrl)) {
                 throw new ServiceException("未配置文件服务控制程序!!!");
             }
@@ -77,4 +79,23 @@ public class FtpFileServiceImpl implements FtpFileService {
             throw new ServiceException("操作失败:" + e.getMessage());
         }
     }
+
+    @Override
+    public List<String> getFileList(String folderPath) {
+        return Arrays.asList("ftp不可用,临时提示文字");
+//        try {
+//            HttpPost httpPost = new HttpPost(fileServiceUrl + "/fileService/getFileList");
+//            List<BasicNameValuePair> params = new ArrayList<>();
+//            params.add(new BasicNameValuePair("folderPath", folderPath));
+//            httpPost.setEntity(new UrlEncodedFormEntity(params));
+//            JSONObject responseJson = Utils.getJSON(httpPost);
+//            if (responseJson.getInteger("code") != 200) {
+//                throw new ServiceException(responseJson.getString("msg"));
+//            }
+//            return responseJson.getList("data", String.class);
+//        } catch (IOException e) {
+//            throw new ServiceException("操作失败:" + e.getMessage());
+//        }
+    }
+
 }

+ 9 - 0
hx-item/src/main/java/com/fjhx/item/controller/product/ProductInfoController.java

@@ -9,6 +9,7 @@ import com.ruoyi.common.core.domain.BaseSelectDto;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 import java.util.Map;
 
@@ -125,4 +126,12 @@ public class ProductInfoController {
         return productInfoService.statisticsProduct();
     }
 
+    /**
+     * 导出Excel
+     */
+    @PostMapping("/exportExcel")
+    public void excelExport(HttpServletResponse httpServletResponse, @RequestBody ProductInfoSelectDto dto) {
+        productInfoService.excelExport(httpServletResponse, dto);
+    }
+
 }

+ 42 - 0
hx-item/src/main/java/com/fjhx/item/entity/product/bo/ProductExcelExportBo.java

@@ -0,0 +1,42 @@
+package com.fjhx.item.entity.product.bo;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.annotation.write.style.ColumnWidth;
+import com.alibaba.excel.annotation.write.style.ContentRowHeight;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.net.URL;
+
+@Getter
+@Setter
+@ContentRowHeight(50)
+public class ProductExcelExportBo {
+    @ColumnWidth(10)
+    @ExcelProperty("产品图片")
+    private URL productImg;
+    @ColumnWidth(15)
+    @ExcelProperty("产品编号")
+    private String productCode;
+    @ColumnWidth(15)
+    @ExcelProperty("产品名称")
+    private String productName;
+    @ColumnWidth(15)
+    @ExcelProperty("产品尺寸")
+    private String productSize;
+    @ColumnWidth(15)
+    @ExcelProperty("产品颜色")
+    private String productColor;
+    @ColumnWidth(15)
+    @ExcelProperty("正面纹路")
+    private String frontalTexture;
+    @ColumnWidth(15)
+    @ExcelProperty("反面纹路")
+    private String reverseTexture;
+    @ColumnWidth(15)
+    @ExcelProperty("生产图片")
+    private String prodImgPath;
+    @ColumnWidth(15)
+    @ExcelProperty("生产文件")
+    private String prodFilePath;
+}

+ 6 - 0
hx-item/src/main/java/com/fjhx/item/service/product/ProductInfoService.java

@@ -12,6 +12,7 @@ import com.fjhx.item.enums.ProductAvailableRecordType;
 import com.ruoyi.common.core.service.BaseService;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 import java.util.Map;
 
@@ -113,4 +114,9 @@ public interface ProductInfoService extends BaseService<ProductInfo> {
      * 产品分析-数据看板
      */
     Page<ProductAnalysisBo> getProductAnalysisPage(Page<Object> page, IWrapper<ProductInfo> wrapper);
+
+    /**
+     * 导出Excel
+     */
+    void excelExport(HttpServletResponse httpServletResponse, ProductInfoSelectDto dto);
 }

+ 81 - 0
hx-item/src/main/java/com/fjhx/item/service/product/impl/ProductInfoServiceImpl.java

@@ -14,8 +14,11 @@ import com.fjhx.common.constant.SourceConstant;
 import com.fjhx.common.entity.InOutBo;
 import com.fjhx.common.enums.InOutType;
 import com.fjhx.common.service.file.FtpFileService;
+import com.fjhx.common.service.file.impl.FtpFileServiceImpl;
+import com.fjhx.file.entity.FileInfoVo;
 import com.fjhx.file.utils.ObsFileUtil;
 import com.fjhx.item.entity.product.bo.ProductAnalysisBo;
+import com.fjhx.item.entity.product.bo.ProductExcelExportBo;
 import com.fjhx.item.entity.product.dto.ProductInfoDto;
 import com.fjhx.item.entity.product.dto.ProductInfoSelectDto;
 import com.fjhx.item.entity.product.po.ProductAvailableRecord;
@@ -30,9 +33,11 @@ import com.fjhx.item.service.product.ProductClassifyService;
 import com.fjhx.item.service.product.ProductInfoService;
 import com.fjhx.item.service.product.ProductStockInfoService;
 import com.fjhx.item.util.CodeEnum;
+import com.fjhx.item.util.excel.util.ExcelUtil;
 import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
 import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
 import com.fjhx.tenant.service.dict.DictTenantDataService;
+import com.fjhx.tenant.utils.DictUtils;
 import com.ruoyi.common.core.domain.BaseIdPo;
 import com.ruoyi.common.core.domain.BasePo;
 import com.ruoyi.common.core.domain.entity.SysDept;
@@ -46,8 +51,11 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.math.BigDecimal;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -750,4 +758,77 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
         return baseMapper.getProductAnalysisPage(page, wrapper);
     }
 
+    @Override
+    public void excelExport(HttpServletResponse httpServletResponse, ProductInfoSelectDto dto) {
+        IWrapper<ProductInfo> wrapper = IWrapper.getWrapper();
+        //权限过滤:产品-子公司看自己的产品,总公司看全部,物料没有限制
+        Long companyId = SecurityUtils.getCompanyId();
+        if (!Objects.equals(companyId, 100L)) {
+            dto.setCompanyId(companyId);
+        }
+        wrapper.and(q1 -> q1.
+                and(q -> q.eq(ProductInfo::getDefinition, 1).eq(ProductInfo::getCompanyId, dto.getCompanyId()))
+                .or().eq(ProductInfo::getDefinition, 2)
+        );
+
+        List<ProductInfo> productInfoList = this.list(wrapper);
+
+        List<Long> pIds = productInfoList.stream().map(ProductInfo::getId).collect(Collectors.toList());
+
+        //处理产品图片
+        Map<Long, List<FileInfoVo>> fileMap = ObsFileUtil.getFileMap(pIds);
+
+        List<ProductExcelExportBo> productExcelExportBoList = new ArrayList<>();
+
+        Map<String, String> frontLinesMap = DictUtils.getDictMap("front_lines");
+        Map<String, String> backLinesMap = DictUtils.getDictMap("back_lines");
+
+        for (ProductInfo productInfo : productInfoList) {
+            //处理长宽高
+            BigDecimal length = productInfo.getLength();
+            BigDecimal width = productInfo.getWidth();
+            BigDecimal height = productInfo.getHeight();
+            length = ObjectUtil.isEmpty(length) ? BigDecimal.ZERO : length;
+            width = ObjectUtil.isEmpty(width) ? BigDecimal.ZERO : width;
+            height = ObjectUtil.isEmpty(height) ? BigDecimal.ZERO : height;
+
+            //获取产品图片信息
+            String fileUrl = "";
+            List<FileInfoVo> fileInfoVoList = fileMap.get(productInfo.getId());
+            if (ObjectUtil.isNotEmpty(fileInfoVoList)) {
+                fileUrl = fileInfoVoList.get(0).getFileUrl();
+            }
+
+            ProductExcelExportBo bo = new ProductExcelExportBo();
+            try {
+                bo.setProductImg(new URL(fileUrl));
+            } catch (MalformedURLException e) {
+                throw new RuntimeException(e);
+            }
+            bo.setProductCode(productInfo.getCustomCode());
+            bo.setProductName(productInfo.getName());
+            bo.setProductSize(String.format("%s * %s * %s", length, width, height));
+            bo.setProductColor(productInfo.getColor());
+
+
+            bo.setFrontalTexture(frontLinesMap.getOrDefault(productInfo.getFrontalTexture(), productInfo.getFrontalTexture()));
+            bo.setReverseTexture(backLinesMap.getOrDefault(productInfo.getReverseTexture(), productInfo.getReverseTexture()));
+
+            //赋值生产图片
+            List<String> prodImgList = ftpFileService.getFileList(productInfo.getProdImgPath());
+            if (ObjectUtil.isNotEmpty(prodImgList)) {
+                bo.setProdImgPath(FtpFileServiceImpl.fileServiceUrl + prodImgList.get(0));
+            }
+            //赋值生产文件
+            List<String> prodFileList = ftpFileService.getFileList(productInfo.getProdFilePath());
+            if (ObjectUtil.isNotEmpty(prodFileList)) {
+                bo.setProdFilePath(FtpFileServiceImpl.fileServiceUrl + prodFileList.get(0));
+            }
+
+            productExcelExportBoList.add(bo);
+        }
+
+        ExcelUtil.export(httpServletResponse, productExcelExportBoList, ProductExcelExportBo.class);
+    }
+
 }