feat(credit): 导入功能支持读取项目名称超链接

- 在 CreditUser 实体类中新增 url 字段用于存储项目网址
- 在导入逻辑中增加读取 Excel 超链接的功能
- 新增 readNameHyperlinks 方法解析“项目名称”列的超链接
- 将读取到的超链接地址设置到对应 CreditUser 实体的 url 属性中
- 引入 Apache POI 相关依赖以支持 Excel 文件操作
- 更新导入处理流程,确保超链接与数据行正确匹配
This commit is contained in:
2025-12-22 17:33:00 +08:00
parent 0c6cb13ca7
commit c1d045b9b2
2 changed files with 57 additions and 0 deletions

View File

@@ -17,7 +17,11 @@ import com.gxwebsoft.credit.param.CreditUserParam;
import com.gxwebsoft.credit.service.CreditUserService; import com.gxwebsoft.credit.service.CreditUserService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@@ -27,10 +31,13 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 招投标信息表控制器 * 招投标信息表控制器
@@ -166,12 +173,18 @@ public class CreditUserController extends BaseController {
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Integer currentUserId = loginUser != null ? loginUser.getUserId() : null; Integer currentUserId = loginUser != null ? loginUser.getUserId() : null;
Integer currentTenantId = loginUser != null ? loginUser.getTenantId() : null; Integer currentTenantId = loginUser != null ? loginUser.getTenantId() : null;
Map<Integer, String> urlMap = readNameHyperlinks(file, 0, usedTitleRows, usedHeadRows);
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
CreditUserImportParam param = list.get(i); CreditUserImportParam param = list.get(i);
try { try {
CreditUser item = convertImportParamToEntity(param); CreditUser item = convertImportParamToEntity(param);
String link = urlMap.get(i);
if (link != null && !link.isEmpty()) {
item.setUrl(link);
}
// 设置默认值 // 设置默认值
if (item.getUserId() == null && currentUserId != null) { if (item.getUserId() == null && currentUserId != null) {
item.setUserId(currentUserId); item.setUserId(currentUserId);
@@ -267,6 +280,46 @@ public class CreditUserController extends BaseController {
return ExcelImportUtil.importExcel(file.getInputStream(), CreditUserImportParam.class, importParams); return ExcelImportUtil.importExcel(file.getInputStream(), CreditUserImportParam.class, importParams);
} }
/**
* 读取“项目名称”列的超链接,按数据行顺序返回。
*/
private Map<Integer, String> readNameHyperlinks(MultipartFile file, int sheetIndex, int titleRows, int headRows) throws Exception {
Map<Integer, String> result = new HashMap<>();
try (InputStream is = file.getInputStream(); Workbook workbook = WorkbookFactory.create(is)) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
if (sheet == null) {
return result;
}
int headerRowNum = titleRows + headRows - 1;
Row headerRow = sheet.getRow(headerRowNum);
int nameColIndex = 0;
if (headerRow != null) {
for (int c = headerRow.getFirstCellNum(); c < headerRow.getLastCellNum(); c++) {
Cell cell = headerRow.getCell(c);
if (cell != null && "项目名称".equals(cell.getStringCellValue())) {
nameColIndex = c;
break;
}
}
}
int dataStartRow = titleRows + headRows;
for (int r = dataStartRow; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Cell cell = row.getCell(nameColIndex);
if (cell != null && cell.getHyperlink() != null) {
String address = cell.getHyperlink().getAddress();
if (address != null && !address.isEmpty()) {
result.put(r - dataStartRow, address);
}
}
}
}
return result;
}
/** /**
* 过滤掉完全空白的导入行,避免空行导致导入失败 * 过滤掉完全空白的导入行,避免空行导致导入失败
*/ */

View File

@@ -1,6 +1,7 @@
package com.gxwebsoft.credit.entity; package com.gxwebsoft.credit.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableLogic;
@@ -29,6 +30,9 @@ public class CreditUser implements Serializable {
@Schema(description = "项目名称") @Schema(description = "项目名称")
private String name; private String name;
@Schema(description = "项目网址")
private String url;
@Schema(description = "唯一标识") @Schema(description = "唯一标识")
private String code; private String code;