liding
2 天以前 f904274947d9a4bb44037e4d18a7d85c6aadaf7b
main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java
@@ -1,11 +1,30 @@
package com.ruoyi.business.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.basic.entity.CoalField;
import com.ruoyi.basic.entity.CoalValue;
import com.ruoyi.basic.mapper.CoalFieldMapper;
import com.ruoyi.basic.mapper.CoalValueMapper;
import com.ruoyi.business.dto.PendingInventoryDto;
import com.ruoyi.business.entity.OfficialInventory;
import com.ruoyi.business.entity.PendingInventory;
import com.ruoyi.business.mapper.OfficialInventoryMapper;
import com.ruoyi.business.mapper.PendingInventoryMapper;
import com.ruoyi.business.service.PendingInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -19,4 +38,160 @@
@RequiredArgsConstructor
public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService {
    private final PendingInventoryMapper pendingInventoryMapper;
    private final OfficialInventoryMapper officialInventoryMapper;
    private final CoalValueMapper coalValueMapper;
    private final CoalFieldMapper coalFieldMapper;
    @Override
    public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
        // 1. 构建主查询
        LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PendingInventory::getCreateTime);
        // 2. 执行主表分页查询
        IPage<PendingInventory> pendingInventoryPage = pendingInventoryMapper.selectPage(page, queryWrapper);
        // 3. 无数据快速返回
        if (CollectionUtils.isEmpty(pendingInventoryPage.getRecords())) {
            return new Page<>(page.getCurrent(), page.getSize(), pendingInventoryPage.getTotal());
        }
        // 4. 提取所有待处理库存ID
        List<Long> pendingIds = pendingInventoryPage.getRecords().stream()
                .map(PendingInventory::getId)
                .collect(Collectors.toList());
        // 5. 批量查询关联的正式库存信息
        Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds);
        // 6. 使用MyBatis-Plus的convert方法转换DTO
        return pendingInventoryPage.convert(record -> {
            PendingInventoryDto dto = new PendingInventoryDto();
            BeanUtils.copyProperties(record, dto);
            // 从预加载的Map中获取officialId
            dto.setOfficialId(pendingToOfficialMap.getOrDefault(record.getId(), null));
            return dto;
        });
    }
    // 批量获取待处理库存与正式库存的映射关系
    private Map<Long, Long> getOfficialInventoryMap(List<Long> pendingIds) {
        if (CollectionUtils.isEmpty(pendingIds)) {
            return Collections.emptyMap();
        }
        // 查询关联的正式库存数据
        LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(OfficialInventory::getId, OfficialInventory::getPendingId)
                .in(OfficialInventory::getPendingId, pendingIds);
        return officialInventoryMapper.selectList(wrapper)
                .stream()
                .collect(Collectors.toMap(
                        OfficialInventory::getPendingId,
                        OfficialInventory::getId,
                        (existing, replacement) -> existing // 如果有重复,保留第一个
                ));
    }
    @Override
    public int addOrEditPending(PendingInventoryDto pendingInventoryDto) {
        PendingInventory pendingInventory = new PendingInventory();
        BeanUtils.copyProperties(pendingInventoryDto, pendingInventory);
        if (Objects.isNull(pendingInventoryDto.getId())) {
            return pendingInventoryMapper.insert(pendingInventory);
        } else {
            return pendingInventoryMapper.updateById(pendingInventory);
        }
    }
    @Override
    public int delByIds(Long[] ids) {
        // 检查参数
        if (ids == null || ids.length == 0) {
            return 0;
        }
        // 构造更新条件
        UpdateWrapper<PendingInventory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids)
                .set("deleted", 1);  // 设置 deleted 为 1 表示已删除
        // 执行批量逻辑删除
        return pendingInventoryMapper.update(null, updateWrapper);
    }
    @Transactional
    @Override
    public int addOrEditCoalValue(PendingInventoryDto pendingInventoryDto) {
        // Step 1: 删除原有 planId 相关数据
        coalValueMapper.delete(new LambdaQueryWrapper<CoalValue>()
                .eq(CoalValue::getPlanId, pendingInventoryDto.getPId()));
        // Step 2: 构建基础 CoalValue 对象
        CoalValue coalValue = new CoalValue();
        BeanUtils.copyProperties(coalValue, pendingInventoryDto);
        coalValue.setPlanId(pendingInventoryDto.getPId());
        List<Map<String, String>> fieldValue = pendingInventoryDto.getFieldValue();
        if (CollectionUtils.isEmpty(fieldValue)) {
            throw new BaseException("字段值不能为空");
        }
        // Step 3: 提前获取所有 field -> CoalField 映射,避免重复查库
        Set<String> allFields = fieldValue.stream()
                .flatMap(map -> map.keySet().stream())
                .collect(Collectors.toSet());
        List<CoalField> coalFields = coalFieldMapper.selectList(
                new LambdaQueryWrapper<CoalField>().in(CoalField::getFields, allFields));
        Map<String, String> fieldMap = coalFields.stream()
                .collect(Collectors.toMap(
                        CoalField::getFields,
                        CoalField::getFieldName));
        // Step 4: 批量插入
        int i = 0;
        for (Map<String, String> fieldMapEntry : fieldValue) {
            for (Map.Entry<String, String> entry : fieldMapEntry.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                String fieldName = fieldMap.get(key);
                if (fieldName == null) {
                    throw new BaseException("字段名不存在");
                }
                coalValue.setId(null); // 清空 id,确保每次都是新记录
                coalValue.setCoalValue(value);
                coalValue.setFields(key);
                coalValue.setFieldName(fieldName);
                i = coalValueMapper.insert(coalValue);
            }
        }
        if (i > 0) {
            BigDecimal quantity = pendingInventoryDto.getInventoryQuantity();
            PendingInventory pendingInventory = pendingInventoryMapper.selectById(pendingInventoryDto.getPId());
            if (pendingInventory == null) {
                throw new BaseException("待入库记录不存在");
            }
            BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity);
            if (left.compareTo(BigDecimal.ZERO) > 0) {
                pendingInventory.setInventoryQuantity(left);
                pendingInventoryMapper.updateById(pendingInventory);
            } else {
                pendingInventoryMapper.deleteById(pendingInventoryDto.getPId());
            }
            //正式库
            if (pendingInventoryDto.getOfficialId() == null) {
                OfficialInventory officialInventory = new OfficialInventory();
                BeanUtils.copyProperties(pendingInventory, officialInventory);
                officialInventory.setId(null);
                officialInventory.setPendingId(pendingInventoryDto.getPId());
                officialInventory.setInventoryQuantity(quantity);
                officialInventoryMapper.insert(officialInventory);
            }else {
                OfficialInventory officialInventory = officialInventoryMapper.selectById(pendingInventoryDto.getOfficialId());
                officialInventory.setInventoryQuantity(quantity.add(officialInventory.getInventoryQuantity()));
                officialInventoryMapper.updateById(officialInventory);
            }
        }
        return i;
    }
}