Commit 34e2c15a authored by alex yao's avatar alex yao

feat: API积分数据分析+导出

parent 30447a39
......@@ -10,93 +10,7 @@ public interface FormReportingConstant {
* 报表类型
*/
interface REPORT_TYPE {
/**
* 兑换记录
*/
public String EXCHANGE_LOG = "exchangeLog";
/**
* 积分兑换
*/
public String INTERNAL_EXCHANGE = "internalExchange";
/**
* 会员
*/
public String MEMBER = "member";
/**
* 员工
*/
public String STAFF = "staff";
/**
* vip预约
*/
public String RESERVATION = "vipReservation";
/**
* 员工排版
*/
public String STAFF_CLASSES = "staffClasses";
/**
* 公告
*/
public String NOTICE = "notice";
/**
* 店铺
*/
public String STORE = "store";
/**
* 店铺员工打卡
*/
public String STAFF_CLOCK_DATA = "staffClockData";
//申请管理
/**
* 出门申请
*/
public String OUT_APPLY = "Out";
/**
* 活动申请
*/
public String ACTIVITY_APPLY = "Activity";
/**
* 加班申请
*/
public String OT_APPLY = "Ot";
/**
* 广播申请
*/
public String BROADCAST_APPLY = "Broadcast";
//营业额管理
/**
* 营业额审批
*/
public String SALE_DATA = "SaleData";
/**
* 营业数据
*/
public String BUSINESS_DATA = "businessData";
/**
* 问卷活动报表
*/
public String MARKETING_GAME_ACTIVITY = "marketingGameActivity";
//束流数据
/**
* 客流数据
*/
public String CUSTOMER_FLOW = "customerFlow";
/**
* 活动数据
*/
public String ORDER_DATA = "orderData";
/**
* B端投票活动候选人名单
*/
public String B_PORT_CANDIDATE_LIST = "bPortCandidateList";
String API_CHANNEL_POINT_USAGE = "API_CHANNEL_POINT_USAGE";
}
......
package cn.com.poc.data_analyze.entity;
/**
* @author alex.yao
* @date 2024/12/20
*/
public class AgentDataAnalyzeApiChannelEntity {
private String dateTime;
private Double count;
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public Double getCount() {
return count;
}
public void setCount(Double count) {
this.count = count;
}
}
select
id,
relation_id,
`type`,
member_id,
point_num,
channel,
`datetime`
from
biz_data_analyze_point_record
where 1=1
<< AND relation_id = :relationId >>
<< AND channel IN (:channels) >>
<< AND datetime >= :startDate AND datetime <= :endDate >>
order by datetime desc
\ No newline at end of file
package cn.com.poc.data_analyze.query;
import java.io.Serializable;
import java.util.List;
/**
* Query Condition class for DataAnalyzePointRecordQuery
*/
public class DataAnalyzePointRecordQueryCondition implements Serializable{
private static final long serialVersionUID = 1L;
private java.lang.String relationId;
public java.lang.String getRelationId(){
return this.relationId;
}
public void setRelationId(java.lang.String relationId){
this.relationId = relationId;
}
private List<String> channels;
public List<String> getChannels(){
return this.channels;
}
public void setChannels(List<String> channels){
this.channels = channels;
}
private java.lang.String startDate;
public java.lang.String getStartDate(){
return this.startDate;
}
public void setStartDate(java.lang.String startDate){
this.startDate = startDate;
}
private java.lang.String endDate;
public java.lang.String getEndDate(){
return this.endDate;
}
public void setEndDate(java.lang.String endDate){
this.endDate = endDate;
}
}
\ No newline at end of file
package cn.com.poc.data_analyze.query;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import cn.com.yict.framemax.data.model.BaseItemClass;
/**
* Query Item class for DataAnalyzePointRecordQuery
*/
@Entity
public class DataAnalyzePointRecordQueryItem extends BaseItemClass implements Serializable{
private static final long serialVersionUID = 1L;
/** id
*id
*/
private java.lang.Long id;
@Column(name = "id")
public java.lang.Long getId(){
return this.id;
}
public void setId(java.lang.Long id){
this.id = id;
}
/** relation_id
*relation_id
*/
private java.lang.String relationId;
@Column(name = "relation_id")
public java.lang.String getRelationId(){
return this.relationId;
}
public void setRelationId(java.lang.String relationId){
this.relationId = relationId;
}
/** type
*type
*/
private java.lang.String type;
@Column(name = "type")
public java.lang.String getType(){
return this.type;
}
public void setType(java.lang.String type){
this.type = type;
}
/** member_id
*member_id
*/
private java.lang.Long memberId;
@Column(name = "member_id")
public java.lang.Long getMemberId(){
return this.memberId;
}
public void setMemberId(java.lang.Long memberId){
this.memberId = memberId;
}
/** point_num
*point_num
*/
private java.lang.Double pointNum;
@Column(name = "point_num")
public java.lang.Double getPointNum(){
return this.pointNum;
}
public void setPointNum(java.lang.Double pointNum){
this.pointNum = pointNum;
}
/** channel
*channel
*/
private java.lang.String channel;
@Column(name = "channel")
public java.lang.String getChannel(){
return this.channel;
}
public void setChannel(java.lang.String channel){
this.channel = channel;
}
/** datetime
*datetime
*/
private java.util.Date datetime;
@Column(name = "datetime")
public java.util.Date getDatetime(){
return this.datetime;
}
public void setDatetime(java.util.Date datetime){
this.datetime = datetime;
}
}
\ No newline at end of file
package cn.com.poc.data_analyze.service;
import cn.com.poc.data_analyze.query.ChannelPointsCountQueryCondition;
import cn.com.poc.data_analyze.query.ChannelPointsCountQueryItem;
import cn.com.poc.data_analyze.query.PointsUsageQueryCondition;
import cn.com.poc.data_analyze.query.PointsUsageQueryItem;
import cn.com.poc.data_analyze.query.*;
import cn.com.yict.framemax.core.service.BaseService;
import cn.com.poc.data_analyze.entity.BizDataAnalyzePointRecordEntity;
import cn.com.yict.framemax.data.model.PagingInfo;
......@@ -26,4 +23,5 @@ public interface BizDataAnalyzePointRecordService extends BaseService {
List<ChannelPointsCountQueryItem> channelPointsCountQuery(ChannelPointsCountQueryCondition condition, PagingInfo pagingInfo);
List<DataAnalyzePointRecordQueryItem> dataAnalyzePointRecordQuery(DataAnalyzePointRecordQueryCondition condition, PagingInfo pagingInfo);
}
\ No newline at end of file
......@@ -3,10 +3,7 @@ package cn.com.poc.data_analyze.service.impl;
import cn.com.poc.data_analyze.convert.BizDataAnalyzePointRecordConvert;
import cn.com.poc.data_analyze.entity.BizDataAnalyzePointRecordEntity;
import cn.com.poc.data_analyze.model.BizDataAnalyzePointRecordModel;
import cn.com.poc.data_analyze.query.ChannelPointsCountQueryCondition;
import cn.com.poc.data_analyze.query.ChannelPointsCountQueryItem;
import cn.com.poc.data_analyze.query.PointsUsageQueryCondition;
import cn.com.poc.data_analyze.query.PointsUsageQueryItem;
import cn.com.poc.data_analyze.query.*;
import cn.com.poc.data_analyze.repository.BizDataAnalyzePointRecordRepository;
import cn.com.poc.data_analyze.service.BizDataAnalyzePointRecordService;
import cn.com.yict.framemax.core.service.impl.BaseServiceImpl;
......@@ -101,4 +98,9 @@ public class BizDataAnalyzePointRecordServiceImpl extends BaseServiceImpl
public List<ChannelPointsCountQueryItem> channelPointsCountQuery(ChannelPointsCountQueryCondition condition, PagingInfo pagingInfo) {
return this.sqlDao.query(condition, ChannelPointsCountQueryItem.class, pagingInfo);
}
@Override
public List<DataAnalyzePointRecordQueryItem> dataAnalyzePointRecordQuery(DataAnalyzePointRecordQueryCondition condition, PagingInfo pagingInfo) {
return this.sqlDao.query(condition, DataAnalyzePointRecordQueryItem.class, pagingInfo);
}
}
\ No newline at end of file
......@@ -55,4 +55,12 @@ public interface AgentDataAnalyzeService {
*/
List<AgentDataAnalyzeTrendEntity> getAgentDataTrend(String agentId, List<String> channel, DataAnalyzeTimeRange timeRange);
/**
* 获取Agent应用API渠道积分使用情况-秒级
* @param agentId
* @param timeRange
* @return
*/
List<AgentDataAnalyzeApiChannelEntity> getAgentApiChannelPointCount(String agentId, DataAnalyzeTimeRange timeRange);
}
......@@ -4,6 +4,7 @@ import cn.com.poc.agent_application.aggregate.AgentApplicationInfoService;
import cn.com.poc.common.utils.Assert;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.data_analyze.constants.DataAnalyzeChannelEnum;
import cn.com.poc.data_analyze.constants.DataAnalyzeTimeDimensionEnum;
import cn.com.poc.data_analyze.constants.DataAnalyzeTimeRangeEnum;
import cn.com.poc.data_analyze.domain.ChannelDataCount;
......@@ -164,7 +165,59 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
}
private static void buildGeneralData(DataAnalyzeTimeDimensionEnum dataAnalyzeTimeDimensionEnum, List<PointsUsageQueryItem> pointsUsageQueryItems, List<AgentUsageQueryItem> agentUsageQueryItems, List<AgentDataAnalyzeGeneralEntity> result) {
@Override
public List<AgentDataAnalyzeApiChannelEntity> getAgentApiChannelPointCount(String agentId, DataAnalyzeTimeRange timeRange) {
String rangType = timeRange.getRangType();
String startTime = "";
String endTime = "";
Date endDate;
Date startDate;
switch (DataAnalyzeTimeRangeEnum.getByType(rangType)) {
case week:
endDate = DateUtils.getTodayEnd();
startDate = DateUtils.getDayStartTime(DateUtils.addDays(endDate, -6));
startTime = DateUtils.formatDate(startDate, DateUtils.yyyy_MM_dd);
endTime = DateUtils.formatDate(endDate, DateUtils.yyyy_MM_dd_HH_mm_ss);
break;
case month:
endDate = DateUtils.getTodayEnd();
startDate = DateUtils.getDayStartTime(DateUtils.addMonth(endDate, -1));
startTime = DateUtils.formatDate(startDate, DateUtils.yyyy_MM_dd);
endTime = DateUtils.formatDate(endDate, DateUtils.yyyy_MM_dd_HH_mm_ss);
break;
case customize:
Assert.notBlank(timeRange.getStartTime());
Assert.notBlank(timeRange.getEndTime());
startTime = timeRange.getStartTime();
endDate = DateUtils.getDayEnd(DateUtils.stringToDateShort(timeRange.getEndTime()));
endTime = DateUtils.formatDate(endDate, DateUtils.yyyy_MM_dd_HH_mm_ss);
break;
default:
throw new I18nMessageException("data-analyze/not.support.rang.type");
}
List<String> channels = new ArrayList<String>() {{
add(DataAnalyzeChannelEnum.api.getChannel());
}};
DataAnalyzePointRecordQueryCondition condition = new DataAnalyzePointRecordQueryCondition();
condition.setRelationId(agentId);
condition.setChannels(channels);
condition.setStartDate(startTime);
condition.setEndDate(endTime);
List<DataAnalyzePointRecordQueryItem> dataAnalyzePointRecordQueryItems = bizDataAnalyzePointRecordService.dataAnalyzePointRecordQuery(condition, null);
List<AgentDataAnalyzeApiChannelEntity> result = new ArrayList<>();
if (CollectionUtils.isNotEmpty(dataAnalyzePointRecordQueryItems)) {
for (DataAnalyzePointRecordQueryItem item : dataAnalyzePointRecordQueryItems) {
AgentDataAnalyzeApiChannelEntity agentDataAnalyzeApiChannelEntity = new AgentDataAnalyzeApiChannelEntity();
agentDataAnalyzeApiChannelEntity.setDateTime(DateUtils.formatDate(item.getDatetime(), DateUtils.yyyy_MM_dd_HH_mm_ss));
agentDataAnalyzeApiChannelEntity.setCount(item.getPointNum());
result.add(agentDataAnalyzeApiChannelEntity);
}
}
return result;
}
private static void buildGeneralData(DataAnalyzeTimeDimensionEnum
dataAnalyzeTimeDimensionEnum, List<PointsUsageQueryItem> pointsUsageQueryItems, List<AgentUsageQueryItem> agentUsageQueryItems, List<AgentDataAnalyzeGeneralEntity> result) {
DataAnalyzeInfo dataAnalyzeInfo = new DataAnalyzeInfo();
//积分
DataAnalyze pointsCount = new DataAnalyze();
......@@ -186,7 +239,8 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
}
private static void setUsageDataAnalyze(DataAnalyze dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
private static void setUsageDataAnalyze(DataAnalyze
dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -208,7 +262,8 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
dataAnalyze.setFluctuate(fluctuate);
}
private static void setUserCountDataAnalyze(DataAnalyze dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
private static void setUserCountDataAnalyze(DataAnalyze
dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -230,7 +285,8 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
dataAnalyze.setFluctuate(fluctuate);
}
private static void setPointsDataAnalyze(List<PointsUsageQueryItem> pointsUsageQueryItems, DataAnalyze dataAnalyze, DataAnalyzeTimeDimensionEnum dimension) {
private static void setPointsDataAnalyze(List<PointsUsageQueryItem> pointsUsageQueryItems, DataAnalyze
dataAnalyze, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -253,7 +309,8 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
}
private void channelUserConvert(String agentId, List<String> channel, Date currDateTime, List<ChannelUsersCountEntity> result) {
private void channelUserConvert(String agentId, List<String> channel, Date
currDateTime, List<ChannelUsersCountEntity> result) {
Date thisWeekBegin = DateUtils.getWeekBegin2(currDateTime);
Date thisWeekEnd = DateUtils.addDays(DateUtils.getWeekEnd2(currDateTime), -1);
ChannelUserCountQueryCondition thisWeekCondition = new ChannelUserCountQueryCondition();
......
......@@ -109,14 +109,14 @@ public class AgentDataAnalyzeRestImpl implements AgentDataAnalyzeRest {
@Override
public List<AgentDataAnalyzeApiChannelDto> getApiChannelPointUsageCount(GetAgentDataApiChannelRequestDto dto) {
checkPermission(dto.getAgentId());
List<String> channels = ListUtil.of(DataAnalyzeChannelEnum.api.getChannel());
List<AgentDataAnalyzeTrendEntity> analyzeTrendEntities = agentDataAnalyzeService.getAgentDataTrend(dto.getAgentId(), channels, dto.getTimeRange());
List<AgentDataAnalyzeApiChannelEntity> analyzeApiChannelEntities = agentDataAnalyzeService.getAgentApiChannelPointCount(dto.getAgentId(), dto.getTimeRange());
List<AgentDataAnalyzeApiChannelDto> result = new ArrayList<>();
if (CollectionUtils.isNotEmpty(analyzeTrendEntities)) {
for (AgentDataAnalyzeTrendEntity analyzeTrendEntity : analyzeTrendEntities) {
if (CollectionUtils.isNotEmpty(analyzeApiChannelEntities)) {
for (AgentDataAnalyzeApiChannelEntity analyzeApiChannelEntity : analyzeApiChannelEntities) {
AgentDataAnalyzeApiChannelDto agentDataAnalyzeApiChannelDto = new AgentDataAnalyzeApiChannelDto();
agentDataAnalyzeApiChannelDto.setDateTime(analyzeTrendEntity.getDate());
agentDataAnalyzeApiChannelDto.setCount(BigDecimal.valueOf(analyzeTrendEntity.getTotalPoints()).setScale(2, RoundingMode.HALF_UP).doubleValue());
agentDataAnalyzeApiChannelDto.setDateTime(analyzeApiChannelEntity.getDateTime());
agentDataAnalyzeApiChannelDto.setCount(BigDecimal.valueOf(analyzeApiChannelEntity.getCount()).setScale(2, RoundingMode.HALF_UP).doubleValue());
result.add(agentDataAnalyzeApiChannelDto);
}
}
......
package cn.com.poc.formreport;
import cn.com.poc.data_analyze.domain.DataAnalyzeTimeRange;
import java.util.Date;
import java.util.List;
/**
* @author Helen
* @date 2021/12/21 16:19
*/
public class FormReportRequest {
/**
* 请求类型:exchangeLog-兑换记录, internalExchange-积分兑换, member-会员
* staff-员工, vipReservation-vip预约, notice-公告
* Out-出门申请,Activity-活动申请,Ot-加班申请,Broadcast-广播申请
*/
private String reportRequestType;
/**
* 每页数量
*/
private int pageSize;
/**
* 页数
*/
private int pageNo;
private Date queryBeginScheduleDate;
//data analyze api-channel
/**
* 应用ID
*/
private String agentId;
/**
* 时间范围
*/
private DataAnalyzeTimeRange timeRange;
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public DataAnalyzeTimeRange getTimeRange() {
return timeRange;
}
public void setTimeRange(DataAnalyzeTimeRange timeRange) {
this.timeRange = timeRange;
}
public Date getQueryBeginScheduleDate() {
return queryBeginScheduleDate;
}
public void setQueryBeginScheduleDate(Date queryBeginScheduleDate) {
this.queryBeginScheduleDate = queryBeginScheduleDate;
}
public String getReportRequestType() {
return reportRequestType;
}
public void setReportRequestType(String reportRequestType) {
this.reportRequestType = reportRequestType;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
package cn.com.poc.formreport.aggregate;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.yict.framemax.core.service.BaseService;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
;
/**
* @author Helen
* @date 2021/12/20 9:59
*/
@Component
public interface FormReportingService extends BaseService {
String report(FormReportRequest reportRequest, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package cn.com.poc.formreport.aggregate.impl;
import cn.com.poc.common.constant.FormReportingConstant;
import cn.com.poc.common.service.BosConfigService;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.poc.formreport.aggregate.FormReportingService;
import cn.com.poc.formreport.strategy.AgentDataAnalyzeApiChannelFormReportStrategy;
import cn.com.poc.formreport.strategy.FormReportStrategy;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
/**
* @author Helen
* @date 2021/12/20 10:00
*/
@Service
public class FormReportingServiceImpl implements FormReportingService {
@Resource
private BosConfigService bosConfigService;
@Resource
private AgentDataAnalyzeApiChannelFormReportStrategy agentDataAnalyzeApiChannelFormReportStrategy;
@Override
public String report(FormReportRequest reportRequest, HttpServletRequest request, HttpServletResponse response) throws Exception {
// 数据校验
String returnStr = null;
Assert.notNull(reportRequest.getReportRequestType(), "请求类型不能为null!");
if (reportRequest.getReportRequestType().equals(FormReportingConstant.REPORT_TYPE.API_CHANNEL_POINT_USAGE)) {
// 执行报表导出
doNormalReport(request, response, reportRequest, agentDataAnalyzeApiChannelFormReportStrategy);
}
return returnStr;
}
/**
* 报表导出的抽象,只需要提供 FormReportStrategy的实现类就可以导出数据
*
* @param request
* @param response
* @param strategy
* @throws Exception
*/
public void doNormalReport(HttpServletRequest request, HttpServletResponse response, FormReportRequest reportRequest, FormReportStrategy strategy) throws Exception {
// 设置响应体
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(strategy.getExcelName(reportRequest) + ".xls", "utf-8"));
// 创建一个工作簿
EasyExcel.write(response.getOutputStream(), strategy.getExcelEntityClass())
.needHead(true) // excel表头
.excelType(ExcelTypeEnum.XLS) // excel类型
.registerWriteHandler(new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 设置列宽
Sheet sheet = writeSheetHolder.getSheet();
sheet.setColumnWidth(cell.getColumnIndex(), FormReportingConstant.EXCEL_CONFIG.COLUMN_WIDTH);
}
}).sheet(strategy.getExcelName(reportRequest)).doWrite(strategy.getFormReportData(reportRequest));// 写数据
}
/**
* 报表导出的抽象,只需要提供 FormReportStrategy的实现类就可以导出数据
*
* @param request
* @param response
* @param strategy
* @throws Exception
*/
public String doNormalReportAndUpload2Oss(HttpServletRequest request, HttpServletResponse response, FormReportRequest reportRequest, FormReportStrategy strategy, Boolean needHead) throws Exception {
// 设置响应体
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(strategy.getExcelName(reportRequest) + ".xls", "utf-8"));
// 创建一个工作簿
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
EasyExcel.write(byteArrayOutputStream, strategy.getExcelEntityClass())
.needHead(needHead) // excel表头
.excelType(ExcelTypeEnum.XLS) // excel类型
.registerWriteHandler(new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 设置列宽
Sheet sheet = writeSheetHolder.getSheet();
sheet.setColumnWidth(cell.getColumnIndex(), FormReportingConstant.EXCEL_CONFIG.COLUMN_WIDTH);
}
}).sheet(strategy.getExcelName(reportRequest)).doWrite(strategy.getFormReportData(reportRequest)); // 写数据
byte[] buffer = byteArrayOutputStream.toByteArray();
InputStream sbs = new ByteArrayInputStream(buffer);
return bosConfigService.upload(sbs, getFileDownloadName(strategy.getExcelName(reportRequest), reportRequest.getQueryBeginScheduleDate()), "xls");
}
private String getFileDownloadName(String excelName, Date queryBeginScheduleDate) throws Exception {
return excelName + "(" + DateUtils.formatDate(queryBeginScheduleDate, "yyyy-MM") + ")";
}
/**
* 报表导出的抽象,只需要提供多个FormReportStrategy的实现类就可以根据FormReportStrategy分成多个Sheet导出数据
*
* @param request
* @param response
* @param strategy
* @throws Exception
*/
public void doNormalReports(HttpServletRequest request, HttpServletResponse response, FormReportRequest reportRequest, FormReportStrategy... strategy) throws Exception {
// 设置响应体
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(strategy[0].getExcelName(reportRequest) + ".xls", "utf-8"));
// 创建多个工作簿
int i = 0;
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.needHead(true)
.registerWriteHandler(new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 设置列宽
Sheet sheet = writeSheetHolder.getSheet();
sheet.setColumnWidth(cell.getColumnIndex(), FormReportingConstant.EXCEL_CONFIG.COLUMN_WIDTH);
}
}).build();
try {
for (FormReportStrategy formReportStrategy : strategy) {
WriteSheet sheet = EasyExcel.writerSheet(i++, formReportStrategy.getExcelName(reportRequest)).head(formReportStrategy.getExcelEntityClass()).build();
excelWriter.write(formReportStrategy.getFormReportData(reportRequest), sheet);
}
} finally {
if (excelWriter != null) excelWriter.finish();
}
}
}
package cn.com.poc.formreport.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import java.util.Date;
/**
* @author alex.yao
* @date 2024/12/20
*/
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT)
public class ApiChannelReportEntity {
@ExcelProperty(value = "date", index = 0)
private String dateTime;
@ExcelProperty(value = "usage_point", index = 1)
private Double count;
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public Double getCount() {
return count;
}
public void setCount(Double count) {
this.count = count;
}
}
package cn.com.poc.formreport.rest;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.yict.framemax.core.rest.BaseRest;
import cn.com.yict.framemax.web.permission.Access;
import cn.com.yict.framemax.web.permission.Permission;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Helen
* @date 2021/12/20 10:02
*/
@Permission(Access.Safety)
public interface FormReportingRest extends BaseRest {
/**
* 列表导出
* @throws Exception
* @return
*/
public String report(@RequestBody FormReportRequest reportRequest, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package cn.com.poc.formreport.rest.impl;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.poc.formreport.aggregate.FormReportingService;
import cn.com.poc.formreport.rest.FormReportingRest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Helen
* @date 2021/12/20 10:04
*/
@Component
public class FormReportingRestImpl implements FormReportingRest {
@Resource
private FormReportingService formReportingService;
@Override
public String report(FormReportRequest reportRequest, HttpServletRequest request, HttpServletResponse response) throws Exception {
return formReportingService.report(reportRequest, request, response);
}
}
package cn.com.poc.formreport.strategy;
import cn.com.poc.formreport.FormReportRequest;
import java.util.List;
public abstract class AbstractFormReportStrategy implements FormReportStrategy{
/**
* 动态表头
* @return
*/
public List<List<String>> dynamicHead(FormReportRequest reportRequest) throws Exception {
return null;
}
/**
* 动态数据
* @return
*/
public List<List<String>> dynamicData(FormReportRequest reportRequest){
return null;
}
@Override
public List<List<Object>> dynamicDataObject(FormReportRequest reportRequest) throws Exception {
return null;
}
}
package cn.com.poc.formreport.strategy;
import cn.com.poc.data_analyze.entity.AgentDataAnalyzeApiChannelEntity;
import cn.com.poc.expose.aggregate.AgentDataAnalyzeService;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.poc.formreport.entity.ApiChannelReportEntity;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;
/**
* Agent应用 API渠道-积分使用数据
*
* @author alex.yao
* @date 2024/12/20
*/
@Component
public class AgentDataAnalyzeApiChannelFormReportStrategy extends AbstractFormReportStrategy implements FormReportStrategy {
@Resource
private AgentDataAnalyzeService agentDataAnalyzeService;
@Override
public List<Object> getFormReportData(FormReportRequest reportRequest) throws Exception {
List<AgentDataAnalyzeApiChannelEntity> analyzeApiChannelEntities = agentDataAnalyzeService.getAgentApiChannelPointCount(reportRequest.getAgentId(), reportRequest.getTimeRange());
return analyzeApiChannelEntities.stream().map(value -> {
ApiChannelReportEntity apiChannelReportEntity = new ApiChannelReportEntity();
apiChannelReportEntity.setDateTime(value.getDateTime());
apiChannelReportEntity.setCount(BigDecimal.valueOf(value.getCount()).setScale(2, RoundingMode.HALF_UP).doubleValue());
return apiChannelReportEntity;
}).collect(Collectors.toList());
}
@Override
public Class getExcelEntityClass() {
return ApiChannelReportEntity.class;
}
@Override
public String getExcelName(FormReportRequest reportRequest) throws Exception {
return "Agent-Api-Channel";
}
}
package cn.com.poc.formreport.strategy;
import cn.com.poc.formreport.FormReportRequest;
import java.util.List;
/**
* @author Helen
* @date 2021/12/21 16:54
*/
public interface FormReportStrategy {
/**
* 获取报表导出的数据
* @return
*/
public List<Object> getFormReportData(FormReportRequest reportRequest) throws Exception;
/**
* 获取excel表对应的实体
*/
public Class getExcelEntityClass();
/**
* 获取报表文件名
*/
public String getExcelName(FormReportRequest reportRequest) throws Exception;
/**
* 动态表头
* @return
*/
List<List<String>> dynamicHead(FormReportRequest reportRequest) throws Exception;
/**
* 动态数据
* @return
*/
List<List<String>> dynamicData(FormReportRequest reportRequest) throws Exception;
/**
* 动态数据
* @return
*/
List<List<Object>> dynamicDataObject(FormReportRequest reportRequest) throws Exception;
}
package cn.com.poc.data_analyze;
import cn.com.poc.common.constant.FormReportingConstant;
import cn.com.poc.data_analyze.constants.DataAnalyzeTimeRangeEnum;
import cn.com.poc.data_analyze.domain.DataAnalyzeTimeRange;
import cn.com.poc.formreport.FormReportRequest;
import cn.com.poc.formreport.aggregate.FormReportingService;
import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
/**
* @author alex.yao
* @date 2024/12/20
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = SingleContextInitializer.class)
@WebAppConfiguration
public class FromReportTest {
@Resource
private FormReportingService formReportingService;
@Test
public void test_apiChannelPointUsage() throws Exception {
// DataAnalyzeTimeRange dataAnalyzeTimeRange = new DataAnalyzeTimeRange();
// dataAnalyzeTimeRange.setRangType(DataAnalyzeTimeRangeEnum.month.getType());
//
// FormReportRequest formReportRequest = new FormReportRequest();
// formReportRequest.setAgentId("0376313e2eca4dbba8340a5de5585051");
// formReportRequest.setTimeRange(dataAnalyzeTimeRange);
// formReportRequest.setReportRequestType(FormReportingConstant.REPORT_TYPE.API_CHANNEL_POINT_USAGE);
//
// String url = formReportingService.report(formReportRequest, null, null);
// System.out.println(url);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment