统计查询导出
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -56,6 +56,11 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>3.3.3</version>
|
||||
</dependency>
|
||||
<!--汉字转拼音-->
|
||||
<dependency>
|
||||
<groupId>com.github.promeg</groupId>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,12 @@ public class UserContribution {
|
||||
@TableField(exist = false)
|
||||
private MyUserEntity user;
|
||||
@TableField(exist = false)
|
||||
private String userTel;
|
||||
@TableField(exist = false)
|
||||
private String userName;
|
||||
@TableField(exist = false)
|
||||
private String level;
|
||||
@TableField(exist = false)
|
||||
private List<UserContribution> contributions;
|
||||
@TableField(exist = false)
|
||||
private int StatQueryFlag;
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
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;
|
||||
@@ -19,9 +22,13 @@ 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.math.BigDecimal;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 湖分管理
|
||||
@@ -119,19 +126,53 @@ public class UserContributionController {
|
||||
//统计查询
|
||||
@RequestMapping("/contributionStatQuery")
|
||||
public R contributionStatQuery(@RequestBody Map<String, Object> params) {
|
||||
return R.ok().put("result", getStatQueryData(params));
|
||||
}
|
||||
|
||||
|
||||
public Page getStatQueryData(Map<String, Object> params) {
|
||||
MPJLambdaWrapper<UserContribution> wrapper = new MPJLambdaWrapper();
|
||||
wrapper.select(UserContribution::getUserId);
|
||||
wrapper.selectSum(UserContribution::getScore);
|
||||
wrapper.leftJoin(MyUserEntity.class, MyUserEntity::getId, UserContribution::getUserId);
|
||||
wrapper.select(UserContribution::getUserId);
|
||||
wrapper.selectAs(MyUserEntity::getTel,"userTel");
|
||||
wrapper.selectAs(MyUserEntity::getName,"userName");
|
||||
wrapper.selectSum(UserContribution::getScore);
|
||||
if (params.containsKey("tel")&&StringUtils.isNotEmpty(params.get("tel").toString())) {
|
||||
wrapper.like(MyUserEntity::getTel,params.get("tel"));
|
||||
}
|
||||
wrapper.groupBy(UserContribution::getUserId);
|
||||
wrapper.orderByDesc(UserContribution::getScore);
|
||||
wrapper.orderByDesc("score");
|
||||
Page<UserContribution> page = contributionService.page(new Page<>(
|
||||
Long.parseLong(params.get("current").toString()), Long.parseLong(params.get("limit").toString())), wrapper);
|
||||
List<UserContribution> userContributions = page.getRecords();
|
||||
for (UserContribution contribution : userContributions) {
|
||||
String level = "";
|
||||
if (contribution.getScore().compareTo(new BigDecimal(500))>=0){
|
||||
level = "7星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(300))>=0){
|
||||
level = "6星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(150))>=0){
|
||||
level = "5星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(135))>=0){
|
||||
level = "4.5星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(120))>=0){
|
||||
level = "4星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(105))>=0){
|
||||
level = "3.5星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(90))>=0){
|
||||
level = "3星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(75))>=0){
|
||||
level = "2.5星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(60))>=0){
|
||||
level = "2星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(45))>=0){
|
||||
level = "1.5星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(30))>=0){
|
||||
level = "1星";
|
||||
}else if (contribution.getScore().compareTo(new BigDecimal(15))>=0){
|
||||
level = "0.5星";
|
||||
}
|
||||
contribution.setLevel(level);
|
||||
List<UserContribution> cs = contributionService.list(new LambdaQueryWrapper<UserContribution>()
|
||||
.eq(UserContribution::getUserId,contribution.getUserId()));
|
||||
if(params.containsKey("startTime")&&StringUtils.isNotEmpty(params.get("startTime").toString())){
|
||||
@@ -147,7 +188,42 @@ public class UserContributionController {
|
||||
contribution.setContributions(cs);
|
||||
contribution.setUser(userService.getById(contribution.getUserId()));
|
||||
}
|
||||
return R.ok().put("result", page);
|
||||
return page;
|
||||
}
|
||||
|
||||
@RequestMapping("/exportContributionStatQuery")
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@RequestMapping("/saveUserContribution")
|
||||
|
||||
Reference in New Issue
Block a user