|
|
|
@ -1,30 +1,47 @@ |
|
|
|
package com.epmet.dataaggre.controller; |
|
|
|
|
|
|
|
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.annotation.RequirePermission; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.enums.RequirePermissionEnum; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.EpmetException; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
import com.epmet.commons.tools.security.dto.TokenDto; |
|
|
|
import com.epmet.commons.tools.security.user.LoginUserUtil; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
|
|
import com.epmet.dataaggre.dto.govorg.CustomerAgencyDTO; |
|
|
|
import com.epmet.dataaggre.beans.GridMemberDataAnalysisExcelExportBean; |
|
|
|
import com.epmet.dataaggre.dto.govorg.CustomerGridDTO; |
|
|
|
import com.epmet.dataaggre.dto.govorg.form.*; |
|
|
|
import com.epmet.dataaggre.dto.govorg.result.*; |
|
|
|
import com.epmet.dataaggre.entity.govorg.CustomerAgencyEntity; |
|
|
|
import com.epmet.dataaggre.entity.govorg.CustomerGridEntity; |
|
|
|
import com.epmet.dataaggre.enums.GridMemberDataAnalysisEnums; |
|
|
|
import com.epmet.dataaggre.service.AggreGridService; |
|
|
|
import com.epmet.dataaggre.service.govorg.GovOrgService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Author zxc |
|
|
|
@ -32,6 +49,7 @@ import java.util.List; |
|
|
|
*/ |
|
|
|
@RestController |
|
|
|
@RequestMapping("org") |
|
|
|
@Slf4j |
|
|
|
public class GovOrgController { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@ -43,6 +61,24 @@ public class GovOrgController { |
|
|
|
@Autowired |
|
|
|
private AggreGridService aggreGridService; |
|
|
|
|
|
|
|
/** |
|
|
|
* 导出路径 |
|
|
|
*/ |
|
|
|
private Path gridMemberStatsExcelExportDir; |
|
|
|
|
|
|
|
{ |
|
|
|
// 初始化目录
|
|
|
|
String home = System.getProperty("user.home"); |
|
|
|
Path importDir = Paths.get(home, "epmet_files", "grid_member_stats_excel_export"); |
|
|
|
if (Files.notExists(importDir)) { |
|
|
|
try { |
|
|
|
Files.createDirectories(importDir); |
|
|
|
} catch (IOException e) { |
|
|
|
log.error("创建导出暂存目录失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
gridMemberStatsExcelExportDir = importDir; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param tokenDTO |
|
|
|
@ -201,4 +237,122 @@ public class GovOrgController { |
|
|
|
return new Result<CustomerGridDTO>().ok(govOrgService.getGridInfo(gridId)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* pc 工作端,网格员巡查统计 |
|
|
|
* @param input |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@PostMapping("pcwork/gridmember-analysis") |
|
|
|
public Result getGridMemberAnalysis4PcWork(@RequestBody GridMemberDataAnalysisFromDTO input) { |
|
|
|
ValidatorUtils.validateEntity(input, GridMemberDataAnalysisFromDTO.listGridMemberDatas4PcWork.class); |
|
|
|
|
|
|
|
String orgId = input.getOrgId(); |
|
|
|
Date startTime = input.getStartTime(); |
|
|
|
Date endTime = input.getEndTime(); |
|
|
|
Integer pageNo = input.getPageNo(); |
|
|
|
Integer pageSize = input.getPageSize(); |
|
|
|
String orgType = input.getOrgType(); |
|
|
|
|
|
|
|
PageData<GridMemberDataAnalysisResultDTO> page = aggreGridService.getGridMemberDataAnalysis4PcWork(orgId, orgType, pageNo, pageSize, startTime, endTime, true); |
|
|
|
return new Result().ok(page); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* pc 工作端,网格员巡查统计,导出excel |
|
|
|
* @param input |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@PostMapping("pcwork/gridmember-analysis/export") |
|
|
|
public void exportGridMemberAnalysis4PcWork(@RequestBody GridMemberDataAnalysisFromDTO input, HttpServletResponse response) { |
|
|
|
ValidatorUtils.validateEntity(input, GridMemberDataAnalysisFromDTO.listGridMemberDatas4PcWork.class); |
|
|
|
|
|
|
|
String orgId = input.getOrgId(); |
|
|
|
Date startTime = input.getStartTime(); |
|
|
|
Date endTime = input.getEndTime(); |
|
|
|
String orgType = input.getOrgType(); |
|
|
|
|
|
|
|
// 缓存路径
|
|
|
|
String tempFileName = UUID.randomUUID().toString().concat(".xlsx"); |
|
|
|
Path tempFilePath = gridMemberStatsExcelExportDir.resolve(tempFileName); |
|
|
|
log.info("工作统计excel文件下载路径:{}", tempFilePath.toString()); |
|
|
|
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
|
|
|
// 组织名称
|
|
|
|
AtomicReference<String> orgName = new AtomicReference<>(""); |
|
|
|
|
|
|
|
if ("grid".equals(orgType)) { |
|
|
|
CustomerGridDTO gridInfo = govOrgService.getGridInfo(orgId); |
|
|
|
Optional.ofNullable(gridInfo).ifPresent((g) -> orgName.set(g.getGridName())); |
|
|
|
} else if ("agency".equals(orgType)) { |
|
|
|
CustomerAgencyEntity agencyInfo = govOrgService.getAgencyInfo(orgId); |
|
|
|
Optional.ofNullable(agencyInfo).ifPresent((a) -> orgName.set(a.getOrganizationName())); |
|
|
|
} |
|
|
|
|
|
|
|
// sheet名
|
|
|
|
String sheetName = getSheetName(orgName.get(), startTime, endTime); |
|
|
|
|
|
|
|
// 分页导出到缓存excel
|
|
|
|
try { |
|
|
|
excelWriter = EasyExcel.write(tempFilePath.toString(), GridMemberDataAnalysisExcelExportBean.class).build(); |
|
|
|
// 这里注意 如果同一个sheet只要创建一次
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build(); |
|
|
|
|
|
|
|
PageData<GridMemberDataAnalysisResultDTO> page; |
|
|
|
|
|
|
|
int pageNo = NumConstant.ONE; |
|
|
|
int pageSize = NumConstant.ONE_THOUSAND; |
|
|
|
|
|
|
|
do { |
|
|
|
page = aggreGridService.getGridMemberDataAnalysis4PcWork(orgId, orgType, pageNo++, pageSize, startTime, endTime, false); |
|
|
|
List<GridMemberDataAnalysisResultDTO> pageList = page.getList(); |
|
|
|
|
|
|
|
List<GridMemberDataAnalysisExcelExportBean> exportBeans = ConvertUtils.sourceToTarget(pageList, GridMemberDataAnalysisExcelExportBean.class); |
|
|
|
|
|
|
|
excelWriter.write(exportBeans, writeSheet); |
|
|
|
|
|
|
|
} while (page.getList().size() == pageSize); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("【PC工作端网格员数据统计导出】生成excel失败:{}", ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} finally { |
|
|
|
// 关闭流
|
|
|
|
Optional.ofNullable(excelWriter).ifPresent((i) -> i.finish()); |
|
|
|
} |
|
|
|
|
|
|
|
// 开始下载缓存excel到客户端,然后删除缓存文件
|
|
|
|
try { |
|
|
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
|
|
|
response.setHeader("Content-Type", "application/vnd.ms-excel"); |
|
|
|
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(sheetName.concat(".xlsx"), "UTF-8")); |
|
|
|
|
|
|
|
IOUtils.copy(new FileInputStream(tempFilePath.toString()), response.getOutputStream()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "【下载工作统计信息】下载失败"); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
Files.delete(tempFilePath); |
|
|
|
} catch (IOException e) { |
|
|
|
log.error(ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "【下载工作统计信息】删除缓存文件失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String getSheetName(String orgName, Date startTime, Date endTime) { |
|
|
|
StringBuilder sb = new StringBuilder(orgName); |
|
|
|
String s = orgName; |
|
|
|
if (startTime == null && endTime != null) { |
|
|
|
sb.append("截止").append(DateUtils.format(endTime, DateUtils.DATE_PATTERN)); |
|
|
|
} else if (startTime != null && endTime != null) { |
|
|
|
sb.append(DateUtils.format(startTime, DateUtils.DATE_PATTERN)) |
|
|
|
.append("至") |
|
|
|
.append(DateUtils.format(endTime, DateUtils.DATE_PATTERN)) |
|
|
|
.append("区间新增值"); |
|
|
|
} |
|
|
|
sb.append("的工作统计"); |
|
|
|
return sb.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|