板材工序接口修改

This commit is contained in:
lym
2024-04-19 14:08:50 +08:00
parent 5ff48fa2ed
commit 3af6472ada
57 changed files with 969 additions and 1577 deletions
@@ -1,6 +1,7 @@
package com.cf.imes.module.manage.controller.admin.plate;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.*;
import com.cf.imes.module.manage.service.plate.PlateExcelUtil;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
@@ -9,7 +10,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
@@ -98,16 +98,17 @@ public class PlateController {
@GetMapping("/get-import-template")
@Operation(summary = "获得导入用户模板")
@PreAuthorize("@ss.hasPermission('manage:plate:export')")
public void importTemplate(HttpServletResponse response) throws IOException {
// 手动创建导出 demo
List<PlateImportExcelVO> list = Arrays.asList(
PlateImportExcelVO.builder().goodsId("SP1123456").goodsName("xixih").material("颗粒板").width(BigDecimal.valueOf(1212.3)).height(BigDecimal.valueOf(12131.6))
.thickness(BigDecimal.valueOf(0.2)).brand("SexEnum.MALE.getSex()").spec("大厂").remark("测试").build(),//.price(33333.2)
PlateImportExcelVO.builder().goodsId("SP1123457").goodsName("2L").material("yuanma@cf.com").width(BigDecimal.valueOf(21.6)).height(BigDecimal.valueOf(123231.6))
.thickness(BigDecimal.valueOf(5.3)).brand("kkkkkk").spec("大厂").remark("测试").build()//.price(333.555)
PlateImportExcelVO.builder().goodsId("SP1123456").goodsName("xixih").material("颗粒板").width(String.valueOf(1212.3)).height(String.valueOf(12131.6))
.thickness(String.valueOf(0.2)).brand("鹅厂").spec("大厂").remark("测试").price(String.valueOf(33333.2)).build(),
PlateImportExcelVO.builder().goodsId("SP1123457").goodsName("2L").material("yuanma@cf.com").width(String.valueOf(21.6)).height(String.valueOf(12231.6))
.thickness(String.valueOf(5.3)).brand("kkkkkk").spec("大厂").remark("测试").price(String.valueOf(333.555)).build()
);
// 输出
ExcelUtils.write(response, "板材导入模板.xls", "板材列表", PlateImportExcelVO.class, list);
ExcelUtils.write(response, "板材导入模板.xlsx", "板材列表", PlateImportExcelVO.class, list);
}
@PostMapping("/import")
@@ -118,11 +119,20 @@ public class PlateController {
@Parameter(name = "organId", description = "板材所属组织ID")
})
@PreAuthorize("@ss.hasPermission('manage:plate:import')")
public CommonResult<PlateImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
public void importExcel(@RequestParam("file") MultipartFile file,
@RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
@RequestParam(value = "organId") Long organId) throws Exception {
List<PlateImportExcelVO> list = ExcelUtils.read(file, PlateImportExcelVO.class);
return success(plateService.importPlateList(list, updateSupport , organId));
@RequestParam(value = "organId") Long organId,
HttpServletResponse response) throws Exception {
PlateExcelUtil plateExcelUtil = new PlateExcelUtil();
List<PlateImportExcelVO> list = plateExcelUtil.read(file, PlateImportExcelVO.class);
// 判断文件是否导入成功
if (!plateExcelUtil.getFlag())
ExcelUtils.write(response, "板材导入模板.xlsx", "板材列表", PlateImportExcelVO.class, list);
else
plateService.importPlateList(list, updateSupport, organId);// 成功,批量导入
plateExcelUtil.clear();
}
}
@@ -1,18 +1,14 @@
package com.cf.imes.module.manage.controller.admin.plate.vo.plate;
import com.cf.imes.framework.excel.core.annotations.DictFormat;
import com.cf.imes.framework.excel.core.convert.DictConvert;
import com.cf.imes.module.system.enums.DictTypeConstants;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import javax.validation.constraints.NotEmpty;
/**
* 用户 Excel 导入 VO
@@ -25,36 +21,80 @@ import java.time.LocalDateTime;
public class PlateImportExcelVO {
@ExcelProperty("商品编号")
@NotEmpty(message = "商品编号不能为空")
private String goodsId;
@ExcelProperty("商品名称")
@NotEmpty(message = "商品名称不能为空")
private String goodsName;
@ExcelProperty("材质")
@NotEmpty(message = "材质不能为空")
private String material;
@ExcelProperty("颜色")
@NotEmpty(message = "颜色不能为空")
private String color;
@ExcelProperty("宽度")
private BigDecimal width;
@NotEmpty(message = "宽度不能为空")
private String width;
@ExcelProperty("高度")
private BigDecimal height;
@ExcelProperty("高度")// float(8,3)
@NotEmpty(message = "高度不能为空")
private String height;
@ExcelProperty("厚度")
private BigDecimal thickness;
@NotEmpty(message = "厚度不能为空")
private String thickness;
@ExcelProperty("价格")
private Double price;
@NotEmpty(message = "价格不能为空")
private String price;
@ExcelProperty("品牌")
@NotEmpty(message = "品牌不能为空")
private String brand;
@ExcelProperty("规格")
@NotEmpty(message = "规格不能为空")
private String spec;
@ExcelProperty("备注")
private String remark;
@ExcelProperty("上传结果")
private String result;
public boolean isValidGoods() {
return !(goodsId == null || goodsId.isEmpty() || goodsName == null || goodsName.isEmpty() || material == null ||
material.isEmpty() || color == null || color.isEmpty() || width == null ||width.isEmpty() || height == null ||
height.isEmpty() || height == null ||thickness.isEmpty() || brand == null || brand.isEmpty() || spec == null || spec.isEmpty());
}
// 数据长度判断
public boolean checkPrecision(Double number) {
if (number == null) {
return false;
}
// 将浮点数转换成字符串
String numberStr = Double.toString(number);
// 分割整数部分和小数部分
String[] parts = numberStr.split("\\.");
String integerPart = parts[0];
String decimalPart = parts.length > 1 ? parts[1] : "";
// 检查整数部分位数
if (integerPart.length() > 5) {
return false;
}
// 检查小数部分位数
if (decimalPart.length() > 3) {
return false;
}
return true;
}
}
@@ -2,8 +2,6 @@ package com.cf.imes.module.manage.controller.admin.plate.vo.plate;
import lombok.*;
import java.math.BigDecimal;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import com.cf.imes.framework.common.pojo.PageParam;
import org.springframework.format.annotation.DateTimeFormat;
@@ -30,13 +28,13 @@ public class PlatePageReqVO extends PageParam {
private String color;
@Schema(description = "宽度")
private BigDecimal width;
private Double width;
@Schema(description = "高度")
private BigDecimal height;
private Double height;
@Schema(description = "厚度")
private BigDecimal thickness;
private Double thickness;
@Schema(description = "价格", example = "3380")
private Double price;
@@ -3,10 +3,6 @@ package com.cf.imes.module.manage.controller.admin.plate.vo.plate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;
@@ -16,11 +12,10 @@ import com.alibaba.excel.annotation.*;
public class PlateRespVO {
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@ExcelProperty("主键")
private Long id;
@Schema(description = "客户的商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@ExcelProperty("客户的商品编号")
@ExcelProperty("商品编号")
private String goodsId;
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "赵六")
@@ -28,7 +23,7 @@ public class PlateRespVO {
private String goodsName;
@Schema(description = "材质:颗粒板、欧松板、多层板、生态板、禾香板、密度板、实木、铝蜂窝板、铝塑板", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("材质:颗粒板、欧松板、多层板、生态板、禾香板、密度板、实木、铝蜂窝板、铝塑板")
@ExcelProperty("材质")
private String material;
@Schema(description = "颜色", requiredMode = Schema.RequiredMode.REQUIRED)
@@ -37,15 +32,15 @@ public class PlateRespVO {
@Schema(description = "宽度", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("宽度")
private BigDecimal width;
private Double width;
@Schema(description = "高度", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("高度")
private BigDecimal height;
private Double height;
@Schema(description = "厚度", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("厚度")
private BigDecimal thickness;
private Double thickness;
@Schema(description = "价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "3380")
@ExcelProperty("价格")
@@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
@Schema(description = "管理后台 - 板材信息表 plate_{N}新增/修改 Request VO")
@Data
@@ -31,15 +30,15 @@ public class PlateSaveReqVO {
@Schema(description = "宽度", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "宽度不能为空")
private BigDecimal width;
private Double width;
@Schema(description = "高度", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "高度不能为空")
private BigDecimal height;
private Double height;
@Schema(description = "厚度", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "厚度不能为空")
private BigDecimal thickness;
private Double thickness;
@Schema(description = "价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "3380")
@NotNull(message = "价格不能为空")
@@ -2,10 +2,6 @@ package com.cf.imes.module.manage.dal.dataobject.plate;
import lombok.*;
import java.math.BigDecimal;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.*;
import com.cf.imes.framework.mybatis.core.dataobject.BaseDO;
@@ -48,15 +44,15 @@ public class PlateDO extends BaseDO {
/**
* 宽度
*/
private BigDecimal width;
private Double width;
/**
* 高度
*/
private BigDecimal height;
private Double height;
/**
* 厚度
*/
private BigDecimal thickness;
private Double thickness;
/**
* 价格
*/
@@ -6,6 +6,7 @@ import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlatePageReqVO;
import com.cf.imes.module.manage.dal.dataobject.plate.PlateDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 板材信息表 plate_{N} Mapper
@@ -37,4 +38,5 @@ public interface PlateMapper extends BaseMapperX<PlateDO> {
.orderByDesc(PlateDO::getId));
}
}
PlateDO selectOneById(@Param("id") Long id, @Param("organId") Long organId);
}
@@ -0,0 +1,102 @@
package com.cf.imes.module.manage.service.plate;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
@Slf4j
public final class PlateExcelListener<T> extends AnalysisEventListener<PlateImportExcelVO> {
/**
* 自定义用于暂时存储data
* 可以通过实例获取该值
*/
private List<PlateImportExcelVO> datas = new ArrayList<>();
private boolean flag = true;
private Map<Integer, String> valueMap = new HashMap<>();
/**
* 每解析一行都会回调invoke()方法
*
* @param data 读取后的数据对象
* @param context 内容
*/
@Override
public void invoke(PlateImportExcelVO data, AnalysisContext context) {
if (!data.isValidGoods()) {//返回false,表示商品信息无效;否则返回true,表示商品信息有效
data.setResult("商品关键信息缺少");
this.flag = false;
}
checkData(data.getHeight(),"高度",data);
checkData(data.getWidth(),"宽度",data);
checkData(data.getThickness(),"厚度",data);
checkData(data.getPrice(),"价格",data);
if (data.getResult() != null)
data.setResult(data.getResult().replace("null,", ""));
valueMap.put(context.getCurrentRowNum(), data.getResult());
datas.add(data);
}
// 错误判断
private void checkData(String valueCheck, String head, PlateImportExcelVO data) {
// 长宽厚、价格判断非空
if (valueCheck == null || valueCheck.equals("")) {
this.flag = false;
data.setResult(data.getResult() + "," + head + "信息缺少");
} else {
try {
if (!data.checkPrecision(Double.valueOf(valueCheck))) {
data.setResult(data.getResult() + "," + head + "数据有误");
this.flag = false;
}
} catch (NumberFormatException e) {
this.flag = false;
data.setResult(data.getResult() + "," + head + "需要填写数字");
}
}
}
/**
* 读取完后操作
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
if (this.flag)
log.info("PlateExcelListener 所有数据读取完成");
}
/**
* 返回数据
*
* @return 返回读取的数据集合
**/
public List<PlateImportExcelVO> getDatas() {
return datas;
}
/**
* 返回读取结果
*
* @return 是否读取成功
**/
public boolean getFlag() {
return flag;
}
/**
* 返回错误信息
*
* @return String
**/
public Map<Integer, String> getValueMap() {
return valueMap;
}
}
@@ -0,0 +1,64 @@
package com.cf.imes.module.manage.service.plate;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Slf4j
public class PlateExcelUtil {
private String value;
private PlateExcelListener<PlateImportExcelVO> excelListener = new PlateExcelListener<>();
public List<PlateImportExcelVO> read(MultipartFile file, Class<PlateImportExcelVO> head) throws IOException {
if (file.isEmpty()) {
value = "文件为空";
throw new IOException("文件为空");
} else if (!(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls"))) {
value = "文件格式不正确";
throw new IOException("文件格式不正确");
} else if (file.getSize() > 1024 * 1024 * 10) {
value = "文件大小超过 10M";
throw new IOException("文件大小超过 10M");
} else if (file.getSize() == 0) {
value = "文件大小为 0";
throw new IOException("文件大小为 0");
} else {
EasyExcel.read(file.getInputStream(), PlateImportExcelVO.class, excelListener)
.autoCloseStream(true) // 不要自动关闭,交给 Servlet 自己处理
.headRowNumber(1).sheet(0).doRead();
//获取读取的数据
List<PlateImportExcelVO> list = excelListener.getDatas();
if (excelListener.getValueMap().size() > 0)
excelListener.getValueMap().forEach((k, v) -> {
list.get(k - 1).setResult(v);
});
return list;
}
}
public boolean getFlag() {
return excelListener.getFlag();
}
public void clear() {
excelListener.getDatas().clear();
}
public Map<Integer, String> getValueMap() {
return excelListener.getValueMap();
}
}
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportExcelVO;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlateImportRespVO;
import com.cf.imes.module.manage.controller.admin.plate.vo.plate.PlatePageReqVO;
@@ -17,7 +18,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import com.cf.imes.module.manage.dal.dataobject.plate.PlateDO;
import com.cf.imes.framework.datapermission.core.util.DataPermissionUtils;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
@@ -51,7 +51,7 @@ public class PlateServiceImpl implements PlateService {
validateGoodExists(createReqVO.getGoodsId(), createReqVO.getOrganId());
plate.setOrganId(createReqVO.getOrganId());
}
plateMapper.insert(plate);
plateMapper.insert(plate.setOrganId(OrganContextHolder.getOrganId()));// 组织id未传输
// 返回
return plate.getId();
}
@@ -90,13 +90,6 @@ public class PlateServiceImpl implements PlateService {
}
private void validatePlateForCreateOrUpdate(PlateImportExcelVO importPlate) {
// 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确
DataPermissionUtils.executeIgnore(() -> {
});
}
@Override
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
public PlateImportRespVO importPlateList(List<PlateImportExcelVO> importPlates, boolean isUpdateSupport , Long organId) {
@@ -106,19 +99,17 @@ public class PlateServiceImpl implements PlateService {
validateOrganExists(organId);
PlateImportRespVO respVO = PlateImportRespVO.builder().createPlateNames(new ArrayList<>())
.updatePlateNames(new ArrayList<>()).failurePlateNames(new LinkedHashMap<>()).build();
// 批量插入集合
List<PlateDO> insertList = new ArrayList<>();
// 批量更新集合
List<PlateDO> updateList = new ArrayList<>();
importPlates.forEach(importPlate -> {
// 校验,判断是否有不符合的原因 校验全部是否为空
try {
validatePlateForCreateOrUpdate(importPlate);
} catch (ServiceException ex) {
respVO.getFailurePlateNames().put(importPlate.getGoodsName() , ex.getMessage());
return;
}
// 判断如果不存在,在进行插入
PlateDO existPlate = plateMapper.selectByGoodID(importPlate.getGoodsId(), organId);
if (existPlate == null) {
plateMapper.insert(BeanUtils.toBean(importPlate, PlateDO.class));
respVO.getCreatePlateNames().add(importPlate.getGoodsName());
insertList.add(BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(organId));
// plateMapper.insert(BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(organId));
// respVO.getCreatePlateNames().add(importPlate.getGoodsName());
return;
}
// 如果存在,判断是否允许更新
@@ -126,11 +117,18 @@ public class PlateServiceImpl implements PlateService {
respVO.getFailurePlateNames().put(importPlate.getGoodsName(), ErrorCodeConstants.PLATE_EXISTS.getMsg());
return;
}
PlateDO plateDO = BeanUtils.toBean(importPlate, PlateDO.class);
plateDO.setId(existPlate.getId());
plateMapper.updateById(plateDO);
respVO.getUpdatePlateNames().add(importPlate.getGoodsName());
PlateDO plateDO = BeanUtils.toBean(importPlate, PlateDO.class).setOrganId(organId).setId(existPlate.getId());
updateList.add(plateDO);
// plateMapper.updateById(plateDO);
// respVO.getUpdatePlateNames().add(importPlate.getGoodsName());
});
// 批量插入,批量更新
if (insertList.size() > 0 && insertList != null) {
plateMapper.insertBatch(insertList);
}
if (updateList.size() > 0 && updateList != null) {
plateMapper.updateBatch(updateList);
}
return respVO;
}
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cf.imes.module.manage.dal.mysql.plate.PlateMapper">
<select id="selectOneById" resultType="com.cf.imes.module.manage.dal.dataobject.plate.PlateDO">
SELECT
id,
goods_id,
organ_id,
goods_name,
material,
color,
width,
height,
thickness,
brand,
spec,
remark,
creator,
create_time,
updater,
update_time,
deleted,
price
FROM
plate
where organ_id = #{organId} and id = #{id} and deleted = 0
</select>
</mapper>