From b46556d4ea0a137f022e7ede604defd12180d423 Mon Sep 17 00:00:00 2001 From: lijikun Date: Tue, 31 Dec 2024 18:55:13 +0800 Subject: [PATCH] =?UTF-8?q?12.31=E6=97=A5,=E5=88=A0=E9=99=A4=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E6=97=A5=E5=BF=97,=E5=AF=BC=E5=87=BAexcel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/controller/couponusers/couponUsers.go | 5 ++- internal/logic/couponusers/couponUsers.go | 54 +++++++++++++++++++++++++- internal/service/couponusers.go | 4 +- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/internal/controller/couponusers/couponUsers.go b/internal/controller/couponusers/couponUsers.go index 46a3070..eb8a398 100644 --- a/internal/controller/couponusers/couponUsers.go +++ b/internal/controller/couponusers/couponUsers.go @@ -165,7 +165,6 @@ func (c *CouponUsers) IsEligibleUser(r *ghttp.Request) { if err := r.Parse(&req); err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } - fmt.Println(req.CouponIds) /*解析token,获取jwcode*/ token := r.Header.Get("token") @@ -225,9 +224,11 @@ func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) { // 导出拥有卡券的用户 func (c *CouponUsers) ExportCouponUsers(r *ghttp.Request) { var req *couponusers.GetCouponUsersReq + // 解析请求参数 if err := r.Parse(&req); err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } - //查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入到响应 + //查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入响应 + service.CouponUsers().ExportCouponUsers(r, req.CouponId, req.Jwcode, req.Name, req.PageNo, req.PageSize) } diff --git a/internal/logic/couponusers/couponUsers.go b/internal/logic/couponusers/couponUsers.go index ab64668..6c49180 100644 --- a/internal/logic/couponusers/couponUsers.go +++ b/internal/logic/couponusers/couponUsers.go @@ -3,16 +3,19 @@ package couponusers import ( "CouponBackendGo/api/v1/couponusers" "CouponBackendGo/internal/dao" + "CouponBackendGo/internal/model/dto" "CouponBackendGo/internal/service" "context" "database/sql" "errors" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gtime" "github.com/xuri/excelize/v2" "mime/multipart" "strconv" "strings" + "time" ) type sCouponUsers struct{} @@ -386,6 +389,55 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i /*未编写*/ // 导出拥有卡券的用户列表 -func (s *sCouponUsers) ExportCouponUsers() { +func (s *sCouponUsers) ExportCouponUsers(r *ghttp.Request, couponId, jwcode int, name string, pageNo, pageSize int) { + //调用查询 + users, err := s.GetCouponUsersByCondition(r.Context(), couponId, jwcode, name, pageNo, pageSize) + if err != nil { + r.Response.WriteJsonExit(dto.Error("查询失败:" + err.Error())) + return + } + if len(users) == 0 { + r.Response.WriteJsonExit(dto.Error("查询结果为空")) + return + } + + //创建 Excel文件 + excelFile := excelize.NewFile() //默认会创建一个Sheet1 + sheetName := "Sheet1" + //设置表头 + headers := []string{"序号", "精网号", "姓名", "部门", "门店"} + for i, header := range headers { + col := string('A' + i) //将索引转换为 Excel列名 + excelFile.SetCellValue(sheetName, col+"1", header) + } + + /*写入数据*/ + rowIndex := 2 //从第二行开始写入数据 + for _, user := range users { + excelFile.SetCellValue(sheetName, "A"+strconv.Itoa(rowIndex), rowIndex-1) //序号 + excelFile.SetCellValue(sheetName, "B"+strconv.Itoa(rowIndex), user.Jwcode) //精网号 + excelFile.SetCellValue(sheetName, "C"+strconv.Itoa(rowIndex), user.Name) //姓名 + excelFile.SetCellValue(sheetName, "D"+strconv.Itoa(rowIndex), user.DeptName) //部门 + excelFile.SetCellValue(sheetName, "E"+strconv.Itoa(rowIndex), user.ShopName) //门店 + rowIndex++ + } + /*写入数据*/ + + //设置文件名 + fileName := "Users_" + 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()) } diff --git a/internal/service/couponusers.go b/internal/service/couponusers.go index 16abb7d..3768a9e 100644 --- a/internal/service/couponusers.go +++ b/internal/service/couponusers.go @@ -10,6 +10,8 @@ import ( "context" "database/sql" "mime/multipart" + + "github.com/gogf/gf/v2/net/ghttp" ) type ( @@ -32,7 +34,7 @@ type ( IssueCouponToUser(ctx context.Context, jwcode int, couponId int) (err error) /*未编写*/ // 导出拥有卡券的用户列表 - ExportCouponUsers() + ExportCouponUsers(r *ghttp.Request, couponId int, jwcode int, name string, pageNo int, pageSize int) } )