给商品导出初步增加带模板导出的逻辑

This commit is contained in:
季圣华
2023-04-17 23:40:42 +08:00
parent b9cb19fab6
commit ab2ff3ab41
5 changed files with 114 additions and 34 deletions

View File

@@ -1,11 +1,10 @@
package com.jsh.erp.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.entities.MaterialInitialStockWithMaterial;
import com.jsh.erp.datasource.entities.MaterialVo4Unit;
import com.jsh.erp.datasource.entities.Unit;
import com.jsh.erp.datasource.vo.MaterialVoSearch;
import com.jsh.erp.service.depot.DepotService;
import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.material.MaterialService;
@@ -16,6 +15,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -23,6 +23,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
@@ -55,6 +56,15 @@ public class MaterialController {
@Resource
private RedisService redisService;
@Value("${server.tomcat.basedir}")
private String basedir;
@Value("${template.path}")
private String templatePath;
@Value("${export.mode}")
private Integer exportMode;
/**
* 检查商品是否存在
* @param id
@@ -360,35 +370,47 @@ public class MaterialController {
List<MaterialVo4Unit> dataList = materialService.exportExcel(StringUtil.toNull(materialParam), StringUtil.toNull(color),
StringUtil.toNull(weight), StringUtil.toNull(expiryNum), StringUtil.toNull(enabled), StringUtil.toNull(enableSerialNumber),
StringUtil.toNull(enableBatchNumber), StringUtil.toNull(remark), StringUtil.toNull(categoryId));
String[] names = {"条码", "名称", "规格", "型号", "颜色", "类别", "扩展信息", "单位", "基础重量", "保质期", "采购价", "零售价", "销售价", "最低售价", "备注", "状态", "序列号", "批号"};
String title = "商品信息";
List<String[]> objects = new ArrayList<>();
if (null != dataList) {
for (MaterialVo4Unit m : dataList) {
String[] objs = new String[100];
objs[0] = m.getmBarCode();
objs[1] = m.getName();
objs[2] = m.getStandard();
objs[3] = m.getModel();
objs[4] = m.getColor();
objs[5] = m.getCategoryName();
objs[6] = materialService.getMaterialOtherByParam(mpArr, m);
objs[7] = m.getCommodityUnit();
objs[8] = m.getWeight() == null? "" : m.getWeight().toString();
objs[9] = m.getExpiryNum() == null? "" : m.getExpiryNum().toString();
objs[10] = m.getPurchaseDecimal() == null? "" : m.getPurchaseDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
objs[11] = m.getCommodityDecimal() == null? "" : m.getCommodityDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
objs[12] = m.getWholesaleDecimal() == null? "" : m.getWholesaleDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
objs[13] = m.getLowDecimal() == null? "" : m.getLowDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
objs[14] = m.getRemark();
objs[15] = m.getEnabled() ? "启用" : "禁用";
objs[16] = "1".equals(m.getEnableSerialNumber()) ? "" : "";
objs[17] = "1".equals(m.getEnableBatchNumber()) ? "" : "";
objects.add(objs);
if(exportMode == 0) {
String[] names = {"条码", "名称", "规格", "型号", "颜色", "类别", "扩展信息", "单位", "基础重量", "保质期", "采购价", "零售价", "销售价", "最低售价", "备注", "状态", "序列号", "批号"};
String title = "商品信息";
List<String[]> objects = new ArrayList<>();
if (null != dataList) {
for (MaterialVo4Unit m : dataList) {
String[] objs = new String[100];
objs[0] = m.getmBarCode();
objs[1] = m.getName();
objs[2] = m.getStandard();
objs[3] = m.getModel();
objs[4] = m.getColor();
objs[5] = m.getCategoryName();
objs[6] = materialService.getMaterialOtherByParam(mpArr, m);
objs[7] = m.getCommodityUnit();
objs[8] = m.getWeight() == null ? "" : m.getWeight().toString();
objs[9] = m.getExpiryNum() == null ? "" : m.getExpiryNum().toString();
objs[10] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[11] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[12] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[13] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[14] = m.getRemark();
objs[15] = m.getEnabled() ? "启用" : "禁用";
objs[16] = "1".equals(m.getEnableSerialNumber()) ? "" : "";
objs[17] = "1".equals(m.getEnableBatchNumber()) ? "" : "";
objects.add(objs);
}
}
File file = ExcelUtils.exportObjectsWithoutTitle(title, names, title, objects);
ExportExecUtil.showExec(file, file.getName(), response);
} else {
List<Map<Integer, String>> list = new ArrayList<>();
Map<Integer, String> map = new HashMap<>();
list.add(map);
String fileName = "商品信息.xls";
String pathFile = basedir + File.separator + fileName;
InputStream path = ExcelUtils.getPathByFileName(templatePath, "goods_template_export.xls");
EasyExcel.write(pathFile).withTemplate(path).sheet(0).needHead(Boolean.FALSE).doWrite(list);
File file = new File(basedir, fileName);
ExportExecUtil.showExec(file, fileName, response);
}
File file = ExcelUtils.exportObjectsWithoutTitle(title, names, title, objects);
ExportExecUtil.showExec(file, file.getName(), response);
} catch (Exception e) {
e.printStackTrace();
}