This commit is contained in:
2025-05-09 21:45:15 +08:00
parent 28879b02fb
commit 830d9ae9d9
4 changed files with 274 additions and 92 deletions

View File

@@ -3,16 +3,13 @@ package admin_promotion
import (
"context"
"fmt"
"time"
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"tyc-server/app/main/model"
"tyc-server/common/xerr"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type RecordLinkClickLogic struct {
@@ -48,50 +45,12 @@ func (l *RecordLinkClickLogic) RecordLinkClick(req *types.RecordLinkClickReq) (r
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "无效的推广链接路径")
}
err = l.svcCtx.AdminPromotionLinkModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
// 更新总统计
totalStats, err := l.svcCtx.AdminPromotionLinkStatsTotalModel.FindOneByLinkId(l.ctx, link.Id)
if err != nil {
return err
}
totalStats.ClickCount = totalStats.ClickCount + 1
err = l.svcCtx.AdminPromotionLinkStatsTotalModel.UpdateWithVersion(l.ctx, session, totalStats)
if err != nil {
return fmt.Errorf("更新总统计失败%+v", err)
}
// 更新历史统计
today := time.Now().Truncate(24 * time.Hour)
builder := l.svcCtx.AdminPromotionLinkStatsHistoryModel.SelectBuilder()
builder = builder.Where("link_id = ? AND DATE(stats_date) = DATE(?)", link.Id, today)
historyStats, err := l.svcCtx.AdminPromotionLinkStatsHistoryModel.FindAll(l.ctx, builder, "")
if err != nil {
return fmt.Errorf("查询今日统计记录失败%+v", err)
}
if len(historyStats) == 0 {
// 创建今天的记录
newStats := &model.AdminPromotionLinkStatsHistory{
LinkId: link.Id,
StatsDate: today,
ClickCount: 1,
}
_, err = l.svcCtx.AdminPromotionLinkStatsHistoryModel.Insert(l.ctx, session, newStats)
if err != nil {
return fmt.Errorf("创建今日统计记录失败%+v", err)
}
} else {
// 更新今日记录
historyStats[0].ClickCount++
err = l.svcCtx.AdminPromotionLinkStatsHistoryModel.UpdateWithVersion(l.ctx, session, historyStats[0])
if err != nil {
return fmt.Errorf("更新历史统计失败%+v", err)
}
}
return nil
})
// 使用 statsService 更新点击统计
err = l.svcCtx.AdminPromotionLinkStatsService.UpdateLinkStats(l.ctx, link.Id)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新统计失败%+v", err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新点击统计失败: %+v", err)
}
return &types.RecordLinkClickResp{
Success: true,
}, nil