Skip to content

Conversation

@littleniannian
Copy link
Collaborator

@littleniannian littleniannian commented Jan 6, 2026

User description

assign in @LordofAvernus

关联的 issue

#3198

描述你的变更

  • 支持excel格式的导出

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 添加 export_format 参数支持 CSV 与 Excel 导出

  • 更新 SQL、工单、SQL管控 API 接口

  • 新增 ExcelBuilder 工具自适应列宽

  • 更新文档及依赖配置


Diagram Walkthrough

flowchart LR
  A["API 修改"] --> B["新增 export_format 参数"]
  B --> C["支持 CSV 与 Excel 导出"]
  C --> D["新增 ExcelBuilder 工具"]
Loading

File Walkthrough

Relevant files
Enhancement
5 files
instance_audit_plan.go
更新 SQL 导出接口支持 CSV/Excel 格式                                                             
+25/-19 
workflow.go
更新工单导出接口支持 CSV/Excel 格式                                                                   
+3/-1     
sql_manage.go
更新 SQL 管控导出接口支持 CSV/Excel 格式                                                         
+5/-3     
excel_builder.go
新增 ExcelBuilder 实现 Excel 文件构建                                                       
+214/-0 
file.go
新增数据导出方法支持 CSV 与 Excel 格式                                                               
+109/-0 
Documentation
3 files
docs.go
更新 swagger 文档导出描述支持 CSV/Excel 格式                                                 
+33/-4   
swagger.json
更新 swagger.json 中导出描述                                                                       
+33/-4   
swagger.yaml
更新 swagger.yaml 中导出描述                                                                       
+25/-4   
Dependencies
2 files
go.mod
引入 excelize 模块依赖更新                                                                             
+2/-0     
go.sum
更新依赖校验文件信息                                                                                             
+5/-0     

@github-actions
Copy link

github-actions bot commented Jan 6, 2026

PR Reviewer Guide 🔍

(Review updated until commit cf332ef)

🎫 Ticket compliance analysis 🔶

3198 - Partially compliant

Compliant requirements:

  • 支持excel格式导出
  • 更新相关文档及swagger描述

Non-compliant requirements:

  • 新增ExcelBuilder工具自适应列宽(需确认是否覆盖所有场景)

Requires further human verification:

  • SQL管控接口的导出逻辑在不同格式下的表现需进一步测试
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

默认值问题

请检查导出格式的默认值设置是否与业务需求和swagger文档描述保持一致,目前NormalizeExportFormat默认返回"csv",但文档描述中默认应为"excel"。建议统一默认值逻辑,避免用户误解。

// NormalizeExportFormat 规范化导出格式
// 如果格式为空,则默认返回 "csv"
func NormalizeExportFormat(format *ExportFormat) ExportFormat {
	if format == nil {
		return CsvExportFormat
	}
	return *format
}

@github-actions
Copy link

github-actions bot commented Jan 6, 2026

PR Code Suggestions ✨

Latest suggestions up to cf332ef

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
添加空输入检查

新增对空字符串的判断,以防止当传入空的ExportFormat时返回空值导致逻辑混乱。文档中描述的默认值与实际默认行为不一致,建议统一默认返回格式。

