Merge branch 'feature-publishLog-daiyb' into dev

This commit is contained in:
daiyb 2026-02-26 15:55:44 +08:00
commit 9998dfa55f
4 changed files with 149 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package cast
import (
"fmt"
"fonchain-fiee/api/cast"
modelCast "fonchain-fiee/pkg/model/cast"
"strings"
@ -102,3 +103,44 @@ func (w *Work) ExportExcelWorkList(data []*cast.WorkListResp_Info) (*excelize.Fi
return f, nil
}
func (w *Work) ExportPublishLogList(data []*cast.PublishLogInfo, savePath string) error {
f := excelize.NewFile()
defer f.Close()
sheetName := "Sheet1"
f.SetSheetName("Sheet1", sheetName)
headers := []string{
"状态", "失败原因", "提交发布时间", "发布平台", "发布方式", "作品类型", "作品标题", "艺人", "用户编号",
}
for col, h := range headers {
cell, _ := excelize.CoordinatesToCellName(col+1, 1)
f.SetCellValue(sheetName, cell, h)
}
for rowIndex, info := range data {
row := rowIndex + 2
platformName := modelCast.PlatformNameKv[info.PlatformID]
if platformName == "" {
platformName = fmt.Sprintf("%d", info.PlatformID)
}
values := []interface{}{
modelCast.PublishStatusMM[int(info.PublishMediaStatus)],
info.Detail,
info.CreatedAt,
modelCast.PlatformNameKv[info.PlatformID],
modelCast.PublishSourceMM[int(info.PublishSource)],
modelCast.WorkCategoryMM[int(info.WorkCategory)],
info.Title,
info.ArtistName,
info.ArtistSubNum,
}
for col, v := range values {
cell, _ := excelize.CoordinatesToCellName(col+1, row)
f.SetCellValue(sheetName, cell, v)
}
}
return f.SaveAs(savePath)
}

View File

@ -31,6 +31,18 @@ var WorkCategoryMM = map[int]string{
1: "图文",
2: "视频",
}
var PublishSourceMM = map[int]string{
1: "手动发布",
2: "定时任务",
3: "手动发布",
}
var PublishStatusMM = map[int]string{
1: "进行中",
2: "发布成功",
3: "发布失败",
4: "发布异常",
}
var WorkCostTypeMM = map[int]string{
1: "套餐",

View File

@ -51,11 +51,11 @@ func MediaRouter(r *gin.RouterGroup) {
work.POST("list-published", serviceCast.WorkListPublished)
work.POST("update-work-script", serviceCast.UpdateWorkScript)
work.POST("publish-log-list", serviceCast.PublishLogList)
work.Any("publish-log-list-export", serviceCast.PublishLogListExport)
}
workNoAuth := noAuth.Group("work")
{
workNoAuth.POST("cron-republish", serviceCast.CronRePublish)
}

View File

@ -2,11 +2,19 @@ package cast
import (
"context"
"fmt"
"strings"
"time"
"fonchain-fiee/api/cast"
"fonchain-fiee/pkg/cache"
logicCast "fonchain-fiee/pkg/logic/cast"
"fonchain-fiee/pkg/model/login"
"fonchain-fiee/pkg/service"
"fonchain-fiee/pkg/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// PublishLogList 查询发布记录列表
@ -23,3 +31,89 @@ func PublishLogList(ctx *gin.Context) {
}
service.Success(ctx, resp)
}
// PublishLogListExport 导出发布记录列表 Excel
func PublishLogListExport(ctx *gin.Context) {
var req cast.ListPublishLogReq
if err := ctx.ShouldBind(&req); err != nil {
return
}
loginInfo := login.GetUserInfoFromC(ctx)
newCtx := NewCtxWithUserInfo(ctx)
lockKey := "PublishLogListExport" + fmt.Sprint(loginInfo.ID)
replay := cache.RedisClient.SetNX(lockKey, time.Now().Unix(), time.Minute*30)
if !replay.Val() {
return
}
defer cache.RedisClient.Del(lockKey)
const batchSize = 5000
var allData []*cast.PublishLogInfo
page := int32(1)
if req.Page > 0 {
page = req.Page
}
originalPageSize := req.PageSize
req.PageSize = batchSize
zap.L().Info("开始分批导出发布记录列表", zap.Int32("batchSize", batchSize))
for {
req.Page = page
zap.L().Info("获取第 N 页数据", zap.Int32("page", page), zap.Int32("pageSize", req.PageSize))
resp, err := service.CastProvider.ListPublishLog(newCtx, &req)
if err != nil {
zap.L().Error("获取发布记录失败", zap.Error(err), zap.Int32("page", page))
return
}
if resp == nil || len(resp.Data) == 0 {
zap.L().Info("没有更多数据", zap.Int32("page", page))
break
}
allData = append(allData, resp.Data...)
zap.L().Info("获取数据成功",
zap.Int32("page", page),
zap.Int("本批次数量", len(resp.Data)),
zap.Int("累计总数", len(allData)))
if len(resp.Data) < batchSize {
zap.L().Info("已到最后一页", zap.Int32("page", page))
break
}
page++
}
req.PageSize = originalPageSize
zap.L().Info("数据获取完成开始生成Excel", zap.Int("总数据量", len(allData)))
if len(allData) == 0 {
return
}
fileName := fmt.Sprintf("发布记录_%s.xlsx", time.Now().Format("20060102150405"))
filePath := fmt.Sprintf("./runtime/%d/%s", loginInfo.ID, fileName)
utils.CheckDirPath("./runtime/"+fmt.Sprint(loginInfo.ID), true)
var logicWork = new(logicCast.Work)
if err := logicWork.ExportPublishLogList(allData, filePath); err != nil {
zap.L().Error("生成Excel失败", zap.Error(err))
service.Error(ctx, err)
return
}
scheme := "http"
if ctx.GetHeader("X-Forwarded-Proto") == "https" {
scheme = "https"
}
exportUrl := fmt.Sprintf("%s://%s/api/fiee/static/%s", scheme, ctx.Request.Host, strings.Replace(filePath, "./runtime/", "", 1))
zap.L().Info("Excel导出成功", zap.String("文件名", fileName), zap.Int("记录数", len(allData)))
service.Success(ctx, gin.H{"url": exportUrl})
}