小班自考证书导出

This commit is contained in:
wuchunlei
2025-06-25 18:04:13 +08:00
parent 15c225dcba
commit 884d0ab1e3

View File

@@ -3,6 +3,7 @@ package com.peanut.modules.master.controller;
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.utils.DateUtils;
import com.peanut.common.utils.R;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.ClassEntityService;
@@ -12,11 +13,20 @@ import com.peanut.modules.common.service.UserCertificateService;
import com.peanut.modules.master.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -37,6 +47,94 @@ public class UserCertificateController {
@Autowired
private CourseService courseService;
//小班自考证书列表
@RequestMapping("/userClassAndZKCertificateList")
public R userClassAndZKCertificateList(@RequestBody Map<String,Object> params) {
MPJLambdaWrapper<UserCertificate> wrapper = new MPJLambdaWrapper();
wrapper.leftJoin(MyUserEntity.class,MyUserEntity::getId,UserCertificate::getUserId);
wrapper.in(UserCertificate::getLabelId,5,8);
wrapper.select("count(1) as count,user_id userId,name,tel,email");
wrapper.groupBy(UserCertificate::getUserId);
wrapper.orderByDesc("count");
Page<Map<String,Object>> certificatePage = userCertificateService.pageMaps(new Page<>(
Long.parseLong(params.get("current").toString()),Long.parseLong(params.get("limit").toString())),wrapper);
for (Map<String,Object> map:certificatePage.getRecords()){
map.put("certificate",userCertificateService.list(new LambdaQueryWrapper<UserCertificate>()
.eq(UserCertificate::getUserId,map.get("userId"))
.in(UserCertificate::getLabelId,5,8)));
}
return R.ok().put("certificateList", certificatePage);
}
//导出小班自考证书
@RequestMapping("/exportUserClassAndZKCertificate")
public void exportUserClassAndZKCertificate(HttpServletResponse response, @RequestBody Map<String,Object> params) {
R r = userClassAndZKCertificateList(params);
if ("0".equals(r.get("code").toString())){
Page<Map<String,Object>> certificateList = (Page<Map<String,Object>>) r.get("certificateList");
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("用户证书");
Row titleRow = sheet.createRow(0);
titleRow.createCell(0).setCellValue("序号");
titleRow.createCell(1).setCellValue("姓名");
titleRow.createCell(2).setCellValue("电话");
titleRow.createCell(3).setCellValue("邮箱");
titleRow.createCell(4).setCellValue("编号");
titleRow.createCell(5).setCellValue("证书");
//序号默认为1
int cell = 1;
//遍历
int no = 1;
int startI = 1;
int i = 1;
for (Map<String,Object> map : certificateList.getRecords()) {
List<UserCertificate> list = (List)map.get("certificate");
for (UserCertificate uc:list){
Row row = sheet.createRow(cell);
row.createCell(0).setCellValue(no);
row.createCell(1).setCellValue(map.get("name").toString());
row.createCell(2).setCellValue(map.get("tel").toString());
row.createCell(3).setCellValue(map.get("email").toString());
row.createCell(4).setCellValue(uc.getCertificateNo());
row.createCell(5).setCellValue((uc.getLabelId()==5?"小班":"自考")+"-"+uc.getTitle());
//序号自增
cell++;
i++;
}
// 合并单元格
if(startI!=i-1){
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));
}
startI = i;
no++;
}
String fileName = "小班自考证书.xlsx";
OutputStream outputStream =null;
try {
//文件名编码格式
fileName = URLEncoder.encode(fileName,"UTF-8");
//设置ContentType请求信息格式
response.setContentType("application/vnd.ms-excel");
//设置标头
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
outputStream = response.getOutputStream();
wb.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//新增证书
@RequestMapping("/addCertificate")
public R addCertificate(@RequestBody UserCertificate userCertificate) {