sqle/utils/file.go [164-169]

 func NormalizeExportFormat(format *ExportFormat) ExportFormat {
-	if format == nil {
+	if format == nil || *format == "" {
 		return CsvExportFormat
 	}
 	return *format
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion adds an extra condition to handle empty string inputs, ensuring that even when a non-nil pointer holds an empty value the default CsvExportFormat is returned. This is a minor but useful enhancement to improve robustness.

Medium

Previous suggestions

Suggestions up to commit bfa5057
CategorySuggestion                                                                                                                                    Impact
Possible issue
修正工作表逻辑


建议检查ExcelBuilder中新建Sheet的逻辑,该逻辑可能会将应保留的工作表删除。请根据实际需求,保留默认工作表或在新建与删除之间使用不同的Sheet名称以确保工作表存在。

sqle/utils/excel_builder.go [24-40]

 func NewExcelBuilder() (*ExcelBuilder, error) {
 	f := excelize.NewFile()
-	sheetName := "Sheet1"
-
-	// 删除默认的Sheet1,创建新的工作表
-	f.NewSheet(sheetName)
-
-	// 删除默认的Sheet1
-	f.DeleteSheet("Sheet1")
+	// 使用默认的活动工作表名称,避免删除导致工作表缺失
+	sheetName := f.GetSheetName(f.GetActiveSheetIndex())
 
 	return &ExcelBuilder{
 		file:      f,
 		sheetName: sheetName,
 		rowIndex:  1,
 		colWidths: make(map[int]float64),
 	}, nil
 }
Suggestion importance[1-10]: 7

__

Why: This suggestion addresses a potential bug in the sheet creation and deletion logic in NewExcelBuilder, replacing hardcoded "Sheet1" with the current active sheet to avoid inadvertently deleting a needed sheet.

Medium
General
统一导出格式默认值


建议确保导出格式的默认值保持一致。在控制器中默认值为excel,但NormalizeExportFormat函数中默认返回csv,这可能导致不一致的行为。请修改默认返回值为excel以统一业务逻辑。

sqle/utils/file.go [158-182]

 func NormalizeExportFormat(format interface{}) string {
 	var formatStr string
 
 	switch v := format.(type) {
 	case string:
 		formatStr = v
 	case *string:
 		if v != nil {
 			formatStr = *v
 		}
 	default:
-		return "csv"
+		return "excel"
 	}
 
 	if formatStr == "" {
-		return "csv"
+		return "excel"
 	}
 
 	formatStr = strings.ToLower(formatStr)
 	if formatStr != "csv" && formatStr != "excel" {
-		return "csv"
+		return "excel"
 	}
 
 	return formatStr
 }
Suggestion importance[1-10]: 6

__

Why: The suggestion improves consistency by changing the default export format from "csv" to "excel" in NormalizeExportFormat, aligning it with the behavior expected in the controller, though its impact is relatively minor.

Low
Suggestions up to commit bfa552b
CategorySuggestion                                                                                                                                    Impact
Possible issue
修正Excel表单初始化

建议重新审查 NewExcelBuilder 中的表单创建逻辑。当前代码中先创建了默认的 "Sheet1",又尝试新建同名工作表后再删除
"Sheet1",这可能导致活跃表被误删或产生混乱。请确保保留或替换默认工作表,并使用明确的工作表名称以确保生成的 Excel 文件有效。

sqle/utils/excel_builder.go [24-40]

 func NewExcelBuilder() (*ExcelBuilder, error) {
 	f := excelize.NewFile()
-	sheetName := "Sheet1"
+	// 设定自定义的工作表名称
+	sheetName := "AuditPlanSheet"
 
-	// 删除默认的Sheet1,创建新的工作表
-	f.NewSheet(sheetName)
-
-	// 删除默认的Sheet1
-	f.DeleteSheet("Sheet1")
+	// 重命名默认工作表为自定义名称,不需要额外创建和删除工作表
+	index := f.GetActiveSheetIndex()
+	f.SetSheetName(f.GetSheetName(index), sheetName)
 
 	return &ExcelBuilder{
 		file:      f,
 		sheetName: sheetName,
 		rowIndex:  1,
 		colWidths: make(map[int]float64),
 	}, nil
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion replaces the creation and deletion of "Sheet1" with renaming the default active sheet, which simplifies the initialization logic and avoids potential issues. The improvement correctly targets the lines 24-40 in the new hunk.

Medium
Suggestions up to commit 6b5e703
CategorySuggestion                                                                                                                                    Impact
Possible issue
修正工作表初始化逻辑

建议检查并调整创建 Excel 工作表的逻辑。当前代码先用默认工作表 "Sheet1"
初始化,再重复创建和删除同一个工作表,可能会导致潜在的数据丢失或逻辑混乱。可以考虑直接使用默认工作表或明确创建一个新工作表并删除默认工作表,以确保 Excel
文件结构正确。

sqle/utils/excel_builder.go [24-32]

 func NewExcelBuilder() (*ExcelBuilder, error) {
     f := excelize.NewFile()
-    sheetName := "Sheet1"
+    // 直接使用默认工作表
+    sheetName := f.GetSheetName(0)
 
-    // 删除默认的Sheet1,创建新的工作表
-    f.NewSheet(sheetName)
+    return &ExcelBuilder{
+        file:      f,
+        sheetName: sheetName,
+        rowIndex:  1,
+        colWidths: make(map[int]float64),
+    }, nil
+}
 
-    // 删除默认的Sheet1
-    f.DeleteSheet("Sheet1")
-    ...
-
Suggestion importance[1-10]: 7

__

Why: The suggestion improves the Excel sheet initialization by avoiding redundant operations that might lead to data loss or confusion, and it provides a clearer approach using the default sheet, though the impact is relatively moderate.

Medium

@github-actions
Copy link

github-actions bot commented Jan 6, 2026

Persistent review updated to latest commit bfa552b

@github-actions
Copy link

github-actions bot commented Jan 7, 2026

Persistent review updated to latest commit bfa5057

@github-actions
Copy link

github-actions bot commented Jan 7, 2026

Persistent review updated to latest commit cf332ef

@LordofAvernus LordofAvernus merged commit 3b66c0a into main Jan 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants