Commit ef3fe76a by zhangtw

入库导入添加校验

parent 583b1606
......@@ -2,10 +2,8 @@ package com.ruoyi.web.controller.inventory;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
......@@ -73,21 +71,24 @@ public class InboundOrdersController extends BaseController
@PostMapping("/export")
public void export(HttpServletResponse response, InboundOrders inboundOrders)
{
List<InboundOrders> list = inboundOrdersService.selectInboundOrdersList(inboundOrders);
// for (InboundOrders inboundItem : list) {
// if (inboundOrders.getOrderStatus() == 1) {
// InboundItemsTO inboundItemsTO = new InboundItemsTO();
// inboundItemsTO.setRelocationId(inboundItem.getL)
// }else if (inboundOrders.getOrderStatus() == 2) {
//
// }else if (inboundOrders.getOrderStatus() == 3) {
//
// }else{
//
// }
// }
List<InboundItemsTO> list = inboundOrdersService.selectInboundOrdersAndItems(inboundOrders);
for (InboundItemsTO inboundItem : list) {
if (inboundItem != null) {
inboundItem.setPackageWeight(inboundItem.getWeight() * (inboundItem.getActualQuantity() / inboundItem.getActualPackages()));
inboundItem.setUnitWeight(inboundItem.getWeight() * (inboundItem.getActualQuantity() / inboundItem.getActualPackages()));
if (inboundItem.getOrderStatus() == 1) {
inboundItem.setRelocation(inboundItem.getRemark());
}else if (inboundItem.getOrderStatus() == 2) {
inboundItem.setFinishedLocation(inboundItem.getRemark());
}else if (inboundItem.getOrderStatus() == 3) {
inboundItem.setRemarkTrdc(inboundItem.getRemark());
inboundItem.setFinishedSystemNo(inboundItem.getSystemNo());
inboundItem.setFinishedOrderId(inboundItem.getOrderId());
}
}
}
ExcelUtil<InboundOrders> util = new ExcelUtil<InboundOrders>(InboundOrders.class);
ExcelUtil<InboundItemsTO> util = new ExcelUtil<InboundItemsTO>(InboundItemsTO.class);
util.exportExcel(response, list, "入库单导出数据");
}
......@@ -180,7 +181,7 @@ public class InboundOrdersController extends BaseController
// 防护3:校验orderType非空且合法
if (orderType == null || !Arrays.asList(1, 2, 3).contains(orderType)) {
return error("导入类型不能为空,仅支持1/2/3!");
return error("导入类型不能为空,当前仅支持1/2/3!");
}
// 2. 解析Excel表头(适配EasyExcel 2.x,无interrupt、无readRowNumber)
......@@ -242,40 +243,62 @@ public class InboundOrdersController extends BaseController
default: throw new ServiceException("不支持的导入类型,请联系管理员" + orderType);
}
}
private static final Map<Class<?>, List<String>> REQUIRED_HEADS_CACHE = new ConcurrentHashMap<>();
/**
* 通用导入逻辑(泛型适配不同VO)
*/
private <T> String handleImport(Class<T> clazz, MultipartFile file, List<String> headerList,
Integer updateSupport, String operName, Integer orderType) throws Exception {
// 反射读取VO中@Excel注解的必填表头
List<String> requiredExcelHeads = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Excel.class)) {
requiredExcelHeads.add(field.getAnnotation(Excel.class).name().trim());
// 1. 获取必填表头
List<String> requiredExcelHeads = REQUIRED_HEADS_CACHE.computeIfAbsent(clazz, cls -> {
List<String> heads = new ArrayList<>();
for (Field field : cls.getDeclaredFields()) {
if (field.isAnnotationPresent(Excel.class)) {
heads.add(cleanHeader(field.getAnnotation(Excel.class).name()));
}
}
}
return Collections.unmodifiableList(heads);
});
// 2. 安全清洗表头
final List<String> finalHeaderList =
(headerList != null ? headerList : Collections.<String>emptyList()).stream()
.map(this::cleanHeader)
.collect(Collectors.toList());
// 校验表头是否包含所有必填项
if (!headerList.containsAll(requiredExcelHeads)) {
// 3. 校验表头
if (!finalHeaderList.containsAll(requiredExcelHeads)) {
List<String> missingHeads = requiredExcelHeads.stream()
.filter(head -> !headerList.contains(head))
.filter(head -> !finalHeaderList.contains(head))
.collect(Collectors.toList());
return "导入数据字段与目标模板不一致,请检查!缺失字段:" + String.join("、", missingHeads);
}
// 解析Excel数据(若依ExcelUtil适配2.x,无需修改)
// 4. 可选:检查字段顺序(如果需要)
// checkColumnOrder(requiredExcelHeads, finalHeaderList);
// 5. 解析Excel
ExcelUtil<T> util = new ExcelUtil<>(clazz);
List<T> dataList = util.importExcel(file.getInputStream());
if (CollectionUtils.isEmpty(dataList)) {
return "Excel中未解析到有效数据,请检查模板是否正确!";
}
// 调用Service导入(需确保Service支持泛型列表,或强转Object)
// 6. 导入数据
return inboundOrdersService.importInboundOrders(dataList, updateSupport, operName, orderType);
}
// 清洗
private String cleanHeader(String header) {
if (header == null) return "";
return header
.replace(" ", " ") // 全角空格转半角
.replace("\u00A0", " ") // 不换行空格(&nbsp;)
.replace("\u2007", " ") // 数字空格
.replace("\u202F", " ") // 窄空格
.replaceAll("\\s+", " ") // 合并多个连续空格为单个
.trim();
}
// 辅助方法:获取缺失的列名(备用)
private String getMissingColumns(List<String> template, List<String> excel) {
return template.stream()
......
......@@ -60,7 +60,7 @@ public class InboundOrders extends BaseEntity
@JsonFormat(pattern = "yyyy-MM-dd")
private Date inboundDateStart;
/** 结束日期 只做查询 */
@JsonFormat(pattern = "yyyy-MM-dd")
private Date inboundDateEnd;
......@@ -101,6 +101,9 @@ public class InboundOrders extends BaseEntity
@Excel(name = "排序号")
private String updateUserCode;
/** 是否为导入数据 1-是 0-否 */
private Integer isImport;
/** 入库单明细信息 */
private List<InboundOrderItems> inboundOrderItemsList;
......@@ -326,32 +329,41 @@ public class InboundOrders extends BaseEntity
this.inboundDateEnd = inboundDateEnd;
}
public Integer getIsImport() {
return isImport;
}
public void setIsImport(Integer isImport) {
this.isImport = isImport;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("orderId", getOrderId())
.append("systemNo", getSystemNo())
.append("orderTypeId", getOrderTypeId())
.append("batchCode", getBatchId())
.append("warehouseId", getWarehouseId())
.append("ownerId", getOwnerId())
.append("orderStatus", getOrderStatus())
.append("inboundDate", getInboundDate())
.append("orderType", getOrderType())
.append("totalPlannedQuantity", getTotalPlannedQuantity())
.append("totalActualQuantity", getTotalActualQuantity())
.append("totalPackages", getTotalPackages())
.append("remark", getRemark())
.append("opUserName", getOpUserName())
.append("isUsed", getIsUsed())
.append("sortNo", getSortNo())
.append("createTime", getCreateTime())
.append("createUserCode", getCreateUserCode())
.append("updateTime", getUpdateTime())
.append("updateUserCode", getUpdateUserCode())
.append("inboundOrderItemsList", getInboundOrderItemsList())
.append("ownerName", getOwnerName())
.toString();
.append("id", getId())
.append("orderId", getOrderId())
.append("systemNo", getSystemNo())
.append("orderTypeId", getOrderTypeId())
.append("batchCode", getBatchId())
.append("warehouseId", getWarehouseId())
.append("ownerId", getOwnerId())
.append("orderStatus", getOrderStatus())
.append("inboundDate", getInboundDate())
.append("orderType", getOrderType())
.append("totalPlannedQuantity", getTotalPlannedQuantity())
.append("totalActualQuantity", getTotalActualQuantity())
.append("totalPackages", getTotalPackages())
.append("remark", getRemark())
.append("opUserName", getOpUserName())
.append("isUsed", getIsUsed())
.append("sortNo", getSortNo())
.append("createTime", getCreateTime())
.append("createUserCode", getCreateUserCode())
.append("updateTime", getUpdateTime())
.append("updateUserCode", getUpdateUserCode())
.append("inboundOrderItemsList", getInboundOrderItemsList())
.append("ownerName", getOwnerName())
.append("isImport", getIsImport())
.toString();
}
}
......@@ -63,15 +63,15 @@ public class InboundItemsTO extends BaseEntity {
/** 库位 */
@Excel(name = "库位")
private String locationId;
private String locationName;
/** 仓库 */
@Excel(name = "仓库")
private String warehouseId;
/** 库位(成品) */
@Excel(name = "库位(成品)")
private String finishedLocationId;
@Excel(name = "目标库位(成品)")
private String finishedLocation;
/** 备注(TRDC) */
@Excel(name = "备注(TRDC)")
......@@ -79,7 +79,7 @@ public class InboundItemsTO extends BaseEntity {
/** 收货库位(正常) */
@Excel(name = "收货库位(正常)")
private String relocationId;
private String relocation;
/** 标签颜色 */
@Excel(name = "标签颜色")
......@@ -107,11 +107,11 @@ public class InboundItemsTO extends BaseEntity {
/** 智观(客户订单号) */
@Excel(name = "智观(客户订单号)")
private String finishedSystemNo;
private String finishedOrderId;
/** 客户订单号/PO号 */
@Excel(name = "客户订单号/PO号")
private String finishedOrderId;
private String finishedSystemNo;
/** 货主 */
@Excel(name = "货主")
......@@ -143,8 +143,7 @@ public class InboundItemsTO extends BaseEntity {
/** 仓库名称 */
private String warehousesName;
/** 库位名称 */
private String locationName;
/** 计划件数 暂无用 */
......@@ -184,4 +183,6 @@ public class InboundItemsTO extends BaseEntity {
/** 更新人编码 */
private String updateUserCode;
private Integer orderStatus;
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package com.ruoyi.inventory.mapper;
import java.util.List;
import com.ruoyi.inventory.domain.InboundOrders;
import com.ruoyi.inventory.domain.InboundOrderItems;
import com.ruoyi.inventory.domain.TO.InboundItemsTO;
import com.ruoyi.inventory.domain.vo.InboundMaterialTotalVO;
/**
......@@ -83,7 +84,6 @@ public interface InboundOrdersMapper
* @return 结果
*/
public int batchInboundOrderItems(List<InboundOrderItems> inboundOrderItemsList);
/**
* 通过入库单主主键删除入库单明细信息
......@@ -94,6 +94,14 @@ public interface InboundOrdersMapper
public int deleteInboundOrderItemsByOrderId(String id);
/**
* 导出查询
*
* @param inboundOrders 入库单主ID
* @return 结果
*/
public List<InboundItemsTO> selectInboundOrdersAndItems(InboundOrders inboundOrders);
/**
* 统计本月入库次数
* @param monthParam yy-MM
* @return 结果
......
......@@ -3,6 +3,7 @@ package com.ruoyi.inventory.service;
import java.util.List;
import com.ruoyi.inventory.domain.InboundOrders;
import com.ruoyi.inventory.domain.TO.InboundItemsTO;
import com.ruoyi.inventory.domain.vo.InboundMaterialTotalVO;
......@@ -31,6 +32,14 @@ public interface IInboundOrdersService
public List<InboundOrders> selectInboundOrdersList(InboundOrders inboundOrders);
/**
* 查询入库单和明细平铺信息
*
* @param inboundOrders 入库单主
* @return 入库单主集合
*/
public List<InboundItemsTO> selectInboundOrdersAndItems(InboundOrders inboundOrders);
/**
* 新增入库单主
*
* @param inboundOrders 入库单主
......
......@@ -9,6 +9,7 @@ import java.util.stream.Collectors;
import com.ruoyi.common.config.WarehouseConfig;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.inventory.domain.*;
import com.ruoyi.inventory.domain.TO.InboundItemsTO;
import com.ruoyi.inventory.domain.vo.InboundMaterialTotalVO;
import com.ruoyi.inventory.mapper.*;
import org.apache.commons.lang3.SystemUtils;
......@@ -74,6 +75,11 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
return inboundOrdersMapper.selectInboundOrdersList(inboundOrders);
}
@Override
public List<InboundItemsTO> selectInboundOrdersAndItems(InboundOrders inboundOrders) {
return inboundOrdersMapper.selectInboundOrdersAndItems(inboundOrders);
}
/**
* 新增入库单主
*
......@@ -87,6 +93,7 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
inboundOrders.setCreateTime(DateUtils.getNowDate());
inboundOrders.setCreateUserCode(SystemUtils.getUserName());
inboundOrders.setCreateBy(SystemUtils.getUserName());
inboundOrders.setIsImport(0);
int rows = inboundOrdersMapper.insertInboundOrders(inboundOrders);
insertInboundOrderItems(inboundOrders);
return rows;
......@@ -238,6 +245,7 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
Method getOrderIdMethod = vo.getClass().getMethod("getOrderId");
String orderId = Optional.ofNullable(getOrderIdMethod.invoke(vo))
.map(Object::toString)
.map(String::trim)
.orElse("");
return StringUtils.isNotBlank(orderId);
} catch (Exception e) {
......@@ -252,10 +260,12 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
Method getBatchIdMethod = vo.getClass().getMethod("getBatchId");
return Optional.ofNullable(getOrderIdMethod.invoke(vo))
.map(Object::toString)
.map(String::trim)
.orElse("")
+ "--" +
Optional.ofNullable(getBatchIdMethod.invoke(vo))
.map(Object::toString)
.map(String::trim)
.orElse("");
} catch (Exception e) {
throw new ServiceException("分组获取orderId失败" + e);
......@@ -272,8 +282,14 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
// 4. 遍历每个入库单分组处理
for (Map.Entry<String, List<T>> entry : orderGroupMap.entrySet()) {
String orderKey = entry.getKey();
String orderId = orderKey.split("--")[0];
String batchId = orderKey.split("--")[1];
String orderId = "";
String batchId = "";
try{
orderId = orderKey.split("--")[0];
batchId = orderKey.split("--")[1];
}catch (IndexOutOfBoundsException e){
throw new ServiceException("请检查表头单号和批次号是否正确?");
}
List<T> voList = entry.getValue();
InboundOrders mainDO = null;
List<InboundOrderItems> itemDOList = new ArrayList<>();
......@@ -321,6 +337,7 @@ public class InboundOrdersServiceImpl implements IInboundOrdersService
mainDO.setCreateBy(operId);
mainDO.setCreateTime(now);
mainDO.setCreateUserCode(operId);
mainDO.setIsImport(1);
mainDO.setOrderTypeId(Optional.ofNullable(orderType).map(String::valueOf).orElse(""));
// 设置默认值
if (mainDO.getSortNo() == null) {
......
......@@ -155,6 +155,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="createUserCode != null">create_user_code,</if>
<if test="updateTime != null">update_time,</if>
<if test="updateUserCode != null">update_user_code,</if>
<if test="isImport != null">is_import,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
......@@ -178,6 +179,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="createUserCode != null">#{createUserCode},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="updateUserCode != null">#{updateUserCode},</if>
<if test="isImport != null">#{isImport},</if>
</trim>
</insert>
......@@ -283,4 +285,54 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
) t
order by total_money asc
</select>
<!--导出-->
<select id="selectInboundOrdersAndItems" resultMap="InboundOrderAndItemsMap" parameterType="com.ruoyi.inventory.domain.InboundOrders">
select io.inbound_date, m.sap_no, m.material_name,
m.ts_code, io.batch_id, ioi.planned_quantity,
ioi.divisor, ioi.actual_packages, ioi.actual_quantity,
m.total_weight, storage_locations.location_name, ioi.warehouse_id,
ioi.label_color, ioi.voucher_number, m.storage_temperature,
m.hazard_id, io.order_id, io.system_no, io.owner_id,
ioi.label_quantity, io.order_type, io.order_status, ioi.remark
from materials as m
inner join inbound_order_items as ioi on ioi.material_id = m.id
inner join inbound_orders as io on ioi.inbound_order_id = io.id
inner join storage_locations as sl on ioi.location_id = sl.id
</select>
<resultMap id="InboundOrderAndItemsMap" type="com.ruoyi.inventory.domain.TO.InboundItemsTO">
<!-- 基础字段映射:SQL字段名 -> Java实体类字段名 -->
<result column="inbound_date" property="inboundDate" />
<result column="sap_no" property="sapNo" />
<result column="material_name" property="materialName" />
<result column="ts_code" property="tsCode" />
<result column="batch_id" property="batchId" />
<result column="planned_quantity" property="plannedQuantity" />
<result column="remark" property="remark" />
<result column="divisor" property="divisor" />
<result column="actual_packages" property="actualPackages" />
<result column="actual_quantity" property="actualQuantity" />
<!-- 重量(成品):映射total_weight到weight -->
<result column="total_weight" property="weight" />
<result column="location_name" property="locationName" />
<result column="warehouse_id" property="warehouseId" />
<result column="label_color" property="labelColor" />
<result column="voucher_number" property="voucherNumber" />
<!-- 保温:映射storage_temperature到keepWarm -->
<result column="storage_temperature" property="keepWarm" />
<result column="hazard_id" property="hazardId" />
<result column="order_id" property="orderId" />
<result column="system_no" property="systemNo" />
<result column="owner_id" property="ownerId" />
<result column="label_quantity" property="labelQuantity" />
<result column="order_type" property="orderType" />
<result column="order_status" property="orderStatus"/>
</resultMap>
</mapper>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论