@ -2,6 +2,8 @@ package com.mathvision.diet.service;
import com.alibaba.excel.EasyExcel ;
import com.alibaba.excel.EasyExcel ;
import com.alibaba.excel.support.ExcelTypeEnum ;
import com.alibaba.excel.support.ExcelTypeEnum ;
import com.alibaba.excel.write.metadata.style.WriteCellStyle ;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy ;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy ;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy ;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy ;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy ;
import com.google.common.collect.Lists ;
import com.google.common.collect.Lists ;
@ -13,12 +15,14 @@ import com.mathvision.diet.repository.MenuDishRepository;
import lombok.extern.slf4j.Slf4j ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.collections4.CollectionUtils ;
import org.apache.commons.collections4.CollectionUtils ;
import org.apache.commons.lang3.tuple.Pair ;
import org.apache.commons.lang3.tuple.Pair ;
import org.apache.poi.ss.usermodel.BorderStyle ;
import org.apache.poi.ss.usermodel.FillPatternType ;
import org.apache.poi.ss.usermodel.IndexedColors ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import javax.annotation.Resource ;
import javax.annotation.Resource ;
import java.io.OutputStream ;
import java.io.OutputStream ;
import java.math.BigDecimal ;
import java.util.* ;
import java.util.* ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import java.util.stream.IntStream ;
import java.util.stream.IntStream ;
@ -110,7 +114,7 @@ public class MenuDishService {
List < Pair < String , MenuDishItemDTO > > items = dishes . stream ( ) . map ( dish - > {
List < Pair < String , MenuDishItemDTO > > items = dishes . stream ( ) . map ( dish - > {
List < MenuDishItemDTO > item = dish . getIngredient ( ) ;
List < MenuDishItemDTO > item = dish . getIngredient ( ) ;
if ( item = = null | | item . size ( ) < itemIndex ) {
if ( item = = null | | item . size ( ) < itemIndex ) {
return Pair . of ( dish . getName ( ) , MenuDishItemDTO . builder ( ) . value ( Maps . newHashMap ( ) ) . build ( ) ) ;
return Pair . of ( "" , MenuDishItemDTO . builder ( ) . value ( Maps . newHashMap ( ) ) . build ( ) ) ;
} else {
} else {
return Pair . of ( dish . getName ( ) , item . get ( itemIndex - 1 ) ) ;
return Pair . of ( dish . getName ( ) , item . get ( itemIndex - 1 ) ) ;
}
}
@ -121,20 +125,27 @@ public class MenuDishService {
items . forEach ( item - > {
items . forEach ( item - > {
content . add ( item . getKey ( ) ) ;
content . add ( item . getKey ( ) ) ;
content . add ( keyName . getOrDefault ( item . getValue ( ) . getKey ( ) , item . getValue ( ) . getKey ( ) ) ) ;
content . add ( keyName . getOrDefault ( item . getValue ( ) . getKey ( ) , item . getValue ( ) . getKey ( ) ) ) ;
allCrows . forEach ( crow - > {
allCrows . forEach ( crow - > content . add ( item . getValue ( ) . getValue ( ) . get ( crow ) ) ) ;
BigDecimal value = item . getValue ( ) . getValue ( ) . get ( crow ) ;
if ( value ! = null ) {
content . add ( value ) ;
}
} ) ;
} ) ;
} ) ;
contents . add ( content ) ;
contents . add ( content ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
WriteCellStyle headWriteCellStyle = new WriteCellStyle ( ) ;
headWriteCellStyle . setFillPatternType ( FillPatternType . SOLID_FOREGROUND ) ;
headWriteCellStyle . setFillForegroundColor ( IndexedColors . PALE_BLUE . getIndex ( ) ) ;
WriteCellStyle contentWriteCellStyle = new WriteCellStyle ( ) ;
contentWriteCellStyle . setBorderLeft ( BorderStyle . THIN ) ;
contentWriteCellStyle . setBorderTop ( BorderStyle . THIN ) ;
contentWriteCellStyle . setBorderRight ( BorderStyle . THIN ) ;
contentWriteCellStyle . setBorderBottom ( BorderStyle . THIN ) ;
EasyExcel . write ( outputStream ) . head ( headers )
EasyExcel . write ( outputStream ) . head ( headers )
. registerWriteHandler ( new HorizontalCellStyleStrategy ( headWriteCellStyle , contentWriteCellStyle ) )
. registerWriteHandler ( new SimpleColumnWidthStyleStrategy ( 25 ) )
. registerWriteHandler ( new SimpleColumnWidthStyleStrategy ( 25 ) )
. registerWriteHandler ( new SimpleRowHeightStyleStrategy ( ( short ) 30 , ( short ) 20 ) )
. registerWriteHandler ( new SimpleRowHeightStyleStrategy ( ( short ) 30 , ( short ) 20 ) )
. excelType ( ExcelTypeEnum . XLSX ) . sheet ( menu . getName ( ) ) . doWrite ( contents ) ;
. excelType ( ExcelTypeEnum . XLSX ) . sheet ( menu . getName ( ) ) . doWrite ( contents ) ;
}
}
}
}