gongchunyi
7 小时以前 8352c314f8a15a5caf6dfa2837562f839eeef6c3
feat: 库存管理调整为按照大类进行区分
已修改11个文件
365 ■■■■ 文件已修改
src/main/java/com/ruoyi/basic/pojo/ProductModel.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/controller/StockInventoryController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/execl/StockInventoryExportData.java 14 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/service/StockInventoryService.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java 99 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/basic/ProductModelMapper.xml 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/stock/StockInventoryMapper.xml 184 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/stock/StockUninventoryMapper.xml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/basic/pojo/ProductModel.java
@@ -69,10 +69,17 @@
    @TableField(exist = false)
    private LocalDateTime createTime;
    @ApiModelProperty(value = "创建用户")
    @TableField(fill = FieldFill.INSERT)
    private Integer createUser;
    @TableField(exist = false)
    private Long parentId;
    @TableField(exist = false)
    private String parentName;
    @ApiModelProperty("顶部父产品id")
    @TableField(exist = false)
    private Long topProductParentId;
}
src/main/java/com/ruoyi/stock/controller/StockInventoryController.java
@@ -44,6 +44,13 @@
        return R.ok(stockInventoryDtoIPage);
    }
    @GetMapping("/pageListCombinedStockInventory")
    @ApiOperation("分页查询联合库存列表")
    public R pageListCombinedStockInventory(Page page, StockInventoryDto stockInventoryDto) {
        IPage<StockInventoryDto> stockInventoryDtoIPage = stockInventoryService.pageListCombinedStockInventory(page, stockInventoryDto);
        return R.ok(stockInventoryDtoIPage);
    }
    @PostMapping("/addstockInventory")
    @ApiOperation("新增库存")
    public R addstockInventory(@RequestBody StockInventoryDto stockInventoryDto) {
src/main/java/com/ruoyi/stock/dto/StockInventoryDto.java
@@ -2,6 +2,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.stock.pojo.StockInventory;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
@@ -50,4 +51,34 @@
     * 厚度
     */
    private BigDecimal thickness;
    @ApiModelProperty("顶部父产品id")
    private Long topParentProductId;
    @ApiModelProperty("库存类型:qualified(合格)、unqualified(不合格)")
    private String stockType;
    @ApiModelProperty("合格库存数量")
    private BigDecimal qualifiedQuantity;
    @ApiModelProperty("不合格库存数量")
    private BigDecimal unQualifiedQuantity;
    @ApiModelProperty("合格库存冻结数量")
    private BigDecimal qualifiedLockedQuantity;
    @ApiModelProperty("不合格库存冻结数量")
    private BigDecimal unQualifiedLockedQuantity;
    @ApiModelProperty("合格库存未冻结数量")
    private BigDecimal qualifiedUnLockedQuantity;
    @ApiModelProperty("不合格库存未冻结数量")
    private BigDecimal unQualifiedUnLockedQuantity;
    @ApiModelProperty("合格库存ID")
    private Long qualifiedId;
    @ApiModelProperty("不合格库存ID")
    private Long unQualifiedId;
}
src/main/java/com/ruoyi/stock/execl/StockInventoryExportData.java
@@ -19,14 +19,20 @@
    @Excel(name = "单位")
    private String unit;
    @Excel(name = "库存数量")
    private BigDecimal qualitity;
    @Excel(name = "合格库存数量")
    private BigDecimal qualifiedQuantity;
    @Excel(name = "不合格库存数量")
    private BigDecimal unQualifiedQuantity;
    @Excel(name = "预警数量")
    private BigDecimal warnNum;
    @Excel(name = "冻结数量")
    private BigDecimal lockedQuantity;
    @Excel(name = "合格冻结数量")
    private BigDecimal qualifiedLockedQuantity;
    @Excel(name = "不合格冻结数量")
    private BigDecimal unQualifiedLockedQuantity;
    @Excel(name = "备注")
    private String remark;
src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java
@@ -29,6 +29,7 @@
    IPage<StockInventoryDto> pagestockInventory(Page page, @Param("ew") StockInventoryDto stockInventoryDto);
    IPage<StockInventoryDto> pageListCombinedStockInventory(Page page, @Param("ew") StockInventoryDto stockInventoryDto);
    int updateAddStockInventory(@Param("ew") StockInventoryDto stockInventoryDto);
src/main/java/com/ruoyi/stock/service/StockInventoryService.java
@@ -23,6 +23,8 @@
    IPage<StockInventoryDto> pagestockInventory(Page page, StockInventoryDto stockInventoryDto);
    IPage<StockInventoryDto> pageListCombinedStockInventory(Page page, StockInventoryDto stockInventoryDto);
    Boolean addstockInventory(StockInventoryDto stockInventoryDto);
    Boolean subtractStockInventory(StockInventoryDto stockInventoryDto);
src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -6,7 +6,7 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.R;
@@ -15,12 +15,14 @@
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockOutRecordDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.execl.StockInventoryExportData;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockInventoryService;
import com.ruoyi.stock.service.StockOutRecordService;
import com.ruoyi.stock.service.StockUninventoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -29,7 +31,9 @@
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
@@ -46,11 +50,17 @@
    private StockInventoryMapper stockInventoryMapper;
    private StockInRecordService stockInRecordService;
    private StockOutRecordService stockOutRecordService;
    private StockUninventoryService stockUninventoryService;
    private SalesLedgerProductMapper salesLedgerProductMapper;
    @Override
    public IPage<StockInventoryDto> pagestockInventory(Page page, StockInventoryDto stockInventoryDto) {
        return stockInventoryMapper.pagestockInventory(page, stockInventoryDto);
    }
    @Override
    public IPage<StockInventoryDto> pageListCombinedStockInventory(Page page, StockInventoryDto stockInventoryDto) {
        return stockInventoryMapper.pageListCombinedStockInventory(page, stockInventoryDto);
    }
    //入库调用
@@ -135,59 +145,98 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R importStockInventory(MultipartFile file) {
        try {
            // 查询所有的产品
            List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectProduct();
            Map<String, SalesLedgerProduct> productMap = new HashMap<>();
            for (SalesLedgerProduct product : salesLedgerProducts) {
                // 使用产品类别和规格型号作为键
                String key = product.getProductCategory() + "|" + product.getSpecificationModel();
                productMap.put(key, product);
            }
            ExcelUtil<StockInventoryExportData> util = new ExcelUtil<StockInventoryExportData>(StockInventoryExportData.class);
            List<StockInventoryExportData> list = util.importExcel(file.getInputStream());
            // 记录未找到匹配项的数据
            List<String> unmatchedRecords = new ArrayList<>();
            int successCount = 0;
            list.forEach(dto -> {
                boolean matched = false;
                for (SalesLedgerProduct item : salesLedgerProducts) {
                    if (item.getProductCategory().equals(dto.getProductName()) &&
                            item.getSpecificationModel().equals(dto.getModel())) {
            for (StockInventoryExportData dto : list) {
                // 构建查找键
                String key = dto.getProductName() + "|" + dto.getModel();
                SalesLedgerProduct matchedProduct = productMap.get(key);
                if (matchedProduct != null) {
                    // 处理合格库存
                    if (dto.getQualifiedQuantity() != null && dto.getQualifiedQuantity().compareTo(BigDecimal.ZERO) > 0) {
                        StockInventoryDto stockInventoryDto = new StockInventoryDto();
                        stockInventoryDto.setRecordId(0L);
                        stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode());
                        stockInventoryDto.setQualitity(dto.getQualitity());
                        stockInventoryDto.setQualitity(dto.getQualifiedQuantity());
                        stockInventoryDto.setRemark(dto.getRemark());
                        stockInventoryDto.setWarnNum(dto.getWarnNum());
                        if (ObjectUtils.isNotEmpty(dto.getLockedQuantity()) && dto.getLockedQuantity().compareTo(dto.getQualitity()) > 0) {
                            throw new RuntimeException("冻结数量不能超过本次导入的库存数量");
                        // 验证合格冻结数量
                        if (ObjectUtils.isNotEmpty(dto.getQualifiedLockedQuantity())) {
                            if (dto.getQualifiedLockedQuantity().compareTo(dto.getQualifiedQuantity()) > 0) {
                                throw new ServiceException("合格冻结数量不能超过本次导入的合格库存数量");
                            }
                            stockInventoryDto.setLockedQuantity(dto.getQualifiedLockedQuantity());
                        } else {
                            stockInventoryDto.setLockedQuantity(BigDecimal.ZERO);
                        }
                        stockInventoryDto.setLockedQuantity(dto.getLockedQuantity());
                        stockInventoryDto.setProductModelId(item.getProductModelId());
                        stockInventoryDto.setProductModelId(matchedProduct.getProductModelId());
                        this.addstockInventory(stockInventoryDto);
                        matched = true;
                        break; // 找到匹配项后跳出循环
                        successCount++;
                    }
                    // 处理不合格库存
                    if (dto.getUnQualifiedQuantity() != null && dto.getUnQualifiedQuantity().compareTo(BigDecimal.ZERO) > 0) {
                        StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
                        stockUninventoryDto.setRecordId(0L);
                        stockUninventoryDto.setRecordType(StockInUnQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode());
                        stockUninventoryDto.setQualitity(dto.getUnQualifiedQuantity());
                        stockUninventoryDto.setRemark(dto.getRemark());
                        // 验证不合格冻结数量
                        if (ObjectUtils.isNotEmpty(dto.getUnQualifiedLockedQuantity())) {
                            if (dto.getUnQualifiedLockedQuantity().compareTo(dto.getUnQualifiedQuantity()) > 0) {
                                throw new ServiceException("不合格冻结数量不能超过本次导入的不合格库存数量");
                            }
                            stockUninventoryDto.setLockedQuantity(dto.getUnQualifiedLockedQuantity());
                        } else {
                            stockUninventoryDto.setLockedQuantity(BigDecimal.ZERO);
                        }
                        stockUninventoryDto.setProductModelId(matchedProduct.getProductModelId());
                        stockUninventoryService.addStockUninventory(stockUninventoryDto);
                        successCount++;
                    }
                } else {
                    // 记录未匹配的产品
                    String unmatchedRecord = "产品名称:" + dto.getProductName() + ",型号:" + dto.getModel();
                    unmatchedRecords.add(unmatchedRecord);
                }
                if (!matched) {
                    // 记录未匹配的数据
                    String unmatchedInfo = String.format("产品名称:%s,规格型号:%s",
                            dto.getProductName(), dto.getModel());
                    unmatchedRecords.add(unmatchedInfo);
                }
            });
            }
            // 构建返回信息
            StringBuilder message = new StringBuilder();
            if (!unmatchedRecords.isEmpty()) {
                message.append("以下产品未找到匹配项:\n");
                message.append("导入成功 " + successCount + " 条记录,以下产品未找到匹配项:\n");
                for (String record : unmatchedRecords) {
                    message.append(record).append("\n");
                }
                throw new RuntimeException(message.toString());
                return R.ok(message.toString());
            }
            return R.ok("导入成功,共处理 " + successCount + " 条记录");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导入库存失败", e);
            return R.fail("导入失败:" + e.getMessage());
        }
        return R.ok("导入成功");
    }
@@ -232,4 +281,4 @@
        stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
        return this.updateById(stockInventory);
    }
}
}
src/main/java/com/ruoyi/stock/service/impl/StockUninventoryServiceImpl.java
@@ -67,6 +67,7 @@
            StockUninventory newStockUnInventory = new StockUninventory();
            newStockUnInventory.setProductModelId(stockUninventoryDto.getProductModelId());
            newStockUnInventory.setQualitity(stockUninventoryDto.getQualitity());
            newStockUnInventory.setLockedQuantity(stockUninventoryDto.getLockedQuantity());
            newStockUnInventory.setVersion(1);
            newStockUnInventory.setRemark(stockUninventoryDto.getRemark());
            stockUninventoryMapper.insert(newStockUnInventory);
src/main/resources/mapper/basic/ProductModelMapper.xml
@@ -56,6 +56,22 @@
            <if test="c.parentName != null and c.parentName != ''">
                AND pt.top_name LIKE CONCAT('%', #{c.parentName}, '%')
            </if>
            <if test="c.topProductParentId != null and c.topProductParentId > 0">
                and p.id in (
                    WITH RECURSIVE product_tree AS (
                        SELECT id
                        FROM product
                        WHERE id = #{c.topProductParentId}
                        UNION ALL
                        SELECT p.id
                        FROM product p
                        INNER JOIN product_tree pt ON p.parent_id = pt.id
                    )
                    select id from product_tree
                )
            </if>
        </where>
        ORDER BY pm.id DESC
    </select>
src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -80,22 +80,174 @@
        </if>
    </select>
    <select id="pageListCombinedStockInventory" resultType="com.ruoyi.stock.dto.StockInventoryDto">
        WITH RECURSIVE product_tree AS (
        SELECT id
        FROM product
        WHERE id = #{ew.topParentProductId}
        UNION ALL
        SELECT p.id
        FROM product p
        INNER JOIN product_tree pt ON p.parent_id = pt.id
        )
        select
            MAX(qualifiedId) as qualifiedId,
            MAX(unQualifiedId) as unQualifiedId,
            SUM(qualifiedQuantity) as qualifiedQuantity,
            SUM(unQualifiedQuantity) as unQualifiedQuantity,
            SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
            SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
            SUM(qualifiedQuantity - qualifiedLockedQuantity) as qualifiedUnLockedQuantity,
            SUM(unQualifiedQuantity - unQualifiedLockedQuantity) as unQualifiedUnLockedQuantity,
            product_model_id,
            MAX(create_time) as create_time,
            MAX(update_time) as update_time,
            MAX(warn_num) as warn_num,
            MAX(version) as version,
            model,
            MAX(remark) as remark,
            unit,
            product_name,
            product_id,
            'combined' as stockType
        from (
            select
            si.id as qualifiedId,
            null as unQualifiedId,
            si.qualitity as qualifiedQuantity,
            0 as unQualifiedQuantity,
            COALESCE(si.locked_quantity, 0) as locked_quantity,
            COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
            0 as unQualifiedLockedQuantity,
            si.product_model_id,
            si.create_time,
            si.update_time,
            COALESCE(si.warn_num, 0) as warn_num,
            si.version,
            (si.qualitity - COALESCE(si.locked_quantity, 0)) as un_locked_quantity,
            pm.model,
            si.remark,
            pm.unit,
            p.product_name,
            p.id as product_id
            from stock_inventory si
            left join product_model pm on si.product_model_id = pm.id
            left join product p on pm.product_id = p.id
            union all
            select
            null as qualifiedId,
            su.id as unQualifiedId,
            0 as qualifiedQuantity,
            su.qualitity as unQualifiedQuantity,
            COALESCE(su.locked_quantity, 0) as locked_quantity,
            0 as qualifiedLockedQuantity,
            COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
            su.product_model_id,
            su.create_time,
            su.update_time,
            0 as warn_num,
            su.version,
            (su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
            pm.model,
            su.remark,
            pm.unit,
            p.product_name,
            p.id as product_id
            from stock_uninventory su
            left join product_model pm on su.product_model_id = pm.id
            left join product p on pm.product_id = p.id
        ) as combined
        <where>
            <if test="ew.productName != null and ew.productName !=''">
                and combined.product_name like concat('%',#{ew.productName},'%')
            </if>
            <if test="ew.model != null and ew.model !=''">
                and combined.model like concat('%',#{ew.model},'%')
            </if>
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                and combined.product_id in (select id from product_tree)
            </if>
        </where>
        group by product_model_id, model, unit, product_name, product_id
    </select>
    <select id="listStockInventoryExportData" resultType="com.ruoyi.stock.execl.StockInventoryExportData">
        select si.qualitity,
        pm.model,
        pm.unit,
        p.product_name,
        coalesce(si.warn_num, 0) as warn_num,
        coalesce(si.locked_quantity, 0) as locked_quantity,
        si.remark,
        si.update_time
        from stock_inventory si
        left join product_model pm on si.product_model_id = pm.id
        left join product p on pm.product_id = p.id
        where 1 = 1
        <if test="ew.productName != null and ew.productName !=''">
            and p.product_name like concat('%',#{ew.productName},'%')
        </if>
        WITH RECURSIVE product_tree AS (
        SELECT id
        FROM product
        WHERE id = #{ew.topParentProductId}
        UNION ALL
        SELECT p.id
        FROM product p
        INNER JOIN product_tree pt ON p.parent_id = pt.id
        )
        select
            SUM(qualifiedQuantity) as qualifiedQuantity,
            SUM(unQualifiedQuantity) as unQualifiedQuantity,
            SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
            SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
            model,
            unit,
            product_name,
            MAX(warn_num) as warn_num,
            MAX(remark) as remark,
            MAX(update_time) as update_time
        from (
            select
            si.qualitity as qualifiedQuantity,
            0 as unQualifiedQuantity,
            COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
            0 as unQualifiedLockedQuantity,
            si.product_model_id,
            si.create_time,
            si.update_time,
            COALESCE(si.warn_num, 0) as warn_num,
            si.remark,
            pm.model,
            pm.unit,
            p.product_name,
            p.id as product_id
            from stock_inventory si
            left join product_model pm on si.product_model_id = pm.id
            left join product p on pm.product_id = p.id
            union all
            select
            0 as qualifiedQuantity,
            su.qualitity as unQualifiedQuantity,
            0 as qualifiedLockedQuantity,
            COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
            su.product_model_id,
            su.create_time,
            su.update_time,
            0 as warn_num,
            su.remark,
            pm.model,
            pm.unit,
            p.product_name,
            p.id as product_id
            from stock_uninventory su
            left join product_model pm on su.product_model_id = pm.id
            left join product p on pm.product_id = p.id
        ) as combined
        <where>
            <if test="ew.productName != null and ew.productName !=''">
                and combined.product_name like concat('%',#{ew.productName},'%')
            </if>
            <if test="ew.model != null and ew.model !=''">
                and combined.model like concat('%',#{ew.model},'%')
            </if>
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                and combined.product_id in (select id from product_tree)
            </if>
        </where>
        group by product_model_id, model, unit, product_name
    </select>
    <select id="stockInventoryPage" resultType="com.ruoyi.stock.dto.StockInRecordDto">
        select sir.*,si.qualitity as current_stock,
@@ -246,4 +398,4 @@
        ORDER BY DATE(sor.create_time) ASC
    </select>
</mapper>
</mapper>
src/main/resources/mapper/stock/StockUninventoryMapper.xml
@@ -34,6 +34,9 @@
            <if test="ew.qualitity != null">
                qualitity = qualitity + #{ew.qualitity},
            </if>
            <if test="ew.lockedQuantity != null">
                locked_quantity = locked_quantity + #{ew.lockedQuantity},
            </if>
            <if test="ew.version != null">
                version = version + 1,
            </if>