湖分统计查询导出

This commit is contained in:
wuchunlei
2025-04-24 17:23:04 +08:00
parent 6728333b59
commit cfe8a58a75
5 changed files with 72 additions and 148 deletions

View File

@@ -56,11 +56,6 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version>
</dependency>
<!--汉字转拼音--> <!--汉字转拼音-->
<dependency> <dependency>
<groupId>com.github.promeg</groupId> <groupId>com.github.promeg</groupId>

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -25,7 +25,7 @@ public class UserContribution {
//明细 //明细
private String detail; private String detail;
//类型 在线教学 01 学术期刊 03 太湖讲堂 05 创作技术 07 注册邀请 11 课程邀请 13 //类型 SysDictDataEntity数据字典里
private String type; private String type;
//兑换标志 未兑换0 兑换完毕1 //兑换标志 未兑换0 兑换完毕1
@@ -45,6 +45,8 @@ public class UserContribution {
@TableField(exist = false) @TableField(exist = false)
private String level; private String level;
@TableField(exist = false) @TableField(exist = false)
private String typeInfo;
@TableField(exist = false)
private List<UserContribution> contributions; private List<UserContribution> contributions;
@TableField(exist = false) @TableField(exist = false)
private int StatQueryFlag; private int StatQueryFlag;

View File

@@ -1,12 +1,9 @@
package com.peanut.modules.master.controller; package com.peanut.modules.master.controller;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper; 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.common.utils.R;
import com.peanut.modules.book.entity.SysDictDataEntity; import com.peanut.modules.book.entity.SysDictDataEntity;
import com.peanut.modules.common.entity.MyUserEntity; 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 com.peanut.modules.sys.service.SysDictDataService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; 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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* 湖分管理 * 湖分管理
@@ -173,8 +171,13 @@ public class UserContributionController {
level = "0.5星"; level = "0.5星";
} }
contribution.setLevel(level); contribution.setLevel(level);
List<UserContribution> cs = contributionService.list(new LambdaQueryWrapper<UserContribution>() MPJLambdaWrapper<UserContribution> mw = new MPJLambdaWrapper();
.eq(UserContribution::getUserId,contribution.getUserId())); 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())){ if(params.containsKey("startTime")&&StringUtils.isNotEmpty(params.get("startTime").toString())){
BigDecimal statQueryScore = BigDecimal.ZERO; BigDecimal statQueryScore = BigDecimal.ZERO;
for (UserContribution c:cs) { for (UserContribution c:cs) {
@@ -192,38 +195,71 @@ public class UserContributionController {
} }
@RequestMapping("/exportContributionStatQuery") @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"; String fileName = "湖分统计查询.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8"); fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8"); response.setCharacterEncoding("utf8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName ); response.setHeader("Content-disposition", "attachment;filename=" + fileName);
List<ContributionStatQuery> dataList = new ArrayList(); OutputStream outputStream = response.getOutputStream();
List<UserContribution> list = getStatQueryData(params).getRecords(); List<UserContribution> list = getStatQueryData(params).getRecords();
int i=0; Workbook workbook = new HSSFWorkbook();
for (UserContribution userContribution:list) { Sheet sheet = workbook.createSheet("Sheet1");
i++; CellStyle cellStyle = workbook.createCellStyle();
for (UserContribution c:userContribution.getContributions()){ cellStyle.setAlignment(HorizontalAlignment.CENTER);
ContributionStatQuery data = new ContributionStatQuery(); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
data.setNo(i); Row headerRow = sheet.createRow(0);// 创建表头行
data.setLevel(userContribution.getLevel()); Cell headerCell0 = headerRow.createCell(0);
data.setUserName(userContribution.getUserName()); headerCell0.setCellValue("序号");
data.setUserTel(userContribution.getUserTel()); Cell headerCell2 = headerRow.createCell(1);
data.setScore(userContribution.getScore()); headerCell2.setCellValue("姓名");
SysDictDataEntity sysDictDataEntity = sysDictDataService.getOne(new LambdaQueryWrapper<SysDictDataEntity>() Cell headerCell3 = headerRow.createCell(2);
.eq(SysDictDataEntity::getDictLabel,"userContributionLabel") headerCell3.setCellValue("电话");
.eq(SysDictDataEntity::getDictType,c.getType())); sheet.setColumnWidth(2,15*256);
data.setContribution(sysDictDataEntity.getDictValue()+"-"+c.getDetail()+"("+c.getScore()+"分)"); Cell headerCell1 = headerRow.createCell(3);
dataList.add(data); 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) workbook.write(outputStream);
.registerWriteHandler(new CustomMergeStrategy(dataList.stream().map(o -> o.getNo()+"").collect(Collectors.toList()), 0)) outputStream.close();
.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);
} }
@RequestMapping("/saveUserContribution") @RequestMapping("/saveUserContribution")