liding
11 小时以前 71fba5328a35b449b11088e540932787220f91d8
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.dto.ProductionMasterDto;
@@ -14,16 +15,16 @@
import com.ruoyi.business.mapper.ProductionMapper;
import com.ruoyi.business.mapper.ProductionMasterMapper;
import com.ruoyi.business.service.ProductionMasterService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -45,6 +46,8 @@
    private final ProductionMapper productionMapper;
    private final OfficialInventoryMapper officialInventoryMapper;
    private final SysUserMapper sysUserMapper;
    @Override
    public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
@@ -153,6 +156,12 @@
        productionMaster.setId(masterId);
        // 3. 统一子表处理逻辑
        Long producerId = productionMasterDto.getProducerId();
        if (producerId == null) {
            throw new BaseException("请选择生产者");
        }
        SysUser sysUser = sysUserMapper.selectUserById(producerId);
        productionMaster.setProducer(sysUser.getUserName());
        if (masterId == null) {
            productionMasterMapper.insert(productionMaster);
            masterId = productionMaster.getId(); // 获取新生成的ID
@@ -221,7 +230,102 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delByIds(Long[] ids) {
        return 0;
        if (ids == null || ids.length == 0) {
            return 0;
        }
        List<Long> idList = Arrays.asList(ids);
        // 1. 预加载所有关联数据
        List<ProductionInventory> allInventoryList = productionInventoryMapper.selectList(
                new LambdaQueryWrapper<ProductionInventory>()
                        .in(ProductionInventory::getProductionMasterId, idList)
        );
        // 2. 按官方库存ID分组并计算库存调整量
        Map<Long, BigDecimal> inventoryAdjustMap = allInventoryList.stream()
                .collect(Collectors.groupingBy(
                        ProductionInventory::getOfficialId,
                        Collectors.reducing(
                                BigDecimal.ZERO,
                                inv -> new BigDecimal(inv.getUsedQuantity()),
                                BigDecimal::add
                        )
                ));
        // 3. 批量更新官方库存 (使用SQL直接更新)
        if (!inventoryAdjustMap.isEmpty()) {
            inventoryAdjustMap.forEach((officialId, adjustAmount) ->
                    officialInventoryMapper.addInventoryQuantity(officialId, adjustAmount)
            );
        }
        // 4. 批量删除关联数据
        if (!allInventoryList.isEmpty()) {
            List<Long> inventoryIds = allInventoryList.stream()
                    .map(ProductionInventory::getId)
                    .collect(Collectors.toList());
            productionInventoryMapper.deleteBatchIds(inventoryIds);
        }
        // 删除生产明细
        productionMapper.delete(
                new LambdaQueryWrapper<Production>()
                        .in(Production::getProductionMasterId, idList)
        );
        // 5. 删除主记录
        return productionMasterMapper.deleteBatchIds(idList);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProductionInventory(ProductionMasterDto productionMasterDto) {
        List<ProductionInventory> inventories = productionMasterDto.getProductionInventoryList();
        if (CollectionUtils.isEmpty(inventories)) {
            return 0;
        }
        // 预收集数据用于批量操作
        Map<Long, BigDecimal> inventoryAdjustMap = new HashMap<>();
        List<Long> productionIdsToDelete = new ArrayList<>(inventories.size());
        for (ProductionInventory inventory : inventories) {
            // 收集需要删除的生产库存ID
            productionIdsToDelete.add(inventory.getId());
            // 累计库存调整量(相同officialId的用量累加)
            BigDecimal adjustment = new BigDecimal(inventory.getUsedQuantity());
            inventoryAdjustMap.merge(
                    inventory.getOfficialId(),
                    adjustment,
                    BigDecimal::add
            );
        }
        // 批量更新官方库存
        for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
            OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
            if (official == null) {
                throw new BaseException("官方库存不存在,ID: " + entry.getKey());
            }
            // 使用线程安全的BigDecimal操作
            official.setInventoryQuantity(
                    Optional.ofNullable(official.getInventoryQuantity())
                            .orElse(BigDecimal.ZERO)
                            .add(entry.getValue())
            );
            officialInventoryMapper.updateById(official);
        }
        // 批量删除生产库存
        if (!productionIdsToDelete.isEmpty()) {
            productionInventoryMapper.deleteBatchIds(productionIdsToDelete);
        }
        return productionIdsToDelete.size();
    }
}