湖分统计查询导出
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -56,11 +56,6 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>3.3.3</version>
|
||||
</dependency>
|
||||
<!--汉字转拼音-->
|
||||
<dependency>
|
||||
<groupId>com.github.promeg</groupId>
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.peanut.common.excel;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ContributionStatQuery {
|
||||
|
||||
@ExcelProperty(value = "序号")
|
||||
private int no;
|
||||
@ExcelProperty(value = "星级")
|
||||
private String level;
|
||||
@ExcelProperty(value = "姓名")
|
||||
private String userName;
|
||||
@ExcelProperty(value = "手机号")
|
||||
private String userTel;
|
||||
@ExcelProperty(value = "明细")
|
||||
private String contribution;
|
||||
@ExcelProperty(value = "湖分")
|
||||
private BigDecimal score;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.peanut.common.excel;
|
||||
|
||||
import com.alibaba.excel.metadata.Head;
|
||||
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// 自定义合并策略 该类继承了AbstractMergeStrategy抽象合并策略,需要重写merge()方法
|
||||
public class CustomMergeStrategy extends AbstractMergeStrategy {
|
||||
|
||||
/**
|
||||
* 分组,每几行合并一次
|
||||
*/
|
||||
private List<Integer> exportFieldGroupCountList;
|
||||
|
||||
/**
|
||||
* 目标合并列index
|
||||
*/
|
||||
private Integer targetColumnIndex;
|
||||
|
||||
// 需要开始合并单元格的首行index
|
||||
private Integer rowIndex;
|
||||
|
||||
// exportDataList为待合并目标列的值
|
||||
public CustomMergeStrategy(List<String> exportDataList, Integer targetColumnIndex) {
|
||||
this.exportFieldGroupCountList = getGroupCountList(exportDataList);
|
||||
this.targetColumnIndex = targetColumnIndex;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
|
||||
|
||||
if (null == rowIndex) {
|
||||
rowIndex = cell.getRowIndex();
|
||||
}
|
||||
// 仅从首行以及目标列的单元格开始合并,忽略其他
|
||||
if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == targetColumnIndex) {
|
||||
mergeGroupColumn(sheet);
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeGroupColumn(Sheet sheet) {
|
||||
int rowCount = rowIndex;
|
||||
for (Integer count : exportFieldGroupCountList) {
|
||||
if(count == 1) {
|
||||
rowCount += count;
|
||||
continue ;
|
||||
}
|
||||
// 合并单元格
|
||||
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1, targetColumnIndex, targetColumnIndex);
|
||||
sheet.addMergedRegionUnsafe(cellRangeAddress);
|
||||
rowCount += count;
|
||||
}
|
||||
}
|
||||
|
||||
// 该方法将目标列根据值是否相同连续可合并,存储可合并的行数
|
||||
private List<Integer> getGroupCountList(List<String> exportDataList){
|
||||
if (CollectionUtils.isEmpty(exportDataList)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
List<Integer> groupCountList = new ArrayList<>();
|
||||
int count = 1;
|
||||
|
||||
for (int i = 1; i < exportDataList.size(); i++) {
|
||||
if (exportDataList.get(i).equals(exportDataList.get(i - 1))) {
|
||||
count++;
|
||||
} else {
|
||||
groupCountList.add(count);
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
// 处理完最后一条后
|
||||
groupCountList.add(count);
|
||||
return groupCountList;
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class UserContribution {
|
||||
//明细
|
||||
private String detail;
|
||||
|
||||
//类型 在线教学 01 学术期刊 03 太湖讲堂 05 创作技术 07 注册邀请 11 课程邀请 13
|
||||
//类型 SysDictDataEntity数据字典里
|
||||
private String type;
|
||||
|
||||
//兑换标志 未兑换0 兑换完毕1
|
||||
@@ -45,6 +45,8 @@ public class UserContribution {
|
||||
@TableField(exist = false)
|
||||
private String level;
|
||||
@TableField(exist = false)
|
||||
private String typeInfo;
|
||||
@TableField(exist = false)
|
||||
private List<UserContribution> contributions;
|
||||
@TableField(exist = false)
|
||||
private int StatQueryFlag;
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.peanut.modules.master.controller;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
import com.peanut.common.excel.ContributionStatQuery;
|
||||
import com.peanut.common.excel.CustomMergeStrategy;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.modules.book.entity.SysDictDataEntity;
|
||||
import com.peanut.modules.common.entity.MyUserEntity;
|
||||
@@ -16,19 +13,20 @@ import com.peanut.modules.master.service.UserContributionService;
|
||||
import com.peanut.modules.sys.service.SysDictDataService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 湖分管理
|
||||
@@ -173,8 +171,13 @@ public class UserContributionController {
|
||||
level = "0.5星";
|
||||
}
|
||||
contribution.setLevel(level);
|
||||
List<UserContribution> cs = contributionService.list(new LambdaQueryWrapper<UserContribution>()
|
||||
.eq(UserContribution::getUserId,contribution.getUserId()));
|
||||
MPJLambdaWrapper<UserContribution> mw = new MPJLambdaWrapper();
|
||||
mw.leftJoin(SysDictDataEntity.class,SysDictDataEntity::getDictType,UserContribution::getType);
|
||||
mw.selectAll(UserContribution.class);
|
||||
mw.selectAs(SysDictDataEntity::getDictValue,"typeInfo");
|
||||
mw.eq(UserContribution::getUserId,contribution.getUserId());
|
||||
mw.eq(SysDictDataEntity::getDictLabel,"userContributionLabel");
|
||||
List<UserContribution> cs = contributionService.list(mw);
|
||||
if(params.containsKey("startTime")&&StringUtils.isNotEmpty(params.get("startTime").toString())){
|
||||
BigDecimal statQueryScore = BigDecimal.ZERO;
|
||||
for (UserContribution c:cs) {
|
||||
@@ -192,38 +195,71 @@ public class UserContributionController {
|
||||
}
|
||||
|
||||
@RequestMapping("/exportContributionStatQuery")
|
||||
public void exportContributionStatQuery(HttpServletResponse response,@RequestBody Map<String, Object> params) throws Exception{
|
||||
public void exportContributionStatQuery(HttpServletResponse response,@RequestBody Map<String, Object> params) throws Exception {
|
||||
String fileName = "湖分统计查询.xlsx";
|
||||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding("utf8");
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + fileName );
|
||||
List<ContributionStatQuery> dataList = new ArrayList();
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
|
||||
OutputStream outputStream = response.getOutputStream();
|
||||
List<UserContribution> list = getStatQueryData(params).getRecords();
|
||||
int i=0;
|
||||
for (UserContribution userContribution:list) {
|
||||
i++;
|
||||
for (UserContribution c:userContribution.getContributions()){
|
||||
ContributionStatQuery data = new ContributionStatQuery();
|
||||
data.setNo(i);
|
||||
data.setLevel(userContribution.getLevel());
|
||||
data.setUserName(userContribution.getUserName());
|
||||
data.setUserTel(userContribution.getUserTel());
|
||||
data.setScore(userContribution.getScore());
|
||||
SysDictDataEntity sysDictDataEntity = sysDictDataService.getOne(new LambdaQueryWrapper<SysDictDataEntity>()
|
||||
.eq(SysDictDataEntity::getDictLabel,"userContributionLabel")
|
||||
.eq(SysDictDataEntity::getDictType,c.getType()));
|
||||
data.setContribution(sysDictDataEntity.getDictValue()+"-"+c.getDetail()+"("+c.getScore()+"分)");
|
||||
dataList.add(data);
|
||||
Workbook workbook = new HSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet("Sheet1");
|
||||
CellStyle cellStyle = workbook.createCellStyle();
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
Row headerRow = sheet.createRow(0);// 创建表头行
|
||||
Cell headerCell0 = headerRow.createCell(0);
|
||||
headerCell0.setCellValue("序号");
|
||||
Cell headerCell2 = headerRow.createCell(1);
|
||||
headerCell2.setCellValue("姓名");
|
||||
Cell headerCell3 = headerRow.createCell(2);
|
||||
headerCell3.setCellValue("电话");
|
||||
sheet.setColumnWidth(2,15*256);
|
||||
Cell headerCell1 = headerRow.createCell(3);
|
||||
headerCell1.setCellValue("星级");
|
||||
Cell headerCell4 = headerRow.createCell(4);
|
||||
headerCell4.setCellValue("湖分");
|
||||
Cell headerCell5 = headerRow.createCell(5);
|
||||
headerCell5.setCellValue("明细");
|
||||
sheet.setColumnWidth(5,60*256);
|
||||
// 填充数据
|
||||
int no = 1;
|
||||
int startI = 1;
|
||||
int i = 1;
|
||||
for (UserContribution userContribution : list) {
|
||||
for (UserContribution c : userContribution.getContributions()) {
|
||||
Row dataRow = sheet.createRow(i);
|
||||
Cell dataCell0 = dataRow.createCell(0);
|
||||
dataCell0.setCellStyle(cellStyle);
|
||||
dataCell0.setCellValue(no);
|
||||
Cell dataCell2 = dataRow.createCell(1);
|
||||
dataCell2.setCellStyle(cellStyle);
|
||||
dataCell2.setCellValue(userContribution.getUserName());
|
||||
Cell dataCell3 = dataRow.createCell(2);
|
||||
dataCell3.setCellStyle(cellStyle);
|
||||
dataCell3.setCellValue(userContribution.getUserTel());
|
||||
Cell dataCell1 = dataRow.createCell(3);
|
||||
dataCell1.setCellStyle(cellStyle);
|
||||
dataCell1.setCellValue(userContribution.getLevel());
|
||||
Cell dataCell4 = dataRow.createCell(4);
|
||||
dataCell4.setCellStyle(cellStyle);
|
||||
dataCell4.setCellValue(userContribution.getScore().toString());
|
||||
Cell dataCell5 = dataRow.createCell(5);
|
||||
dataCell5.setCellValue(c.getTypeInfo() + "-" + c.getDetail() + "(" + c.getScore() + "分)");
|
||||
i++;
|
||||
}
|
||||
// 合并单元格
|
||||
sheet.addMergedRegion(new CellRangeAddress(startI, i+1, 0, 0));
|
||||
sheet.addMergedRegion(new CellRangeAddress(startI, i+1, 1, 1));
|
||||
sheet.addMergedRegion(new CellRangeAddress(startI, i+1, 2, 2));
|
||||
sheet.addMergedRegion(new CellRangeAddress(startI, i+1, 3, 3));
|
||||
sheet.addMergedRegion(new CellRangeAddress(startI, i+1, 4, 4));
|
||||
startI = i+2;
|
||||
no++;
|
||||
}
|
||||
EasyExcel.write(response.getOutputStream(), ContributionStatQuery.class)
|
||||
.registerWriteHandler(new CustomMergeStrategy(dataList.stream().map(o -> o.getNo()+"").collect(Collectors.toList()), 0))
|
||||
.registerWriteHandler(new CustomMergeStrategy(dataList.stream().map(o -> o.getUserName()).collect(Collectors.toList()), 2))
|
||||
.registerWriteHandler(new CustomMergeStrategy(dataList.stream().map(o -> o.getUserTel()).collect(Collectors.toList()), 3))
|
||||
.registerWriteHandler(new CustomMergeStrategy(dataList.stream().map(o -> o.getScore().toString()).collect(Collectors.toList()), 5))
|
||||
.sheet("湖分统计查询")
|
||||
.doWrite(dataList);
|
||||
workbook.write(outputStream);
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
@RequestMapping("/saveUserContribution")
|
||||
|
||||
Reference in New Issue
Block a user