|
|
@ -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,7 +389,56 @@ 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()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|