|
@ -5,11 +5,15 @@ import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"github.com/360EntSecGroup-Skylar/excelize" |
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp" |
|
|
pkgRecord "link_homework/api/v1/record" |
|
|
pkgRecord "link_homework/api/v1/record" |
|
|
"link_homework/internal/dao" |
|
|
"link_homework/internal/dao" |
|
|
"link_homework/internal/model/dto" |
|
|
"link_homework/internal/model/dto" |
|
|
"link_homework/internal/service" |
|
|
"link_homework/internal/service" |
|
|
|
|
|
"strconv" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type sRecord struct{} |
|
|
type sRecord struct{} |
|
@ -183,3 +187,70 @@ func (s *sRecord) GetShopInfoByDeptId(ctx context.Context, deptId string) (shops |
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%s shopinfo", deptId), shopJSON) |
|
|
_, err = g.Redis().Set(ctx, fmt.Sprintf("%s shopinfo", deptId), shopJSON) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ExportExcel 查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入响应
|
|
|
|
|
|
func (s *sRecord) ExportExcel(r *ghttp.Request, groupId, jwcode int, deptId, shopId string, pageNo, pageSize int) { |
|
|
|
|
|
// 调用查询服务
|
|
|
|
|
|
records, err := service.Record().GetRecordByCondition( |
|
|
|
|
|
r.Context(), groupId, jwcode, deptId, shopId, pageNo, 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()) |
|
|
|
|
|
} |