@@ -1,8 +1,8 @@
package digital.laboratory.platform.entrustment.service.impl ;
package digital.laboratory.platform.entrustment.service.impl ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.date.LocalDateTimeUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.log.Log ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.core.toolkit.StringUtils ;
import com.baomidou.mybatisplus.core.toolkit.StringUtils ;
@@ -10,10 +10,12 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import digital.laboratory.platform.common.core.exception.CheckedException ;
import digital.laboratory.platform.common.core.exception.CheckedException ;
import digital.laboratory.platform.common.core.util.R ;
import digital.laboratory.platform.entrustment.convert.DrugLiteConvert ;
import digital.laboratory.platform.entrustment.convert.DrugLiteConvert ;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert ;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert ;
import digital.laboratory.platform.entrustment.dto.CheckoutResultExcelDTO ;
import digital.laboratory.platform.entrustment.dto.CheckoutResultExcelDTO ;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO ;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO ;
import digital.laboratory.platform.entrustment.dto.ResultExcelDTO ;
import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult ;
import digital.laboratory.platform.entrustment.entity.EntrustMaterialCheckoutResult ;
import digital.laboratory.platform.entrustment.entity.Entrustment ;
import digital.laboratory.platform.entrustment.entity.Entrustment ;
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial ;
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial ;
@@ -39,6 +41,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpServletResponse ;
import java.io.IOException ;
import java.io.IOException ;
import java.math.BigDecimal ;
import java.math.BigDecimal ;
import java.math.RoundingMode ;
import java.net.URLEncoder ;
import java.net.URLEncoder ;
import java.time.LocalDateTime ;
import java.time.LocalDateTime ;
import java.time.format.DateTimeFormatter ;
import java.time.format.DateTimeFormatter ;
@@ -517,122 +520,115 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl<Entrus
return materialPage ;
return materialPage ;
}
}
public List < EntrustmentIdentificationMaterialVO > getResultData ( Integer entrustType , String oldResult , LocalDateTime startTime , LocalDateTime endTime ) {
public List < EntrustmentIdentificationMaterialVO > getResultData ( ResultExcelDTO excelDTO ) {
LambdaQueryWrapper < EntrustMaterialCheckoutResult > qw = new LambdaQueryWrapper < > ( ) ;
if ( startTime ! = null & & endTime ! = null ) {
qw . ge ( startTime ! = null , EntrustMaterialCheckoutResult : : getCreateTime , startTime )
. le ( endTime ! = null , EntrustMaterialCheckoutResult : : getCreateTime , endTime ) ;
} else {
qw . ge ( EntrustMaterialCheckoutResult : : getCreateTime , this . getStartTime ( null ) ) ;
}
// 查询所有符合时间条件的检测结果
List < EntrustMaterialCheckoutResult > resultList = this . list ( qw
. isNotNull ( EntrustMaterialCheckoutResult : : getQualitativeResult )
. ne ( EntrustMaterialCheckoutResult : : getQualitativeResult , " " ) ) ;
// 如果没有查询到数据,则直接返回空列表
if ( CollUtil . isEmpty ( resultList ) ) {
return Collections . emptyList ( ) ;
}
// 按委托 ID 进行分组
Map < String , List < EntrustMaterialCheckoutResult > > resultMap = resultList . stream ( )
. collect ( Collectors . groupingBy ( EntrustMaterialCheckoutResult : : getEntrustId ) ) ;
// 获取所有的委托 ID
Set < String > entrustIdList = resultMap . keySet ( ) ;
LocalDateTime startTime = excelDTO . getStartTime ( ) ;
LocalDateTime endTime = excelDTO . getEndTime ( ) ;
String oldResult = excelDTO . getOldResult ( ) ;
Integer entrustType = excelDTO . getEntrustType ( ) ;
// 创建查询条件包装器
LambdaQueryWrapper < Entrustment > wrapper = new LambdaQueryWrapper < > ( ) ;
LambdaQueryWrapper < Entrustment > wrapper = new LambdaQueryWrapper < > ( ) ;
// 设置时间范围过滤条件
if ( startTime ! = null & & endTime ! = null ) {
wrapper . between ( Entrustment : : getAcceptTime , startTime , endTime ) ;
} else {
wrapper . ge ( Entrustment : : getAcceptTime , this . getStartTime ( null ) ) ;
}
// 处理 oldResult 过滤条件
if ( StringUtils . isNotBlank ( oldResult ) ) {
if ( StringUtils . isNotBlank ( oldResult ) ) {
if ( oldResult . equals ( " 委托 " ) ) {
if ( " 委托 " . equals ( oldResult ) ) {
String types [ ] = { " 首次鉴定 " , " 补充鉴定 " , " 重新鉴定 " } ;
wrapper . in ( Entrustment : : getOldIdentificationResult , " 首次鉴定 " , " 补充鉴定 " , " 重新鉴定 " ) ;
wrapper . in ( Entrustment : : getOldIdentificationResult , types ) ;
} else {
} else {
wrapper . eq ( Entrustment : : getOldIdentificationResult , oldResult ) ;
wrapper . eq ( Entrustment : : getOldIdentificationResult , oldResult ) ;
}
}
}
}
// 批量查询符合条件的委托信息,避免多次查询数据库
List < Entrustment > entrustmentList = entrustmentService . list ( wrapper
. in ( Entrustment : : getId , entrustIdList )
. eq ( entrustType ! = null , Entrustment : : getEntrustmentType , entrustType ) ) ;
// 查询符合条件的 Entrustment 列表,避免重复查询数据库
List < Entrustment > entrustmentList = entrustmentService . list (
wrapper . eq ( entrustType ! = null , Entrustment : : getEntrustmentType , entrustType )
) ;
System . out . println ( " 委托的数量: " + entrustmentList . size ( ) ) ;
// 如果无符合条件的委托信息,则返回空列表
if ( CollUtil . isEmpty ( entrustmentList ) ) {
if ( CollUtil . isEmpty ( entrustmentList ) ) {
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
}
// 获取所有 符合条件的委托 ID
// 获取符合条件的委托 ID 集合,提高查询效率
Set < String > filteredE ntrustIds = entrustmentList . stream ( )
Set < String > e ntrustIds = entrustmentList . stream ( )
. map ( Entrustment : : getId )
. map ( Entrustment : : getId )
. collect ( Collectors . toSet ( ) ) ;
. collect ( Collectors . toSet ( ) ) ;
// 获取所有符合条件的委托map
// 将 Entrustment 列表转换为 Map, 提高后续查询速度
Map < String , Entrustment > entrustMap = entrustmentList . stream ( )
Map < String , Entrustment > entrustMap = entrustmentList . stream ( )
. collect ( Collectors . toMap ( Entrustment : : getId , Function . identity ( ) ) ) ;
. collect ( Collectors . toMap ( Entrustment : : getId , Function . identity ( ) ) ) ;
// 过滤出 符合条件的检测结果
// 查询所有 符合条件的 EntrustmentIdentificationMaterial 数据
List < EntrustMaterialCheckoutResult > newResultList = resultList . stream ( )
List < EntrustmentIdentificationMaterial > materialList = entrustmentIdentificationMaterialService . list (
. filter ( result - > filteredEntrustIds . contains ( result . getEntrustId ( ) ) )
Wrappers . < EntrustmentIdentificationMaterial > lambdaQuery ( )
. collect ( Collectors . toList ( ) ) ;
. in ( EntrustmentIdentificationMaterial : : getEntrustmentId , entrustIds )
) ;
// 提取所有 resultId 批量查询 EntrustmentIdentificationMaterial, 避免循环查询数据库
// 若无符合条件的材料,则直接返回空列表
Set < String > resultIds = newResultList . stream ( )
if ( CollUtil . isEmpty ( materialList ) ) {
. map ( EntrustMaterialCheckoutResul t : : getId )
return Collections . emptyLis t ( ) ;
}
// 获取材料 ID 集合,提高查询效率
Set < String > materialIds = materialList . stream ( )
. map ( EntrustmentIdentificationMaterial : : getId )
. collect ( Collectors . toSet ( ) ) ;
. collect ( Collectors . toSet ( ) ) ;
// 批量 查询所有相关的材料信息
// 查询所有符合条件的检测结果数据
List < EntrustmentIdentificationMaterial > materialList = entrustmentIdentificationMaterialService . list ( Wrappers
List < EntrustMaterialCheckoutResult > resultList = this . list (
. < EntrustmentIdentificationMaterial > lambdaQuery ( )
Wrappers . < EntrustMaterialCheckoutResult > lambdaQuery ( )
. in ( EntrustmentIdentificationMaterial : : getId , result Ids)
. in ( EntrustMaterialCheckoutResult : : getId , material Ids)
. last ( " ORDER BY " +
) ;
" CAST(SUBSTRING_INDEX(accept_no, '-', 1) AS UNSIGNED) ASC, " + // 年份排序
" CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(accept_no, '-', -2), '-', 1) AS UNSIGNED) ASC, " + // 流水号排序
" CAST(SUBSTRING_INDEX(accept_no, '-', -1) AS UNSIGNED) ASC " ) ) ; // 子流水号排序
// 将检测结果转换为 Map, 避免后续 O(n^2) 复杂度查询
Map < String , EntrustMaterialCheckoutResult > resultMap = resultList . stream ( )
. collect ( Collectors . toMap ( EntrustMaterialCheckoutResult : : getId , Function . identity ( ) ) ) ;
// 将材料信息转换为 Map 以便快速查找
// 构造返回的 VO 列表
Map < String , EntrustmentIdentificationMaterial> materialMap = materialList . stream ( )
List < EntrustmentIdentificationMaterialVO > voList = materialList . stream ( ) . map ( material - > {
. collect ( Collectors . toMap ( EntrustmentIdentificationMaterial : : getId , Function . identity ( ) ) ) ;
// 构建返回的 VO 对象列表
List < EntrustmentIdentificationMaterialVO > voList = newResultList . stream ( )
. map ( result - > {
EntrustmentIdentificationMaterial material = materialMap . get ( result . getId ( ) ) ;
if ( material = = null ) {
return null ;
}
EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO ( ) ;
EntrustmentIdentificationMaterialVO vo = new EntrustmentIdentificationMaterialVO ( ) ;
BeanUtils . copyProperties ( material , vo ) ;
BeanUtils . copyProperties ( material , vo ) ;
vo . setDrugLites ( DrugLiteConvert . getDrugLites ( result . getQualitativeResult ( ) ) ) ;
Entrustment entrust = entrustMap . get ( vo . getEntrustmentId ( ) ) ;
String oldIdentificationResult = entrust . getOldIdentificationResult ( ) ;
if ( oldIdentificationResult . equals ( " 首次鉴定 " ) | | oldIdentificationResult . equals ( " 补充鉴定 " ) | | oldIdentificationResult . equals ( " 重新鉴定 " ) ) {
vo . setOldIdentificationResult ( " 委托 " ) ;
} else {
vo . setOldIdentificationResult ( oldIdentificationResult ) ;
}
vo . setOrgName ( entrust . getClientOrgName ( ) ) ;
return vo ;
} )
. filter ( Objects : : nonNull ) // 过滤掉可能为空的数据
. collect ( Collectors . toList ( ) ) ;
// 按 acceptNo 进行排序
// 设置检测结果
voList . sort ( Comparator . comparing ( ( EntrustmentIdentificationMaterial m ) - > {
vo . setDrugLites ( Optional . ofNullable ( resultMap . get ( material . getId ( ) ) )
String [ ] parts = m . getAcceptNo ( ) . split ( " - " ) ;
. map ( EntrustMaterialCheckoutResult : : getQualitativeResult )
return new int [ ] { Integer . parseInt ( parts [ 0 ] ) , Integer . parseInt ( parts [ 1 ] ) , Integer . parseInt ( parts [ 2 ] ) } ;
. map ( DrugLiteConvert : : getDrugLites )
} , Comparator . comparingInt ( ( int [ ] arr ) - > arr [ 0 ] ) // 按年份排序
. orElse ( null ) ) ;
. thenComparingInt ( arr - > arr [ 1 ] ) // 按流水号排序
. thenComparingInt ( arr - > arr [ 2 ] ) ) ) ; // 按子流水号排序
// 设置委托信息
return voList ;
Entrustment entrust = entrustMap . get ( material . getEntrustmentId ( ) ) ;
if ( entrust ! = null ) {
String oldIdentificationResult = entrust . getOldIdentificationResult ( ) ;
vo . setOldIdentificationResult (
( " 首次鉴定 " . equals ( oldIdentificationResult ) | | " 补充鉴定 " . equals ( oldIdentificationResult ) | | " 重新鉴定 " . equals ( oldIdentificationResult ) )
? " 委托 "
: oldIdentificationResult
) ;
vo . setOrgName ( entrust . getClientOrgName ( ) ) ;
}
return vo ;
} ) . collect ( Collectors . toList ( ) ) ;
// 按 acceptNo 进行排序并返回结果
return entrustmentIdentificationMaterialService . sortVoByAcceptNo ( voList ) ;
}
}
public Map < String , List < EntrustmentIdentificationMaterialVO > > getResultDataMap (
public Map < String , List < EntrustmentIdentificationMaterialVO > > getResultDataMap (
Integer entrustType , String oldResult , LocalDateTime startTime , LocalDateTime endTime ) {
ResultExcelDTO excelDTO ) {
LocalDateTime startTime = excelDTO . getStartTime ( ) ;
LocalDateTime endTime = excelDTO . getEndTime ( ) ;
String oldResult = excelDTO . getOldResult ( ) ;
Integer entrustType = excelDTO . getEntrustType ( ) ;
try {
try {
// 1. 构建查询条件
// 1. 构建查询条件
LambdaQueryWrapper < Entrustment > qw = new LambdaQueryWrapper < > ( ) ;
LambdaQueryWrapper < Entrustment > qw = new LambdaQueryWrapper < > ( ) ;
@@ -766,165 +762,172 @@ public class EntrustMaterialCheckoutResultServiceImpl extends ServiceImpl<Entrus
}
}
@Override
@Override
public void getExcelByResult ( HttpServletResponse response , Integer entrustType , String oldResult , LocalDateTime
public R getExcelByResult ( HttpServletResponse response , ResultExcelDTO excelDTO ) throws IOException {
startTime , LocalDateTime endTime ) throws IOException {
// 获取查询数据
List < EntrustmentIdentificationMaterialVO > resultData = this . getResultData ( entrustType , oldResult , startTime , endTime ) ;
List < EntrustmentIdentificationMaterialVO > resultData = this . getResultData ( excelDTO ) ;
Map < String , List < EntrustmentIdentificationMaterialVO > > resultDataMap = this . getResultDataMap ( entrustType , oldResult , startTime , endTime ) ;
Map < String , List < EntrustmentIdentificationMaterialVO > > resultDataMap = this . getResultDataMap ( excelDTO ) ;
LocalDateTime startTime = excelDTO . getStartTime ( ) ;
LocalDateTime endTime = excelDTO . getEndTime ( ) ;
String oldResult = excelDTO . getOldResult ( ) ;
Integer entrustType = excelDTO . getEntrustType ( ) ;
if ( resultData . isEmpty ( ) | | resultDataMap . isEmpty ( ) ) {
if ( resultData . isEmpty ( ) | | resultDataMap . isEmpty ( ) ) {
throw new RuntimeException ( " 没有符合条件的数据! " ) ;
return R . failed ( " 没有符合条件的数据! " ) ;
}
}
Workbook workbook = new XSSFWorkbook ( ) ;
Workbook workbook = new XSSFWorkbook ( ) ;
Sheet sheet = null ;
if ( entrustType = = 0 ) {
sheet = workbook . createSheet ( " 常规毒品 " ) ;
} else {
if ( StringUtils . isNotBlank ( oldResult ) ) {
sheet = workbook . createSheet ( " 生物样本- " + oldResult ) ;
} else {
sheet = workbook . createSheet ( " 生物样本 " ) ;
}
createDataSheet ( workbook , entrustType , oldResult , resultData ) ;
}
createStatisticsSheet ( workbook , resultDataMap , entrustType , oldResult , startTime , endTime ) ;
String [ ] InVivoHeadList = new String [ ] { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 年龄 " , " 性别 " , " 委托单位 " , " 受理日期 " , " 检出结果 " } ;
createDrugSheets ( workbook , resultDataMap , entrustType ) ;
String [ ] InVitroHeadList = new String [ ] { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 委托单位 " , " 受理日期 " , " 检出结果 " } ;
String [ ] headList ;
workbook . write ( response . getOutputStream ( ) ) ;
if ( entrustType = = 0 ) {
return R . ok ( " 导出成功! " ) ;
headList = InVitroHeadList ;
} else {
headList = InVivoHeadList ;
}
}
Row row = sheet . createRow ( 0 ) ;
private void createDataSheet ( Workbook workbook , Integer entrustType , String oldResult , List < EntrustmentIdentificationMaterialVO > resultData ) {
for ( int i = 0 ; i < headList . length ; i + + ) {
// 确定表名
Cell cell = row . createCell ( i ) ;
String sheetName = ( entrustType = = 0 ) ? " 常规毒品 " : ( StringUtils . isNotBlank ( oldResult ) ? " 生物样本- " + oldResult : " 生物样本 " ) ;
cell . setCellValue ( headList [ i ] ) ;
Sheet sheet = workbook . createSheet ( s heetName ) ;
// 定义表头
String [ ] inVivoHeaders = { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 年龄 " , " 性别 " , " 委托单位 " , " 受理日期 " , " 检出结果 " } ;
String [ ] inVitroHeaders = { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 委托单位 " , " 受理日期 " , " 检出结果 " } ;
String [ ] headers = ( entrustType = = 0 ) ? inVitroHeaders : inVivoHeaders ;
// 创建表头行
Row headerRow = sheet . createRow ( 0 ) ;
for ( int i = 0 ; i < headers . length ; i + + ) {
headerRow . createCell ( i ) . setCellValue ( headers [ i ] ) ;
sheet . setColumnWidth ( i , 20 * 256 ) ;
}
}
int masculineSize = 0 ;
// 填充数据
for ( int i = 0 ; i < resultData . size ( ) ; i + + ) {
for ( int i = 0 ; i < resultData . size ( ) ; i + + ) {
EntrustmentIdentificationMaterialVO vo = resultData . get ( i ) ;
EntrustmentIdentificationMaterialVO vo = resultData . get ( i ) ;
Row contentR ow = sheet . createRow ( i + 1 ) ;
Row r ow = sheet . createRow ( i + 1 ) ;
contentRow . createCell ( 0 ) . setCellValue ( vo . getOldIdentificationResult ( ) ) ;
fillRowData ( row , vo , entrustType ) ;
contentRow . createCell ( 1 ) . setCellValue ( vo . getAcceptNo ( ) ) ;
if ( CollUtil . isNotEmpty ( vo . getDrugLites ( ) ) ) {
contentRow . createCell ( 2 ) . setCellValue ( vo . getName ( ) ) ;
masculineSize + + ;
contentRow . createCell ( 3 ) . setCellValue ( vo . getFormName ( ) ) ;
contentRow . createCell ( 4 ) . setCellValue ( vo . getQuantity ( ) + vo . getUnit ( ) ) ;
if ( entrustType = = 1 ) {
contentRow . createCell ( 5 ) . setCellValue ( vo . getMaterialAge ( ) ) ;
contentRow . createCell ( 6 ) . setCellValue ( vo . getBiologyGender ( ) ) ;
contentRow . createCell ( 7 ) . setCellValue ( vo . getOrgName ( ) ) ;
contentRow . createCell ( 8 ) . setCellValue ( vo . getAcceptTime ( ) . format ( DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) ) ) ;
List < DrugLite > drugLites = vo . getDrugLites ( ) ;
if ( CollUtil . isNotEmpty ( drugLites ) ) {
StringBuilder drugName = new StringBuilder ( ) ;
for ( DrugLite drugLite : drugLites ) {
drugName . append ( drugLite . getName ( ) ) . append ( " , " ) ;
}
contentRow . createCell ( 9 ) . setCellValue ( drugName . substring ( 0 , drugName . length ( ) - 1 ) ) ;
} else {
contentRow . createCell ( 9 ) . setCellValue ( " " ) ;
}
} else {
contentRow . createCell ( 5 ) . setCellValue ( vo . getOrgName ( ) ) ;
contentRow . createCell ( 6 ) . setCellValue ( vo . getAcceptTime ( ) . format ( DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) ) ) ;
List < DrugLite > drugLites = vo . getDrugLites ( ) ;
if ( CollUtil . isNotEmpty ( drugLites ) ) {
StringBuilder drugName = new StringBuilder ( ) ;
for ( DrugLite drugLite : drugLites ) {
drugName . append ( drugLite . getName ( ) ) . append ( " , " ) ;
}
contentRow . createCell ( 7 ) . setCellValue ( drugName . substring ( 0 , drugName . length ( ) - 1 ) ) ;
} else {
contentRow . createCell ( 7 ) . setCellValue ( " " ) ;
}
}
}
}
Row lastRow = sheet . createRow ( resultData . size ( ) + 2 ) ;
lastRow . createCell ( 0 ) . setCellValue ( " 检材总数量: " + resultData . size ( ) ) ;
lastRow . createCell ( 1 ) . setCellValue ( " 阳性总数量: " + masculineSize ) ;
BigDecimal result = new BigDecimal ( masculineSize )
. multiply ( new BigDecimal ( 100 ) )
. divide ( new BigDecimal ( resultData . size ( ) ) , 2 , RoundingMode . HALF_UP ) ;
lastRow . createCell ( 2 ) . setCellValue ( " 总检出率: " + result . toString ( ) + " % " ) ;
}
}
Sheet s tatisticsSheet = workbook . createSheet ( " 统计 " ) ;
private void createS tatisticsSheet ( Workbook workbook , Map < String , List < EntrustmentIdentificationMaterialVO > > resultDataMap , Integer entrustType , String oldResult , LocalDateTime startTime , LocalDateTime endTime ) {
Sheet sheet = workbook . createSheet ( " 统计 " ) ;
Row statisticsSheetRow = statisticsSheet . createRow ( 0 ) ;
String [ ] headers = { " 化合物种类 " , " 类型 " , " 检出数量 " , " 总数量 " , " 检出率 " } ;
String [ ] statisticsHeadList = new String [ ] { " 化合物种类 " , " 类型 " , " 检出数量 " , " 总数量 " , " 检出率 " } ;
// 创建表头
Row headerRow = sheet . createRow ( 0 ) ;
for ( int i = 0 ; i < statisticsHeadList . length ; i + + ) {
for ( int i = 0 ; i < headers . length ; i + + ) {
Cell cell = statisticsSheetRow . createCell ( i ) ;
headerRow . createCell ( i ) . setCellValue ( headers [ i ] ) ;
cell . setCellValue ( statisticsHeadList [ i ] ) ;
sheet . setColumnWidth ( i , 20 * 256 ) ;
}
Set < String > drugSet = resultDataMap . keySet ( ) ;
int materialSize = 0 ;
for ( String s : drugSet ) {
materialSize + = resultDataMap . get ( s ) . size ( ) ;
}
}
// 获取总数量
Map < String , Integer > sizeMap = this . getSizeMap ( entrustType , oldResult , startTime , endTime ) ;
Map < String , Integer > sizeMap = this . getSizeMap ( entrustType , oldResult , startTime , endTime ) ;
System . out . println ( sizeMap ) ;
// 填充统计数据
int rowIndex = 1 ;
for ( String drugName : drugSet ) {
for ( Map . Entry < String, List < EntrustmentIdentificationMaterialVO > > entry : resultDataMap . entrySet ( ) ) {
List < EntrustmentIdentificationMaterialVO > materialVOList = resultDataMap . get ( drugName ) ;
String drugName = entry . getKey ( ) ;
List < EntrustmentIdentificationMaterialVO > materials = entry . getValue ( ) ;
Row row = sheet . createRow ( rowIndex + + ) ;
if ( StringUtils . isNotBlank ( oldResult ) ) {
if ( StringUtils . isNotBlank ( oldResult ) ) {
Row sheetRow = statisticsSheet . createRow ( statisticsSheet . getLastRowNum ( ) + 1 ) ;
sheetR ow. createCell ( 0 ) . setCellValue ( drugName ) ;
r ow. createCell ( 0 ) . setCellValue ( drugName ) ;
sheetR ow. createCell ( 1 ) . setCellValue ( oldResult ) ;
r ow. createCell ( 1 ) . setCellValue ( oldResult ) ;
sheetR ow. createCell ( 2 ) . setCellValue ( materialVOList . size ( ) ) ;
r ow. createCell ( 2 ) . setCellValue ( materials . size ( ) ) ;
sheetR ow. createCell ( 3 ) . setCellValue ( sizeMap . get ( oldResult ) ) ;
r ow. createCell ( 3 ) . setCellValue ( sizeMap . getOrDefault ( oldResult , 0 )) ;
sheetR ow. createCell ( 4 ) . setCellValue ( String . format ( " %.2f " , materialVOList . size ( ) * 1 . 0 / sizeMap . get ( oldResult ) * 100 ) + " % " ) ;
r ow. createCell ( 4 ) . setCellValue ( String . format ( " %.2f%% " , materials . size ( ) * 100 . 0 / sizeMap . getOrDefault ( oldResult , 1 ) ) ) ;
} else {
} else {
Map < String , List < EntrustmentIdentificationMaterialVO > > map = materialVOList . stream ( ) . collect ( Collectors . groupingBy ( EntrustmentIdentificationMaterialVO : : getOldIdentificationResult ) ) ;
Map < String , List < EntrustmentIdentificationMaterialVO > > groupedData = materials . stream ( ) . collect ( Collectors . groupingBy ( EntrustmentIdentificationMaterialVO : : getOldIdentificationResult ) ) ;
Set < String > keySet = map . k eySet( ) ;
for ( Map . Entry < String , List < EntrustmentIdentificationMaterialVO > > group : groupedData . entr ySet ( ) ) {
for ( String key : keySet ) {
row . createCell ( 0 ) . setCellValue ( drugName ) ;
System . out . println ( key ) ;
row . createCell ( 1 ) . setCellValue ( group . getKey ( ) ) ;
int size = ma p. get (key ) . size ( ) ;
row . createCell ( 2 ) . setCellValue ( grou p. getValue ( ) . size ( ) ) ;
R ow sheetRow = statisticsSheet . createRow ( statisticsSheet . getLastRowNum ( ) + 1 ) ;
r ow. createCell ( 3 ) . setCellValue ( sizeMap . getOrDefault ( group . getKey ( ) , 0 ) ) ;
sheetR ow. createCell ( 0 ) . setCellValue ( drugName ) ;
r ow. createCell ( 4 ) . setCellValue ( String . format ( " %.2f%% " , group . getValue ( ) . size ( ) * 100 . 0 / sizeMap . getOrDefault ( group . getKey ( ) , 1 ) ) ) ;
sheetRow . createCell ( 1 ) . setCellValue ( key ) ;
}
sheetRow . createCell ( 2 ) . setCellValue ( size ) ;
}
sheetRow . createCell ( 3 ) . setCellValue ( Integer . valueOf ( sizeMap . get ( key ) ) ) ;
sheetRow . createCell ( 4 ) . setCellValue ( String . format ( " %.2f " , size * 1 . 0 / sizeMap . get ( key ) * 100 ) + " % " ) ;
}
}
}
}
private void createDrugSheets ( Workbook workbook , Map < String , List < EntrustmentIdentificationMaterialVO > > resultDataMap , Integer entrustType ) {
for ( Map . Entry < String , List < EntrustmentIdentificationMaterialVO > > entry : resultDataMap . entrySet ( ) ) {
String drugName = entry . getKey ( ) . replaceAll ( " [^ \ u4e00- \ u9fa5a-zA-Z0-9] " , " " ) ;
Sheet sheet = workbook . createSheet ( drugName ) ;
// 定义表头
String [ ] headers = ( entrustType = = 0 ) ? new String [ ] { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 委托单位 " , " 受理日期 " , " 检出结果 " }
: new String [ ] { " 检材类型 " , " 检材编号 " , " 检材名称 " , " 检材性状 " , " 检材质量 " , " 年龄 " , " 性别 " , " 委托单位 " , " 受理日期 " , " 检出结果 " } ;
// 创建表头行
Row headerRow = sheet . createRow ( 0 ) ;
for ( int i = 0 ; i < headers . length ; i + + ) {
headerRow . createCell ( i ) . setCellValue ( headers [ i ] ) ;
sheet . setColumnWidth ( i , 20 * 256 ) ;
}
}
for ( String drugName : drugSet ) {
// 填充数据
List < EntrustmentIdentificationMaterialVO > materialVOList = resultDataMap . get ( drugName ) ;
int rowIndex = 1 ;
String title = drugName . replaceAll ( " [^ \\ u4e00- \\ u9fa5a-zA-Z0-9] " , " " ) ;
for ( EntrustmentIdentificationMaterialVO vo : entry . getValue ( ) ) {
sheet = workbook . createSheet ( title ) ;
Row row = sheet . createRow ( rowIndex + + ) ;
Row row0 = sheet . createRow ( 0 ) ;
fillRowData ( row, vo , entrustType ) ;
for ( int i = 0 ; i < headList . length ; i + + ) {
}
Cell cell = row0 . createCell ( i ) ;
}
cell . setCellValue ( headList [ i ] ) ;
}
}
for ( int i = 0 ; i < materialVOList . size ( ) ; i + + ) {
private void fillRowData ( Row row , EntrustmentIdentificationMaterialVO vo , Integer entrustType ) {
EntrustmentIdentificationMaterialVO vo = materialVOList . ge t ( i ) ;
row . createCell ( 0 ) . setCellValue ( vo . getOldIdentificationResul t ( ) ) ;
R ow contentRow = sheet . createRow ( i + 1 ) ;
r ow. createCell ( 1 ) . setCellValue ( vo . getAcceptNo ( ) ) ;
contentR ow . createCell ( 0 ) . setCellValue ( vo . getOldIdentificationResult ( ) ) ;
r ow . createCell ( 2 ) . setCellValue ( vo . getName ( ) ) ;
contentR ow . createCell ( 1 ) . setCellValue ( vo . getAcceptNo ( ) ) ;
r ow . createCell ( 3 ) . setCellValue ( vo . getFormName ( ) ) ;
contentR ow . createCell ( 2 ) . setCellValue ( vo . getName ( ) ) ;
r ow . createCell ( 4 ) . setCellValue ( vo . getQuantity ( ) + vo . getUnit ( ) ) ;
contentRow . createCell ( 3 ) . setCellValue ( vo . getFormName ( ) ) ;
contentRow . createCell ( 4 ) . setCellValue ( vo . getQuantity ( ) + vo . getUnit ( ) ) ;
if ( entrustType = = 1 ) {
if ( entrustType = = 1 ) {
contentR ow . createCell ( 5 ) . setCellValue ( vo . getMaterialAge ( ) ) ;
r ow . createCell ( 5 ) . setCellValue ( vo . getMaterialAge ( ) ! = null | | vo . getMaterialAge ( ) ! = 0 ? vo . getMaterialAge ( ) . toString ( ) + " 岁 " : " 未知 " ) ;
contentR ow . createCell ( 6 ) . setCellValue ( vo . getBiologyGender ( ) ) ;
r ow . createCell ( 6 ) . setCellValue ( StringUtils . isNotBlank ( vo . getBiologyGender ( ) ) ? vo . getBiologyGender ( ) : " 未知 " ) ;
contentR ow . createCell ( 7 ) . setCellValue ( vo . getOrgName ( ) ) ;
r ow . createCell ( 7 ) . setCellValue ( vo . getOrgName ( ) ) ;
contentR ow . createCell ( 8 ) . setCellValue ( vo . getAcceptTime ( ) . format ( DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) ) ) ;
r ow . createCell ( 8 ) . setCellValue ( LocalDateTimeUtil . format ( vo . getAcceptTime ( ) , " yyyy-MM-dd " ) ) ;
contentRow . createCell ( 9 ) . s etCellValue ( drugName ) ;
List < DrugLite > drugLites = vo . g etDrugLites ( ) ;
if ( drugLites ! = null & & ! drugLites . isEmpty ( ) ) {
String result = drugLites . stream ( )
. map ( DrugLite : : getName )
. collect ( Collectors . joining ( " , " ) ) ; // 自动去除最后的逗号
row . createCell ( 9 ) . setCellValue ( result ) ;
} else {
} else {
contentR ow . createCell ( 5 ) . setCellValue ( vo . getOrgName ( ) ) ;
r ow . createCell ( 9 ) . setCellValue ( " 未检出 " ) ;
contentRow . createCell ( 6 ) . setCellValue ( vo . getAcceptTime ( ) . format ( DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) ) ) ;
}
contentRow . createCell ( 7 ) . setCellValue ( drugName ) ;
} else {
row . createCell ( 5 ) . setCellValue ( vo . getOrgName ( ) ) ;
row . createCell ( 6 ) . setCellValue ( LocalDateTimeUtil . format ( vo . getAcceptTime ( ) , " yyyy-MM-dd " ) ) ;
List < DrugLite > drugLites = vo . getDrugLites ( ) ;
if ( drugLites ! = null & & ! drugLites . isEmpty ( ) ) {
String result = drugLites . stream ( )
. map ( DrugLite : : getName )
. collect ( Collectors . joining ( " , " ) ) ; // 自动去除最后的逗号
row . createCell ( 7 ) . setCellValue ( result ) ;
} else {
row . createCell ( 7 ) . setCellValue ( " 未检出 " ) ;
}
}
}
}
}
}
workbook . write ( response . getOutputStream ( ) ) ;
}
public Map < String , Integer > getSizeMap ( Integer entrustType , String oldResult , LocalDateTime startTime , LocalDateTime endTime ) {
public Map < String , Integer > getSizeMap ( Integer entrustType , String oldResult , LocalDateTime startTime , LocalDateTime endTime ) {
// 构造查询条件,查询 EntrustmentIdentificationMaterial 数据
// 构造查询条件,查询 EntrustmentIdentificationMaterial 数据
LambdaQueryWrapper < EntrustmentIdentificationMaterial > qw = new LambdaQueryWrapper < > ( ) ;
LambdaQueryWrapper < EntrustmentIdentificationMaterial > qw = new LambdaQueryWrapper < > ( ) ;
LocalDateTime queryStartTime = Optional . ofNullable ( startTime ) . orElseGet ( ( ) - > this . getStartTime ( null ) ) ;
LocalDateTime queryStartTime = Optional . ofNullable ( startTime ) . orElseGet ( ( ) - > this . getStartTime ( null ) ) ;
if ( endTime ! = null ) {
if ( endTime ! = null ) {
qw . between ( EntrustmentIdentificationMaterial : : getAcceptTime , queryStartTime , endTime ) ;
qw . between ( EntrustmentIdentificationMaterial : : getAcceptTime , queryStartTime , endTime ) ;
} else {
} else {