liding
14 小时以前 4fedbed9949c6160dcfa216d6660bd3c625f7bce
main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
@@ -5,7 +5,9 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.entity.CoalInfo;
import com.ruoyi.basic.entity.Customer;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.business.dto.SalesRecordDto;
import com.ruoyi.business.entity.OfficialInventory;
@@ -22,6 +24,11 @@
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * <p>
@@ -43,12 +50,60 @@
    private final OfficialInventoryMapper officialInventoryMapper;
    private final CoalInfoMapper coalInfoMapper;
    @Override
    public IPage<SalesRecord> selectSalesRecordList(Page page, SalesRecordDto salesRecordDto) {
    public IPage<SalesRecordDto> selectSalesRecordList(Page<SalesRecord> page, SalesRecordDto salesRecordDto) {
        // 1. 创建查询条件,按创建时间倒序排序
        LambdaQueryWrapper<SalesRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SalesRecord::getCreateTime);
        return salesRecordMapper.selectPage(page, queryWrapper);
        // 2. 获取分页的销售记录
        IPage<SalesRecord> salesRecordPage = salesRecordMapper.selectPage(page, queryWrapper);
        // 3. 批量查询所有CoalInfo
        List<Long> coalIds = salesRecordPage.getRecords().stream()
                .map(SalesRecord::getCoalId)  // 获取所有SalesRecord的coalId
                .collect(Collectors.toList());
        Map<Long, CoalInfo> coalInfoMap;
        // 如果有煤炭ID,执行批量查询
        if (!coalIds.isEmpty()) {
            // 使用selectList进行批量查询
            LambdaQueryWrapper<CoalInfo> coalInfoQueryWrapper = new LambdaQueryWrapper<>();
            coalInfoQueryWrapper.in(CoalInfo::getId, coalIds);
            List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalInfoQueryWrapper);
            // 将查询结果放入Map中,煤炭ID为键,CoalInfo为值
            coalInfoMap = coalInfos.stream()
                    .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
        } else {
            coalInfoMap = new HashMap<>();
        }
        // 4. 创建返回结果页,使用BeanUtils进行属性复制
        Page<SalesRecordDto> resultPage = new Page<>();
        BeanUtils.copyProperties(salesRecordPage, resultPage);  // 复制分页信息
        // 5. 转换SalesRecord为SalesRecordDto,并设置每条销售记录的煤炭信息
        List<SalesRecordDto> dtoList = salesRecordPage.getRecords().stream().map(salesRecord -> {
            SalesRecordDto dto = new SalesRecordDto();
            BeanUtils.copyProperties(salesRecord, dto);  // 将SalesRecord的属性复制到SalesRecordDto中
            // 设置Coal信息
            CoalInfo coalInfo = coalInfoMap.get(salesRecord.getCoalId());
            if (coalInfo != null) {
                dto.setCoal(coalInfo.getCoal());
            }
            return dto;
        }).collect(Collectors.toList());
        resultPage.setRecords(dtoList);  // 设置转换后的DTO列表
        return resultPage;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -61,7 +116,7 @@
        if (officialInventory == null) {
            throw new BaseException("正式库煤种信息不存在");
        }
        if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0){
        if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) {
            throw new BaseException("销售数量不能大于库存数量");
        }
        officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity()));
@@ -69,7 +124,7 @@
        officialInventoryMapper.updateById(officialInventory);
        // 构建销售记录实体
        SalesRecord salesRecord = buildSalesRecord(salesRecordDto,officialInventory.getCoalId());
        SalesRecord salesRecord = buildSalesRecord(salesRecordDto, officialInventory.getCoalId());
        // 处理新增/更新逻辑
        if (salesRecordDto.getId() == null) {
@@ -94,7 +149,7 @@
        }
    }
    private SalesRecord buildSalesRecord(SalesRecordDto dto,Long coalId) {
    private SalesRecord buildSalesRecord(SalesRecordDto dto, Long coalId) {
        SalesRecord record = new SalesRecord();
        BeanUtils.copyProperties(dto, record);