导出运行数据
This commit is contained in:
parent
3d6a661399
commit
ea29dd781a
|
@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@RestController
|
||||
|
@ -84,4 +85,15 @@ public class ReportController {
|
|||
log.info("销量排名,开始时间:{};结束时间:{}", begin, end);
|
||||
return Result.success(reportService.getSalesTop10(begin, end));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出运行数据
|
||||
*
|
||||
* @param response HttpResponse
|
||||
*/
|
||||
@ApiOperation(("导出运行数据"))
|
||||
@GetMapping("/export")
|
||||
public void export(HttpServletResponse response) {
|
||||
reportService.exportBusinessData(response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.sky.vo.SalesTop10ReportVO;
|
|||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public interface ReportService {
|
||||
|
@ -43,4 +44,11 @@ public interface ReportService {
|
|||
* @return OrderReportVO
|
||||
*/
|
||||
SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end);
|
||||
|
||||
/**
|
||||
* 导出运行数据
|
||||
*
|
||||
* @param response HttpResponse
|
||||
*/
|
||||
void exportBusinessData(HttpServletResponse response);
|
||||
}
|
||||
|
|
|
@ -5,14 +5,18 @@ import com.sky.entity.Orders;
|
|||
import com.sky.mapper.OrderMapper;
|
||||
import com.sky.mapper.UserMapper;
|
||||
import com.sky.service.ReportService;
|
||||
import com.sky.vo.OrderReportVO;
|
||||
import com.sky.vo.SalesTop10ReportVO;
|
||||
import com.sky.vo.TurnoverReportVO;
|
||||
import com.sky.vo.UserReportVO;
|
||||
import com.sky.service.WorkspaceService;
|
||||
import com.sky.vo.*;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
|
@ -27,6 +31,8 @@ public class ReportServiceImpl implements ReportService {
|
|||
private OrderMapper orderMapper;
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
@Resource
|
||||
private WorkspaceService workspaceService;
|
||||
|
||||
/**
|
||||
* 获得订单数量
|
||||
|
@ -113,11 +119,7 @@ public class ReportServiceImpl implements ReportService {
|
|||
totalUserList.add(totalUser);
|
||||
newUserList.add(newUser);
|
||||
}
|
||||
return UserReportVO.builder()
|
||||
.dateList(StringUtils.join(dateList, ","))
|
||||
.totalUserList(StringUtils.join(totalUserList, ","))
|
||||
.newUserList(StringUtils.join(newUserList, ","))
|
||||
.build();
|
||||
return UserReportVO.builder().dateList(StringUtils.join(dateList, ",")).totalUserList(StringUtils.join(totalUserList, ",")).newUserList(StringUtils.join(newUserList, ",")).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -161,14 +163,7 @@ public class ReportServiceImpl implements ReportService {
|
|||
if (totalOrderCount != 0) {
|
||||
orderCompletionRate = vailOrderCount.doubleValue() / totalOrderCount;
|
||||
}
|
||||
return OrderReportVO.builder()
|
||||
.dateList(StringUtils.join(dateList, ","))
|
||||
.orderCountList(StringUtils.join(orderCountList, ","))
|
||||
.validOrderCountList((StringUtils.join(vailOrderCountList, ",")))
|
||||
.totalOrderCount(totalOrderCount)
|
||||
.validOrderCount(vailOrderCount)
|
||||
.orderCompletionRate(orderCompletionRate)
|
||||
.build();
|
||||
return OrderReportVO.builder().dateList(StringUtils.join(dateList, ",")).orderCountList(StringUtils.join(orderCountList, ",")).validOrderCountList((StringUtils.join(vailOrderCountList, ","))).totalOrderCount(totalOrderCount).validOrderCount(vailOrderCount).orderCompletionRate(orderCompletionRate).build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -191,10 +186,59 @@ public class ReportServiceImpl implements ReportService {
|
|||
String numberList = StringUtils.join(numbers, ",");
|
||||
|
||||
// 封装返回结果数据
|
||||
return SalesTop10ReportVO
|
||||
.builder()
|
||||
.nameList(nameList)
|
||||
.numberList(numberList)
|
||||
.build();
|
||||
return SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出运行数据
|
||||
*
|
||||
* @param response HttpResponse
|
||||
*/
|
||||
@Override
|
||||
public void exportBusinessData(HttpServletResponse response) {
|
||||
// 查询数据库,获取营业额数据---查询最近30天营业额数据
|
||||
LocalDate dateBegin = LocalDate.now().minusDays(30);
|
||||
LocalDate dateEnd = LocalDate.now().minusDays(1);
|
||||
// 查询概览数据
|
||||
BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MIN));
|
||||
// 通过POI将数据写到Excel文件中
|
||||
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
|
||||
try {
|
||||
assert inputStream != null;
|
||||
XSSFWorkbook excel = new XSSFWorkbook(inputStream);
|
||||
XSSFSheet sheet = excel.getSheet("Sheet1");
|
||||
sheet.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + "至" + dateEnd);
|
||||
// 第四行,营业额数据
|
||||
XSSFRow row4 = sheet.getRow(3);
|
||||
row4.getCell(2).setCellValue(businessDataVO.getTurnover());
|
||||
row4.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
|
||||
row4.getCell(6).setCellValue(businessDataVO.getNewUsers());
|
||||
// 第五行,营业额数据
|
||||
XSSFRow row5 = sheet.getRow(4);
|
||||
row5.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
|
||||
row5.getCell(4).setCellValue(businessDataVO.getUnitPrice());
|
||||
|
||||
for (int i = 0; i < 30; i++) {
|
||||
LocalDate date = dateBegin.plusDays(i);
|
||||
// 查询某一天数据
|
||||
BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
|
||||
sheet.getRow(7 + i).getCell(1).setCellValue(date.toString());
|
||||
sheet.getRow(7 + i).getCell(2).setCellValue(businessData.getTurnover());
|
||||
sheet.getRow(7 + i).getCell(3).setCellValue(businessData.getValidOrderCount());
|
||||
sheet.getRow(7 + i).getCell(4).setCellValue(businessData.getOrderCompletionRate());
|
||||
sheet.getRow(7 + i).getCell(5).setCellValue(businessData.getUnitPrice());
|
||||
sheet.getRow(7 + i).getCell(6).setCellValue(businessData.getNewUsers());
|
||||
}
|
||||
|
||||
// 通过输出Excel文件下载到客户端浏览器
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
excel.write(outputStream);
|
||||
// 关闭
|
||||
outputStream.close();
|
||||
excel.close();
|
||||
inputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue