@ -2,12 +2,17 @@ package com.epmet.datareport.controller.screen;
import cn.afterturn.easypoi.excel.ExcelExportUtil ;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams ;
import com.alibaba.excel.EasyExcel ;
import com.alibaba.excel.ExcelWriter ;
import com.alibaba.excel.write.metadata.WriteSheet ;
import com.epmet.commons.tools.annotation.LoginUser ;
import com.epmet.commons.tools.constant.NumConstant ;
import com.epmet.commons.tools.constant.StrConstant ;
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult ;
import com.epmet.commons.tools.redis.common.CustomerStaffRedis ;
import com.epmet.commons.tools.security.dto.TokenDto ;
import com.epmet.commons.tools.utils.ConvertUtils ;
import com.epmet.commons.tools.utils.ExcelUtils ;
import com.epmet.commons.tools.utils.Result ;
import com.epmet.commons.tools.validator.ValidatorUtils ;
import com.epmet.datareport.service.evaluationindex.screen.ScreenProjectOrgDailyService ;
@ -29,10 +34,12 @@ import com.epmet.project.dto.result.ProjectCategoryResultDTO;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.poi.ss.usermodel.Workbook ;
import org.jetbrains.annotations.NotNull ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.* ;
import javax.servlet.http.HttpServletResponse ;
import java.io.InputStream ;
import java.net.URLEncoder ;
import java.util.ArrayList ;
import java.util.HashMap ;
@ -195,20 +202,13 @@ public class ScreenProjectController {
* @date 2021 / 11 / 4 3 : 38 下午
* /
@PostMapping ( "selectprojectcategory/export" )
public void selectProjectCategoryExport ( @RequestBody ProjectCategoryFormDTO formDTO , /*@LoginUser*/ TokenDto tokenDto , HttpServletResponse response ) throws Exception {
tokenDto . setUserId ( "36bc0fb38565ecdebf8ab9b476b44548" ) ;
tokenDto . setCustomerId ( "45687aa479955f9d06204d415238f7cc" ) ;
ValidatorUtils . validateEntity ( formDTO , ProjectCategoryFormDTO . ProjectCategory Form. class ) ;
public void selectProjectCategoryExport ( @RequestBody ProjectCategoryFormDTO formDTO , @LoginUser TokenDto tokenDto , HttpServletResponse response ) throws Exception {
//tokenDto.setUserId("36bc0fb38565ecdebf8ab9b476b44548");
//tokenDto.setCustomerId("45687aa479955f9d06204d415238f7cc");
ValidatorUtils . validateEntity ( formDTO , ProjectCategoryFormDTO . CategoryProjectExport Form. class ) ;
List < ProjectCategoryResultDTO > data = screenProjectService . selectProjectCategory ( formDTO , tokenDto ) ;
String templatePath = "excel/project_category_temp.xlsx" ;
StringBuilder dateBuilder = new StringBuilder ( ) ;
if ( StringUtils . isNotBlank ( formDTO . getStartTime ( ) ) ) {
dateBuilder . append ( formDTO . getStartTime ( ) ) . append ( StrConstant . HYPHEN ) ;
}
if ( StringUtils . isNotBlank ( formDTO . getEndTime ( ) ) ) {
dateBuilder . append ( formDTO . getEndTime ( ) ) ;
}
List < ProjectCategoryResultDTO > resultDTOList = new ArrayList < > ( ) ;
data . forEach ( e - > {
resultDTOList . add ( e ) ;
@ -219,7 +219,7 @@ public class ScreenProjectController {
Map < String , Object > mapData = new HashMap < > ( ) ;
mapData . put ( "list" , resultDTOList ) ;
mapData . put ( "orgName" , staffInfo . getAgencyName ( ) ) ;
mapData . put ( "exportDate" , dateBuilder . toString ( ) ) ;
mapData . put ( "exportDate" , getExportDateStr ( formDTO . getStartTime ( ) , formDTO . getEndTime ( ) ) ) ;
Workbook workbook = ExcelExportUtil . exportExcel ( new TemplateExportParams ( templatePath ) , mapData ) ;
response . setHeader ( "content-Type" , "application/vnd.ms-excel" ) ;
response . addHeader ( "Content-Disposition" , "attachment;fileName=" + URLEncoder . encode ( "项目分类统计.xls" , "UTF-8" ) ) ;
@ -239,4 +239,76 @@ public class ScreenProjectController {
return new Result < PageCategoryProjectListResultDTO > ( ) . ok ( screenProjectService . selectCategoryProjectList ( formDTO , tokenDto ) ) ;
}
/ * *
* @Description 【 项目分类 】 查询项目分类
* @param formDTO
* @param tokenDto
* @author zxc
* @date 2021 / 11 / 4 3 : 38 下午
* /
@PostMapping ( "selectcategoryprojectlist/export" )
public void selectProjectCategoryExport ( @RequestBody CategoryProjectListFormDTO formDTO , @LoginUser TokenDto tokenDto , HttpServletResponse response ) throws Exception {
ExcelWriter excelWriter = null ;
try {
//tokenDto.setUserId("36bc0fb38565ecdebf8ab9b476b44548");
//tokenDto.setCustomerId("45687aa479955f9d06204d415238f7cc");
formDTO . setPageNo ( NumConstant . ONE ) ;
formDTO . setPageSize ( NumConstant . ONE_THOUSAND ) ;
formDTO . setIsPage ( false ) ;
ValidatorUtils . validateEntity ( formDTO , ProjectCategoryFormDTO . CategoryProjectExportForm . class ) ;
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis . getStaffInfo ( tokenDto . getCustomerId ( ) , tokenDto . getUserId ( ) ) ;
Map < String , Object > mapData = new HashMap < > ( ) ;
mapData . put ( "orgName" , staffInfo = = null ? StrConstant . EPMETY_STR : staffInfo . getAgencyName ( ) ) ;
mapData . put ( "exportDate" , getExportDateStr ( formDTO . getStartTime ( ) , formDTO . getEndTime ( ) ) ) ;
mapData . put ( "categoryName" , formDTO . getCategoryName ( ) ) ;
if ( StringUtils . isNotBlank ( formDTO . getParentCategoryName ( ) ) ) {
mapData . put ( "categoryName" , formDTO . getParentCategoryName ( ) . concat ( StrConstant . HYPHEN ) . concat ( formDTO . getCategoryName ( ) ) ) ;
}
String templatePath = "excel/project_temp_easy_excel.xlsx" ;
InputStream inputStream = this . getClass ( ) . getClassLoader ( ) . getResourceAsStream ( templatePath ) ;
//String template = this.getClass().getResource(templatePath).getPath();
String fileName = "项目统计.xlsx" ;
excelWriter = EasyExcel . write ( ExcelUtils . getOutputStream ( fileName , response ) ) . withTemplate ( inputStream ) . build ( ) ;
WriteSheet writeSheet = EasyExcel . writerSheet ( ) . build ( ) ;
excelWriter . fill ( mapData , writeSheet ) ;
PageCategoryProjectListResultDTO data = null ;
do {
data = screenProjectService . selectCategoryProjectList ( formDTO , tokenDto ) ;
if ( data . getList ( ) . isEmpty ( ) ) {
break ;
}
formDTO . setPageNo ( formDTO . getPageNo ( ) + 1 ) ;
excelWriter . fill ( data . getList ( ) , writeSheet ) ;
} while ( data . getList ( ) . size ( ) = = formDTO . getPageSize ( ) ) ;
// String templatePath = "excel/project_temp.xlsx";
// Workbook workbook = ExcelExportUtil.exportExcel(new TemplateExportParams(templatePath), mapData);
// response.setHeader("content-Type", "application/vnd.ms-excel");
// response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("项目统计.xls", "UTF-8"));
// workbook.write(response.getOutputStream());
} finally {
if ( excelWriter ! = null ) {
excelWriter . finish ( ) ;
}
}
}
@NotNull
private String getExportDateStr ( String startTime , String endTime ) {
StringBuilder dateBuilder = new StringBuilder ( ) ;
if ( StringUtils . isNotBlank ( startTime ) ) {
dateBuilder . append ( startTime ) . append ( StrConstant . HYPHEN ) ;
}
if ( StringUtils . isNotBlank ( endTime ) ) {
dateBuilder . append ( endTime ) ;
}
return dateBuilder . toString ( ) ;
}
}