package com.template.controller;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.template.api.WelcomeBuildControllerAPI;
import com.template.common.utils.ExcelUtils;
import com.template.model.enumModel.eFileType;
import com.template.model.pojo.*;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.StudentPageVo;
import com.template.model.vo.WelcomeBuildVo;
import com.template.services.WelcomeBuildService;
import com.template.services.WelcomeSchoolService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
*
* 前端控制器
*
*
* @author ceshi
* @since 2025-06-13
*/
@RestController
public class WelcomeBuildController implements WelcomeBuildControllerAPI {
@Autowired
private WelcomeBuildService welcomeBuildService;
@Autowired
WelcomeSchoolService welcomeSchoolService;
@Override
public CommonResult saveBuild(WelcomeBuild welcomeBuild) {
String build = welcomeBuild.getBuild();
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(WelcomeBuild::getBuild, build)
.eq(WelcomeBuild::getSchool, welcomeBuild.getSchool());
WelcomeBuild wb = welcomeBuildService.getOne(wrapper);
if (ObjectUtils.isNotEmpty(wb)) {
return CommonResult.fail("已存在该楼栋");
}
welcomeBuildService.save(welcomeBuild);
return CommonResult.ok();
}
@Override
public CommonResult deleteBuild(Integer buildId) {
boolean result = welcomeBuildService.removeById(buildId);
return result ? CommonResult.ok() : CommonResult.fail();
}
@Override
public CommonResult updateBuild(WelcomeBuild welcomeBuild) {
//先查询是否有当前楼栋信息
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(WelcomeBuild::getBuild, welcomeBuild.getBuild())
.eq(WelcomeBuild::getSchool, welcomeBuild.getSchool());
WelcomeBuild one = welcomeBuildService.getOne(wrapper);
if (ObjectUtils.isNotEmpty(one)) {
Integer id = one.getId();
if (!id.equals(welcomeBuild.getId())) {
return CommonResult.fail("修改的楼栋已存在");
}
}
welcomeBuildService.updateById(welcomeBuild);
return CommonResult.ok();
}
@Override
public CommonResult listBuild(int currentPage, int pageCount, Integer schoolId, String buildId, String sex) {
PageUtils pageUtils = welcomeBuildService.listBuild(currentPage, pageCount, schoolId, buildId, sex);
return CommonResult.ok(pageUtils);
}
@Override
public CommonResult schoolGroup() {
List schoolGroup = welcomeSchoolService.schoolGroup();
return CommonResult.ok(schoolGroup);
}
@Override
public CommonResult buildGroup(Integer schoolId) {
List schoolGroup = welcomeBuildService.buildGroup(schoolId);
return CommonResult.ok(schoolGroup);
}
@Override
public CommonResult importBuildExcel(MultipartFile file) throws IOException, ParseException {
System.out.println("导入楼栋信息");
if (file.isEmpty() || file.getSize() == 0) {
return CommonResult.fail("导入文件不能为空");
}
String ContentType = file.getContentType();
InputStream inputStream = file.getInputStream();
List result = new ArrayList<>();
//xls格式文件
if (ContentType.equals(eFileType.Xls.getValue())) {
CommonResult> resultData = readXls(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else if (ContentType.equals(eFileType.Xlsx.getValue())) {
CommonResult> resultData = readXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else {
return CommonResult.fail("楼栋信息数据导入只支持Xls或Xlsx格式文件");
}
// 去掉导入表的重复数据
ArrayList welcomeBuilds = new ArrayList<>();
for (int i = 0; i < result.size(); i++) {
WelcomeBuild welcomeBuild = result.get(i);
if (!welcomeBuilds.contains(welcomeBuild)) {
welcomeBuilds.add(welcomeBuild);
}
}
boolean resultBool = welcomeBuildService.saveOrUpdateBatch(welcomeBuilds);
System.out.println("导入楼栋1");
return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
}
/**
* xls文件读取方法
*
* @param inputStream
* @return
* @throws IOException
* @throws ParseException
*/
private CommonResult> readXls(InputStream inputStream) throws IOException, ParseException {
List result = new ArrayList<>();
HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
List ws = welcomeSchoolService.list(null);
List wb = welcomeBuildService.list(null);
//读取第一张sheet
HSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
try {
//rowNum = 3 从第三行开始获取值
//sheetAt.getLastRowNum():从0开始统计数量 所以得+1
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
HSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//第一行数据获取月份
if (rowNum == 0) {
String number = dataFormatter.formatCellValue(row.getCell(0));//序号
if (!number.equals("序号")) {
return CommonResult.fail("导入数据第一列为序号");
}
String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
if (!school.equals("校区名称")) {
return CommonResult.fail("导入数据第二列为校区名称");
}
String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
if (!build.equals("楼栋名称")) {
return CommonResult.fail("导入数据第三列为楼栋名称");
}
String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
if (!sex.equals("楼栋性别")) {
return CommonResult.fail("导入数据第四列为楼栋性别");
}
String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
if (!floors.equals("楼栋层数")) {
return CommonResult.fail("导入数据第五列为楼栋层数");
}
String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
if (!startFloor.equals("起始层数")) {
return CommonResult.fail("导入数据第六列为起始层数");
}
String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
if (!remark.equals("备注")) {
return CommonResult.fail("导入数据第七列为备注");
}
} else {
WelcomeBuild buildData = new WelcomeBuild();
String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
if (ObjectUtils.isEmpty(school)) {
continue;
}
Optional wos = ws.stream().filter(e -> e.getSchool().equals(school)).findFirst();
if (wos != null && wos.isPresent()) {
buildData.setSchoolId(wos.get().getId()+"");
}else {
int h=rowNum+1;
return CommonResult.fail("第"+h+"行,不存在该校区");
}
String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
Optional ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)).findFirst();
if (ob != null && ob.isPresent()) {
buildData.setId(ob.get().getId());
}
buildData.setSchool(school);
buildData.setBuild(build);
String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
buildData.setSex(sex);
String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
buildData.setFloors(floors);
String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
buildData.setStartFloor(Integer.valueOf(startFloor));
String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
buildData.setRemark(remark);
result.add(buildData);
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
/**
* xls文件读取方法
*
* @param inputStream
* @return
* @throws IOException
* @throws ParseException
*/
private CommonResult> readXlsx(InputStream inputStream) throws IOException, ParseException {
List result = new ArrayList<>();
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
List ws = welcomeSchoolService.list(null);
List wb = welcomeBuildService.list(null);
//读取第一张sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
try {
//rowNum = 3 从第三行开始获取值
//sheetAt.getLastRowNum():从0开始统计数量 所以得+1
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
XSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//第一行数据获取月份
if (rowNum == 0) {
String number = dataFormatter.formatCellValue(row.getCell(0));//序号
if (!number.equals("序号")) {
return CommonResult.fail("导入数据第一列为序号");
}
String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
if (!school.equals("校区名称")) {
return CommonResult.fail("导入数据第二列为校区名称");
}
String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
if (!build.equals("楼栋名称")) {
return CommonResult.fail("导入数据第三列为楼栋名称");
}
String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
if (!sex.equals("楼栋性别")) {
return CommonResult.fail("导入数据第四列为楼栋性别");
}
String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
if (!floors.equals("楼栋层数")) {
return CommonResult.fail("导入数据第五列为楼栋层数");
}
String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
if (!startFloor.equals("起始层数")) {
return CommonResult.fail("导入数据第六列为起始层数");
}
String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
if (!remark.equals("备注")) {
return CommonResult.fail("导入数据第七列为备注");
}
} else {
WelcomeBuild buildData = new WelcomeBuild();
String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
if (ObjectUtils.isEmpty(school)) {
continue;
}
Optional wos = ws.stream().filter(e -> e.getSchool().equals(school)).findFirst();
if (wos != null && wos.isPresent()) {
buildData.setSchoolId(wos.get().getId()+"");
}else {
int h=rowNum+1;
return CommonResult.fail("第"+h+"行,不存在该校区");
}
String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
Optional ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)).findFirst();
if (ob != null && ob.isPresent()) {
buildData.setId(ob.get().getId());
}
buildData.setSchool(school);
buildData.setBuild(build);
String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
buildData.setSex(sex);
String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
buildData.setFloors(floors);
String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
buildData.setStartFloor(Integer.valueOf(startFloor));
String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
buildData.setRemark(remark);
result.add(buildData);
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
@Override
public CommonResult downloadBuildExcel() {
return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/welcome/homeimage/楼栋信息管理.xlsx");
}
@Override
public void welcomeBuildExport(HttpServletResponse response, String schoolName, String buildName, String sex) {
List result = welcomeBuildService.listBuild(schoolName, buildName, sex);
//导出
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("楼栋信息");
// 创建样式对象
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
Row headerRow = sheet.createRow(0);
Cell no = headerRow.createCell(0);
no.setCellStyle(style);
no.setCellValue("序号");
Cell school = headerRow.createCell(1);
school.setCellStyle(style);
school.setCellValue("校区名称");
Cell buildStr = headerRow.createCell(2);
buildStr.setCellStyle(style);
buildStr.setCellValue("楼栋名称");
Cell sexStr = headerRow.createCell(3);
sexStr.setCellStyle(style);
sexStr.setCellValue("楼栋性别");
Cell floors = headerRow.createCell(4);
floors.setCellStyle(style);
floors.setCellValue("楼栋层数");
Cell startFloor = headerRow.createCell(5);
startFloor.setCellStyle(style);
startFloor.setCellValue("起始层数");
Cell remark = headerRow.createCell(6);
remark.setCellStyle(style);
remark.setCellValue("备注");
for (int i = 0; i < result.size(); i++) {
WelcomeBuild build = result.get(i);
Row dataRow = sheet.createRow(i + 1);
Cell nod = dataRow.createCell(0);
nod.setCellStyle(style);
nod.setCellValue(i + 1);
Cell schoold = dataRow.createCell(1);
schoold.setCellStyle(style);
schoold.setCellValue(build.getSchool());
Cell buildStrd = dataRow.createCell(2);
buildStrd.setCellStyle(style);
buildStrd.setCellValue(build.getBuild());
Cell sexd = dataRow.createCell(3);
sexd.setCellStyle(style);
sexd.setCellValue(build.getSex());
Cell floord = dataRow.createCell(4);
floord.setCellStyle(style);
floord.setCellValue(build.getFloors());
Cell startFloord = dataRow.createCell(5);
startFloord.setCellStyle(style);
startFloord.setCellValue(build.getStartFloor());
Cell remarkd = dataRow.createCell(6);
remarkd.setCellStyle(style);
remarkd.setCellValue(build.getRemark());
}
// 将工作簿写入文件
ExcelUtils.excelDownload(workbook, "楼栋信息.xlsx", response);
}
}