| | |
| | | package com.ycl.platform.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService { |
| | | |
| | | private final YwUnitMapper unitMapper; |
| | |
| | | public Result page(ReportQuery query) { |
| | | IPage<ReportVO> page = PageUtil.getPage(query, ReportVO.class); |
| | | query.setUnitId(SecurityUtils.getUnitId()); |
| | | if (query.getReportTimeEnd() != null) { |
| | | query.setReportTimeEnd(DateUtils.getDayEnd(query.getReportTimeEnd())); |
| | | } |
| | | |
| | | if (query.getEffectTimeEnd() != null) { |
| | | query.setEffectTimeEnd(DateUtils.getDayEnd(query.getEffectTimeEnd())); |
| | | } |
| | | long dbStartTime = System.currentTimeMillis(); |
| | | baseMapper.page(page, query); |
| | | long dbCostTime = System.currentTimeMillis() - dbStartTime; |
| | | // 5. 打印耗时日志(含请求参数,方便后续排查慢查询) |
| | | log.info("报表数据库查询耗时:{}ms,查询参数:{}", dbCostTime, JSON.toJSONString(query)); |
| | | |
| | | long dictStartTime = System.currentTimeMillis(); |
| | | List<SysDictData> errorTypeList = dictTypeService.selectDictDataByType("report_error_type"); |
| | | long dictEndTime = System.currentTimeMillis() - dictStartTime; |
| | | Map<String, String> dictMap = errorTypeList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel)); |
| | | page.getRecords().stream().forEach(item -> { |
| | | if (StringUtils.hasText(item.getErrorType())) { |
| | |
| | | item.setErrorType(sb.substring(0, sb.length() - 1)); |
| | | } |
| | | }); |
| | | log.info("组装信息耗时:{}ms", dictEndTime); |
| | | return Result.ok().data(page).total(page.getTotal()); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public List<ReportVO> export(ReportQuery query) { |
| | | // 1. 分页查询主数据(不变) |
| | | IPage<ReportVO> page = PageUtil.getPage(query, ReportVO.class); |
| | | page.setSize(-1); |
| | | query.setUnitId(SecurityUtils.getUnitId()); |
| | | baseMapper.page(page, query); |
| | | List<SysDictData> errorTypeList = dictTypeService.selectDictDataByType("report_error_type"); |
| | | Map<String, String> dictMap = errorTypeList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel)); |
| | | page.getRecords().forEach(item -> { |
| | | if (StringUtils.hasText(item.getErrorType())) { |
| | | item.setErrorTypeList(List.of(item.getErrorType().split(","))); |
| | | StringBuilder sb = new StringBuilder(); |
| | | item.getErrorTypeList().stream().forEach(err -> { |
| | | String s = dictMap.get(err); |
| | | if (org.springframework.util.StringUtils.hasText(s)) { |
| | | sb.append(s).append("、"); |
| | | } |
| | | }); |
| | | item.setErrorType(sb.substring(0, sb.length() - 1)); |
| | | } |
| | | // 审核结果 |
| | | List<ReportAuditingRecord> records = new LambdaQueryChainWrapper<>(reportAuditingRecordService.getBaseMapper()) |
| | | .eq(ReportAuditingRecord::getReportId, item.getId()) |
| | | .orderByDesc(ReportAuditingRecord::getCreateTime) |
| | | .last("limit 1") |
| | | .list(); |
| | | if (! CollectionUtils.isEmpty(records)) { |
| | | item.setResultStr(records.get(0).getResult() ? "通过" : "未通过"); |
| | | item.setResultRemark(records.get(0).getResultRemark()); |
| | | item.setAuditingTime(records.get(0).getCreateTime()); |
| | | List<ReportVO> records = page.getRecords(); |
| | | if (CollectionUtils.isEmpty(records)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 2. 批量查询字典数据(不变,建议加缓存) |
| | | List<SysDictData> errorTypeList = dictTypeService.selectDictDataByType("report_error_type"); |
| | | Map<String, String> errorDictMap = errorTypeList.stream() |
| | | .collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel, (k1, k2) -> k2)); |
| | | |
| | | // 3. 批量查询审核记录(核心修改:用子查询替代窗口函数,兼容低版本MySQL) |
| | | List<Integer> reportIds = records.stream().map(ReportVO::getId).collect(Collectors.toList()); |
| | | // 子查询:获取每个reportId的最新创建时间 |
| | | LambdaQueryWrapper<ReportAuditingRecord> maxTimeQuery = new LambdaQueryWrapper<ReportAuditingRecord>() |
| | | .in(ReportAuditingRecord::getReportId, reportIds) |
| | | .groupBy(ReportAuditingRecord::getReportId) |
| | | .select(ReportAuditingRecord::getReportId, ReportAuditingRecord::getCreateTime); // 分组查reportId和最大时间 |
| | | |
| | | // 主查询:关联子查询,拿到每个reportId最新的审核记录 |
| | | List<ReportAuditingRecord> allAuditRecords = reportAuditingRecordService.getBaseMapper().selectList( |
| | | new LambdaQueryWrapper<ReportAuditingRecord>() |
| | | .in(ReportAuditingRecord::getReportId, reportIds) |
| | | // 关联子查询,匹配reportId和最新创建时间 |
| | | .inSql(ReportAuditingRecord::getCreateTime, |
| | | "SELECT MAX(create_time) FROM t_report_auditing_record WHERE report_id IN (" + |
| | | reportIds.stream().map(String::valueOf).collect(Collectors.joining(",")) + |
| | | ") GROUP BY report_id") |
| | | ); |
| | | |
| | | // 构建reportId -> 最新审核记录的Map(不变) |
| | | Map<Integer, ReportAuditingRecord> auditRecordMap = allAuditRecords.stream() |
| | | .collect(Collectors.toMap(ReportAuditingRecord::getReportId, record -> record, (k1, k2) -> { |
| | | // 极端情况:同一reportId有相同创建时间的记录,取任意一个(按创建时间倒序取第一个) |
| | | return k1.getCreateTime().after(k2.getCreateTime()) ? k1 : k2; |
| | | })); |
| | | |
| | | // 4. 循环处理数据(不变,保留之前的字符串优化和空值处理) |
| | | records.forEach(item -> { |
| | | // 处理故障类型(不变) |
| | | String errorType = item.getErrorType(); |
| | | if (StringUtils.hasText(errorType)) { |
| | | String[] errorTypeArr = StringUtils.split(errorType, ","); |
| | | StringJoiner sj = new StringJoiner("、"); |
| | | for (String err : errorTypeArr) { |
| | | String dictLabel = errorDictMap.get(err); |
| | | if (StringUtils.hasText(dictLabel)) { |
| | | sj.add(dictLabel); |
| | | } |
| | | } |
| | | item.setErrorTypeList(Arrays.asList(errorTypeArr)); |
| | | item.setErrorType(sj.toString()); |
| | | } |
| | | |
| | | // 处理审核结果(不变,从Map获取) |
| | | ReportAuditingRecord auditRecord = auditRecordMap.get(item.getId()); |
| | | if (auditRecord != null) { |
| | | item.setResultStr(auditRecord.getResult() ? "通过" : "未通过"); |
| | | item.setResultRemark(auditRecord.getResultRemark()); |
| | | item.setAuditingTime(auditRecord.getCreateTime()); |
| | | } else { |
| | | item.setResultStr("审核中"); |
| | | } |
| | | item.setReportContent(EscapeUtil.clean(item.getReportContent())); |
| | | }); |
| | | return page.getRecords(); |
| | | } |
| | | |
| | | item.setReportContent(EscapeUtil.clean(StringUtils.defaultString(item.getReportContent()))); |
| | | }); |
| | | |
| | | return records; |
| | | } |
| | | |
| | | @Override |
| | | public Result auditingRecord(Integer id) { |