Browse Source

12月24日下午后台查询页面逻辑,token过期时间,将导出方法分离到service

dev_ljk
lijikun 5 months ago
parent
commit
5b9d5a02ad
  1. 67
      link_homework/internal/controller/record/manageRecord.go

67
link_homework/internal/controller/record/manageRecord.go

@ -90,69 +90,6 @@ func (m *ManageRecord) ExportRecordByCondition(r *ghttp.Request) {
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
}
// 设置响应头,指定内容类型为Excel文件
r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
// 设置响应头,指定文件名为fileName
r.Response.Header().Set("Content-Disposition", "attachment; filename="+fileName)
// 将buffer中的内容写入响应
r.Response.Write(buffer.Bytes())
//查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入响应
service.Record().ExportExcel(r, req.Id, req.Jwcode, req.DeptId, req.ShopId, req.PageNo, req.PageSize)
}
Loading…
Cancel
Save