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.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; import com.ruoyi.business.entity.SalesRecord; import com.ruoyi.business.mapper.OfficialInventoryMapper; import com.ruoyi.business.mapper.SalesRecordMapper; import com.ruoyi.business.service.SalesRecordService; 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.time.LocalDate; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** *

* 销售记录表 服务实现类 *

* * @author ruoyi * @since 2025-06-11 */ @Service @RequiredArgsConstructor public class SalesRecordServiceImpl extends ServiceImpl implements SalesRecordService { private final SalesRecordMapper salesRecordMapper; private final SysUserMapper userMapper; private final CustomerMapper customerMapper; private final OfficialInventoryMapper officialInventoryMapper; private final CoalInfoMapper coalInfoMapper; @Override public IPage selectSalesRecordList(Page page, SalesRecordDto salesRecordDto) { // 1. 创建查询条件,按创建时间倒序排序 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.orderByDesc(SalesRecord::getCreateTime); // 2. 获取分页的销售记录 IPage salesRecordPage = salesRecordMapper.selectPage(page, queryWrapper); // 3. 批量查询所有CoalInfo List coalIds = salesRecordPage.getRecords().stream() .map(SalesRecord::getCoalId) // 获取所有SalesRecord的coalId .collect(Collectors.toList()); Map coalInfoMap; // 如果有煤炭ID,执行批量查询 if (!coalIds.isEmpty()) { // 使用selectList进行批量查询 LambdaQueryWrapper coalInfoQueryWrapper = new LambdaQueryWrapper<>(); coalInfoQueryWrapper.in(CoalInfo::getId, coalIds); List coalInfos = coalInfoMapper.selectList(coalInfoQueryWrapper); // 将查询结果放入Map中,煤炭ID为键,CoalInfo为值 coalInfoMap = coalInfos.stream() .collect(Collectors.toMap(CoalInfo::getId, Function.identity())); } else { coalInfoMap = new HashMap<>(); } // 4. 创建返回结果页,使用BeanUtils进行属性复制 Page resultPage = new Page<>(); BeanUtils.copyProperties(salesRecordPage, resultPage); // 复制分页信息 // 5. 转换SalesRecord为SalesRecordDto,并设置每条销售记录的煤炭信息 List 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) public int addOrEditSalesRecord(SalesRecordDto salesRecordDto) { // 参数校验 validateSalesRecordDto(salesRecordDto); // 更新正式库待补库数量 OfficialInventory officialInventory = officialInventoryMapper.selectById(salesRecordDto.getCoalId()); if (officialInventory == null) { throw new BaseException("正式库煤种信息不存在"); } if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) { throw new BaseException("销售数量不能大于库存数量"); } officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity())); officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity()); officialInventoryMapper.updateById(officialInventory); // 构建销售记录实体 SalesRecord salesRecord = buildSalesRecord(salesRecordDto, officialInventory.getCoalId()); // 处理新增/更新逻辑 if (salesRecordDto.getId() == null) { return insertSalesRecord(salesRecord); } else { return updateSalesRecord(salesRecord); } } private void validateSalesRecordDto(SalesRecordDto dto) { if (dto == null) { throw new BaseException("销售记录数据不能为空"); } if (dto.getRegistrantId() == null) { throw new BaseException("登记人ID不能为空"); } if (dto.getCustomerId() == null) { throw new BaseException("客户ID不能为空"); } if (dto.getCoalId() == null) { throw new BaseException("请选择一条煤种信息"); } } private SalesRecord buildSalesRecord(SalesRecordDto dto, Long coalId) { SalesRecord record = new SalesRecord(); BeanUtils.copyProperties(dto, record); // 设置登记人信息 SysUser registrant = userMapper.selectUserById(dto.getRegistrantId()); if (registrant == null) { throw new BaseException("登记人信息不存在"); } record.setRegistrant(registrant.getUserName()); // 设置客户信息 Customer customer = customerMapper.selectById(dto.getCustomerId()); if (customer == null) { throw new BaseException("客户信息不存在"); } record.setCustomer(customer.getCustomerName()); // 设置日期信息 LocalDate now = LocalDate.now(); if (record.getId() == null) { // 新增时设置日期 record.setSaleDate(now); record.setRegistrationDate(now); } else { // 更新时不覆盖原有日期 SalesRecord existing = salesRecordMapper.selectById(record.getId()); if (existing == null) { throw new BaseException("销售记录不存在"); } record.setSaleDate(existing.getSaleDate()); record.setRegistrationDate(existing.getRegistrationDate()); } // 煤种 record.setCoalId(coalId); return record; } private int insertSalesRecord(SalesRecord record) { int result = salesRecordMapper.insert(record); if (result <= 0) { throw new BaseException("销售记录创建失败"); } return result; } private int updateSalesRecord(SalesRecord record) { int result = salesRecordMapper.updateById(record); if (result <= 0) { throw new BaseException("销售记录更新失败"); } return result; } @Override public int delByIds(Long[] ids) { // 检查参数 if (ids == null || ids.length == 0) { return 0; } // 构造更新条件 UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.in("id", ids) .set("deleted", 1); // 设置 deleted 为 1 表示已删除 // 执行批量逻辑删除 return salesRecordMapper.update(null, updateWrapper); } }