🎨 新增兑换码库导出excel功能
This commit is contained in:
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/lckt/global"
|
||||
"git.echol.cn/loser/lckt/model/app"
|
||||
request2 "git.echol.cn/loser/lckt/model/app/request"
|
||||
@@ -12,8 +13,10 @@ import (
|
||||
"git.echol.cn/loser/lckt/utils"
|
||||
"git.echol.cn/loser/lckt/utils/wechat"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/xuri/excelize/v2"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -346,3 +349,90 @@ func (s RedeemCodeService) Redeem(p request2.RedeemCDK, ctx *gin.Context) (err e
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s RedeemCodeService) ExportCDK(p request2.ExportCDK) (f *excelize.File, err error) {
|
||||
var cdks []app.CDK
|
||||
err = global.GVA_DB.Where("redeem_id = ?", p.Eid).Find(&cdks).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导出兑换码失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
var redeem app.RedeemCode
|
||||
err = global.GVA_DB.Where("id = ?", p.Eid).First(&redeem).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("读取码库信息失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 导出Excel
|
||||
f = excelize.NewFile()
|
||||
temp, err := os.CreateTemp("", "cdk.xlsx")
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建临时文件失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
defer os.Remove(temp.Name())
|
||||
defer temp.Close()
|
||||
|
||||
// 创建一个工作表
|
||||
index, err := f.NewSheet("Sheet1")
|
||||
// 设置单元格的值
|
||||
f.SetCellValue("Sheet1", "A1", "赠送码标题")
|
||||
f.SetCellValue("Sheet1", "B1", "CDK")
|
||||
f.SetCellValue("Sheet1", "C1", "状态")
|
||||
// 兑换链接
|
||||
f.SetCellValue("Sheet1", "D1", "兑换链接")
|
||||
f.SetCellValue("Sheet1", "E1", "使用人")
|
||||
for i, cdk := range cdks {
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), redeem.CodeName)
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), cdk.Code)
|
||||
if cdk.Status == 2 {
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), "已使用")
|
||||
} else if cdk.Status == 1 {
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), "未使用")
|
||||
}
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), p.Domain+"pages/user/cdk/index?dhm="+cdk.Code)
|
||||
f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), cdk.UseName)
|
||||
}
|
||||
|
||||
// 设置工作簿的默认工作表
|
||||
f.SetActiveSheet(index)
|
||||
// 设置列宽
|
||||
if err := f.SetColWidth("Sheet1", "A", "A", 20); err != nil {
|
||||
global.GVA_LOG.Error("设置列宽失败", zap.Error(err))
|
||||
}
|
||||
if err := f.SetColWidth("Sheet1", "B", "B", 20); err != nil {
|
||||
global.GVA_LOG.Error("设置列宽失败", zap.Error(err))
|
||||
}
|
||||
if err := f.SetColWidth("Sheet1", "C", "C", 10); err != nil {
|
||||
global.GVA_LOG.Error("设置列宽失败", zap.Error(err))
|
||||
}
|
||||
if err := f.SetColWidth("Sheet1", "D", "D", 100); err != nil {
|
||||
global.GVA_LOG.Error("设置列宽失败", zap.Error(err))
|
||||
}
|
||||
if err := f.SetColWidth("Sheet1", "E", "E", 20); err != nil {
|
||||
global.GVA_LOG.Error("设置列宽失败", zap.Error(err))
|
||||
}
|
||||
|
||||
// 设置单元格样式 居中
|
||||
style, err := f.NewStyle(&excelize.Style{
|
||||
Alignment: &excelize.Alignment{
|
||||
Horizontal: "center",
|
||||
Vertical: "center",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("设置单元格样式失败", zap.Error(err))
|
||||
}
|
||||
if err := f.SetCellStyle("Sheet1", "A1", fmt.Sprintf("E%d", len(cdks)+1), style); err != nil {
|
||||
global.GVA_LOG.Error("设置单元格样式失败", zap.Error(err))
|
||||
}
|
||||
|
||||
if err = f.Write(temp); err != nil {
|
||||
global.GVA_LOG.Error("写入临时文件失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user