| | |
| | | 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.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import lombok.RequiredArgsConstructor; |
| | |
| | | public IPage<SalesRecordDto> selectSalesRecordList(Page<SalesRecord> page, SalesRecordDto salesRecordDto) { |
| | | // 1. 创建查询条件,按创建时间倒序排序 |
| | | LambdaQueryWrapper<SalesRecord> queryWrapper = new LambdaQueryWrapper<>(); |
| | | |
| | | // 按日期查询 |
| | | if (salesRecordDto.getSaleDate() != null) { |
| | | queryWrapper.eq(SalesRecord::getSaleDate, salesRecordDto.getSaleDate()); |
| | | } |
| | | |
| | | // 按煤种名称查询 |
| | | if (StringUtils.isNotBlank(salesRecordDto.getCoal())) { |
| | | LambdaQueryWrapper<CoalInfo> coalQueryWrapper = new LambdaQueryWrapper<>(); |
| | | coalQueryWrapper.like(CoalInfo::getCoal, salesRecordDto.getCoal()); |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalQueryWrapper); |
| | | |
| | | if (!coalInfos.isEmpty()) { |
| | | List<Long> coalIds = coalInfos.stream() |
| | | .map(CoalInfo::getId) |
| | | .collect(Collectors.toList()); |
| | | queryWrapper.in(SalesRecord::getCoalId, coalIds); |
| | | } else { |
| | | // 如果没有匹配的煤种,直接返回空结果 |
| | | queryWrapper.eq(SalesRecord::getCoalId, -1L); // 使用不可能存在的ID |
| | | } |
| | | } |
| | | |
| | | queryWrapper.orderByDesc(SalesRecord::getCreateTime); |
| | | |
| | | // 2. 获取分页的销售记录 |
| | |
| | | |
| | | // 4. 批量查询煤种信息并填充到结果中 |
| | | if (!coalIds.isEmpty()) { |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectBatchIds(coalIds); |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectByIds(coalIds); |
| | | for (CoalInfo coalInfo : coalInfos) { |
| | | Map<String, Object> record = resultMap.get(coalInfo.getId()); |
| | | if (record != null) { |
| | |
| | | } |
| | | } |
| | | |
| | | // 最终结果是一个List<Map>,每个Map包含合并后的销售数据和对应的煤种信息 |
| | | List<Map<String, Object>> results = new ArrayList<>(resultMap.values()); |
| | | |
| | | //月度数据 |
| | | //查询所有煤种信息 |
| | | List<CoalInfo> allCoalTypes = coalInfoMapper.selectList( |
| | | new QueryWrapper<CoalInfo>().orderByAsc("id") |
| | | ); |
| | | //预计算销量:按coalId分组统计总销量 |
| | | Map<Long, BigDecimal> salesByCoalId = salesRecords.stream() |
| | | .collect(Collectors.groupingBy( |
| | | SalesRecord::getCoalId, |
| | | Collectors.reducing( |
| | | BigDecimal.ZERO, |
| | | SalesRecord::getSaleQuantity, |
| | | BigDecimal::add |
| | | ) |
| | | )); |
| | | |
| | | // 2. 创建 resultMouth,存储煤种及其销量(Map 结构) |
| | | Map<String, BigDecimal> resultMouthMap = new LinkedHashMap<>(); |
| | | for (CoalInfo coal : allCoalTypes) { |
| | | resultMouthMap.put( |
| | | coal.getCoal(), // 煤种名称(如 "无烟煤") |
| | | salesByCoalId.getOrDefault(coal.getId(), BigDecimal.valueOf(0)) // 销量 |
| | | ); |
| | | } |
| | | |
| | | result.put("revenueAmount", revenueAmount.setScale(2, RoundingMode.HALF_UP)); |
| | | result.put("changeRate", changeRate); |
| | | result.put("trend", trend); |
| | |
| | | result.put("trendQuantity", trendQuantity); |
| | | result.put("revenueDistribution", revenueDistribution); |
| | | result.put("salesResults", results); |
| | | result.put("resultMouth", resultMouthMap); |
| | | |
| | | return result; |
| | | } |