5 changed files with 12 additions and 93 deletions
-
1link_homework/go.mod
-
2link_homework/go.sum
-
6link_homework/internal/controller/clientPage/clientPage.go
-
90link_homework/internal/controller/record/export.go
-
6link_homework/internal/controller/record/manageRecord.go
@ -1,90 +0,0 @@ |
|||||
package record |
|
||||
|
|
||||
import ( |
|
||||
"github.com/360EntSecGroup-Skylar/excelize" |
|
||||
"github.com/gogf/gf/v2/net/ghttp" |
|
||||
"link_homework/api/v1/record" |
|
||||
"link_homework/internal/model/dto" |
|
||||
"link_homework/internal/service" |
|
||||
"strconv" |
|
||||
"time" |
|
||||
) |
|
||||
|
|
||||
// ManageRecord 处理记录的相关操作
|
|
||||
type ExportRecord struct{} |
|
||||
|
|
||||
func NewExportRecord() *ExportRecord { |
|
||||
return &ExportRecord{} |
|
||||
} |
|
||||
|
|
||||
// ExportRecordByCondition 导出记录
|
|
||||
func (m *ExportRecord) ExportRecordByCondition(r *ghttp.Request) { |
|
||||
var req record.GetRecordByConditionReq |
|
||||
// 解析查询条件
|
|
||||
if err := r.Parse(&req); err != nil { |
|
||||
r.Response.WriteJsonExit(dto.Error("参数解析失败: " + err.Error())) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
// 调用查询服务
|
|
||||
records, err := service.Record().GetRecordByCondition( |
|
||||
r.Context(), |
|
||||
req.Id, req.Jwcode, req.DeptId, req.ShopId, req.PageNo, req.PageSize, |
|
||||
) |
|
||||
if err != nil { |
|
||||
//g.Log().Error(r.Context(), "查询记录失败: ", err)
|
|
||||
r.Response.WriteJsonExit(dto.Error("查询记录失败: " + err.Error())) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
if len(records) == 0 { |
|
||||
//g.Log().Warning(r.Context(), "查询结果为空: 参数 = ", req)
|
|
||||
r.Response.WriteJsonExit(dto.Error("查询结果为空")) |
|
||||
return |
|
||||
} |
|
||||
// 创建 Excel 文件
|
|
||||
excelFile := excelize.NewFile() // 默认会创建一个 Sheet1
|
|
||||
sheetName := "Sheet1" // 使用默认的 Sheet1
|
|
||||
|
|
||||
// 设置表头
|
|
||||
headers := []string{"序号", "精网号", "名字", "部门ID", "部门名称", "门店ID", "门店名称", "题目ID", "题目类型", "题目名称", "简答题标题", "作答内容", "提交时间"} |
|
||||
for i, header := range headers { |
|
||||
col := string('A' + i) // 将索引转换为 Excel 列名
|
|
||||
excelFile.SetCellValue(sheetName, col+"1", header) |
|
||||
} |
|
||||
|
|
||||
// 写入数据
|
|
||||
rowIndex := 2 // 从第二行开始写入数据
|
|
||||
for _, record := range records { |
|
||||
for _, reply := range record.Reply { // 遍历每个 `Reply`
|
|
||||
excelFile.SetCellValue(sheetName, "A"+strconv.Itoa(rowIndex), rowIndex-1) |
|
||||
excelFile.SetCellValue(sheetName, "B"+strconv.Itoa(rowIndex), record.Jwcode) |
|
||||
excelFile.SetCellValue(sheetName, "C"+strconv.Itoa(rowIndex), record.Name) |
|
||||
excelFile.SetCellValue(sheetName, "D"+strconv.Itoa(rowIndex), record.DeptId) |
|
||||
excelFile.SetCellValue(sheetName, "E"+strconv.Itoa(rowIndex), record.DeptName) |
|
||||
excelFile.SetCellValue(sheetName, "F"+strconv.Itoa(rowIndex), record.ShopId) |
|
||||
excelFile.SetCellValue(sheetName, "G"+strconv.Itoa(rowIndex), record.ShopName) |
|
||||
excelFile.SetCellValue(sheetName, "H"+strconv.Itoa(rowIndex), reply.FormId) |
|
||||
excelFile.SetCellValue(sheetName, "I"+strconv.Itoa(rowIndex), reply.Type) |
|
||||
excelFile.SetCellValue(sheetName, "J"+strconv.Itoa(rowIndex), reply.Description) |
|
||||
excelFile.SetCellValue(sheetName, "K"+strconv.Itoa(rowIndex), reply.ContentTitle) |
|
||||
excelFile.SetCellValue(sheetName, "L"+strconv.Itoa(rowIndex), reply.Content) |
|
||||
excelFile.SetCellValue(sheetName, "M"+strconv.Itoa(rowIndex), reply.UpdatedAt.String()) |
|
||||
rowIndex++ |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 设置文件名
|
|
||||
fileName := "Records_" + time.Now().Format("20060102150405") + ".xlsx" |
|
||||
|
|
||||
// 保存到缓冲区并返回
|
|
||||
buffer, err := excelFile.WriteToBuffer() |
|
||||
if err != nil { |
|
||||
r.Response.WriteJsonExit(dto.Error("生成 Excel 文件失败: " + err.Error())) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
|
||||
r.Response.Header().Set("Content-Disposition", "attachment; filename="+fileName) |
|
||||
r.Response.Write(buffer.Bytes()) |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue