This commit is contained in:
parent
96bb9c2bcd
commit
a7c4b93e7c
7
pom.xml
7
pom.xml
@ -110,10 +110,11 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>4.0.3</version>
|
||||
<groupId>cn.idev.excel</groupId>
|
||||
<artifactId>fastexcel</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
@ -10,6 +10,7 @@ import com.google.common.cache.LoadingCache;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -22,6 +23,7 @@ import tech.riemann.ims.entity.material.Material;
|
||||
import tech.riemann.ims.service.material.IMaterialService;
|
||||
import tech.riemann.ims.service.material.ITypeService;
|
||||
import tech.riemann.ims.utils.BarcodeUtil;
|
||||
import tech.riemann.ims.utils.ExcelUtil;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
@ -78,6 +80,24 @@ public class MaterialController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("material/download-excel")
|
||||
@Operation(summary = "导出物料列表")
|
||||
public void downloadExcel(@Parameter(description = "类型") @RequestParam(required = false, defaultValue = "") Integer type,
|
||||
@Parameter(description = "搜索关键词") @RequestParam(required = false, defaultValue = "") String key,
|
||||
HttpServletResponse response) {
|
||||
boolean hasLike = Strings.isNotBlank(key);
|
||||
key = String.format("%%%s%%", key);
|
||||
List<Material> result = materialService.list(Wrappers.<Material>lambdaQuery()
|
||||
.like(hasLike, Material::getName, key)
|
||||
.or()
|
||||
.like(hasLike, Material::getCode, key)
|
||||
.or()
|
||||
.like(hasLike, Material::getType, key)
|
||||
.orderByDesc(Material::getUpdatedTime));
|
||||
result.forEach(item -> item.setTypeName(typeService.getTypeName(item.getType())));
|
||||
ExcelUtil.exportExcel(response, "物料列表-" + System.currentTimeMillis(), "库存", Material.class, result);
|
||||
}
|
||||
|
||||
@PostMapping("material/list")
|
||||
@Operation(summary = "查询所有物料列表")
|
||||
public List<Material> all(@Parameter(description = "类型") @RequestParam(required = false, defaultValue = "") String type,
|
||||
|
@ -1,5 +1,6 @@
|
||||
package tech.riemann.ims.entity;
|
||||
|
||||
import cn.idev.excel.annotation.ExcelIgnore;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
@ -41,18 +42,21 @@ public class IdBaseEntity extends Entity {
|
||||
|
||||
@Id
|
||||
@TableId(type = IdType.AUTO)
|
||||
@ExcelIgnore
|
||||
private Long id;
|
||||
|
||||
@Column("created_time")
|
||||
@Comment("创建时间")
|
||||
@TableField("created_time")
|
||||
@Default
|
||||
@ExcelIgnore
|
||||
protected LocalDateTime createdTime = LocalDateTime.now();
|
||||
|
||||
@Column("updated_time")
|
||||
@TableField("updated_time")
|
||||
@Comment("最后更新时间")
|
||||
@Default
|
||||
@ExcelIgnore
|
||||
protected LocalDateTime updatedTime = LocalDateTime.now();
|
||||
|
||||
@Schema(description = "创建人")
|
||||
@ -60,6 +64,7 @@ public class IdBaseEntity extends Entity {
|
||||
@TableField("created_by")
|
||||
@Comment("创建人")
|
||||
@JsonIgnore
|
||||
@ExcelIgnore
|
||||
protected String createdBy;
|
||||
|
||||
@Schema(description = "更新人")
|
||||
@ -67,6 +72,7 @@ public class IdBaseEntity extends Entity {
|
||||
@TableField("updated_by")
|
||||
@Comment("更新人")
|
||||
@JsonIgnore
|
||||
@ExcelIgnore
|
||||
protected String updatedBy;
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
package tech.riemann.ims.entity.material;
|
||||
|
||||
import cn.idev.excel.annotation.ExcelIgnore;
|
||||
import cn.idev.excel.annotation.ExcelProperty;
|
||||
import cn.idev.excel.annotation.write.style.ColumnWidth;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@ -37,6 +40,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_code")
|
||||
@Comment("编码")
|
||||
@ColDefine(notNull = false, width = 128, precision = 0)
|
||||
@ExcelProperty("物料编码")
|
||||
@ColumnWidth(40)
|
||||
private String code;
|
||||
|
||||
@Schema(description = "名称", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -44,6 +49,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_name")
|
||||
@Comment("名称")
|
||||
@ColDefine(notNull = false, width = 128, precision = 0)
|
||||
@ExcelProperty("物料名称")
|
||||
@ColumnWidth(60)
|
||||
private String name;
|
||||
|
||||
@Schema(description = "是否赋码", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -51,6 +58,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_assign_rule")
|
||||
@Comment("赋码规则")
|
||||
@ColDefine(notNull = false, type = ColType.INT)
|
||||
@ExcelProperty("是否赋码")
|
||||
@ColumnWidth(40)
|
||||
private Boolean assignRule; // 0-低值易耗品(不参与赋码) 1-高价值工具类(参与唯一赋码)
|
||||
|
||||
@Schema(description = "类型", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -58,9 +67,12 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_type")
|
||||
@Comment("类型")
|
||||
@ColDefine(notNull = false, width = 128, precision = 0)
|
||||
@ExcelIgnore
|
||||
private String type;
|
||||
|
||||
@TableField(exist = false)
|
||||
@ExcelProperty("物料类型")
|
||||
@ColumnWidth(40)
|
||||
private String typeName;
|
||||
|
||||
@Schema(description = "价格", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -68,6 +80,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_price")
|
||||
@Comment("价格")
|
||||
@ColDefine(notNull = false, width = 18, precision = 0)
|
||||
@ExcelProperty("价格")
|
||||
@ColumnWidth(40)
|
||||
private BigDecimal price;
|
||||
|
||||
@Schema(description = "规格", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -75,6 +89,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_spec")
|
||||
@Comment("规格")
|
||||
@ColDefine(notNull = false, width = 128, precision = 0)
|
||||
@ExcelProperty("物料型号")
|
||||
@ColumnWidth(40)
|
||||
private String spec;
|
||||
|
||||
|
||||
@ -83,6 +99,8 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_stock")
|
||||
@Comment("库存数量")
|
||||
@ColDefine(notNull = false, type = ColType.INT)
|
||||
@ExcelProperty("库存数量")
|
||||
@ColumnWidth(40)
|
||||
private Integer stock;
|
||||
|
||||
@Schema(description = "备注", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
@ -90,6 +108,7 @@ public class Material extends IdBaseEntity {
|
||||
@Column("m_description")
|
||||
@Comment("备注")
|
||||
@ColDefine(notNull = false, width = 500, precision = 0)
|
||||
@ExcelIgnore
|
||||
private String description;
|
||||
|
||||
|
||||
|
38
src/main/java/tech/riemann/ims/utils/ExcelUtil.java
Normal file
38
src/main/java/tech/riemann/ims/utils/ExcelUtil.java
Normal file
@ -0,0 +1,38 @@
|
||||
package tech.riemann.ims.utils;
|
||||
|
||||
import club.zhcs.lina.starter.exception.BizException;
|
||||
import cn.idev.excel.FastExcel;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author mayong
|
||||
* @since 2025/3/15 16:17
|
||||
*/
|
||||
@UtilityClass
|
||||
public class ExcelUtil {
|
||||
|
||||
|
||||
public void exportExcel(HttpServletResponse response,
|
||||
String excelName,
|
||||
String sheetName,
|
||||
Class<?> clazz,
|
||||
Collection<?> result) {
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode(excelName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
try {
|
||||
FastExcel.write(response.getOutputStream(), clazz)
|
||||
.sheet(sheetName)
|
||||
.doWrite(result);
|
||||
} catch (IOException e) {
|
||||
throw BizException.create("导出Excel失败");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user