Commit 8d68ce9a by 彭芳

[feat]: add return filed

parent e4755882
......@@ -8,8 +8,8 @@ const (
)
type PageRequest struct {
Page int `form:"page" json:"page" binding:"required"` // 页码
PageSize int `form:"page_size" json:"page_size" binding:"required"` // 每页大小
Page int `form:"page" json:"page"` // 页码
PageSize int `form:"page_size" json:"page_size" ` // 每页大小
OrderBy string // 排序字段
OrderByMode string // 排序方式 desc asc
}
......
package errs
import "fmt"
var (
ErrorArgs = New(40001, "请求参数错误")
ServiceError = New(50000, "系统错误请稍后再试。")
)
type Error struct {
Code int // 错误码
Msg string // 错误信息
}
func (e *Error) Error() string {
return e.Msg
}
func (e *Error) WithMessage(msg string) *Error {
out := &Error{
Code: e.Code,
Msg: e.Msg,
}
if out.Msg == "" {
out.Msg = msg
} else {
out.Msg = fmt.Sprintf("%s: %s", e.Msg, msg)
}
return out
}
func New(code int, msg string) *Error {
return &Error{Code: code, Msg: msg}
}
......@@ -21,16 +21,35 @@ func NewReportRepo(data *Data, logger log.Logger) biz.CampaignRepo {
}
func (p *campaignRepo) List(ctx context.Context, customerId int64, req *biz.ListCampaignRequest) (int64, biz.CampaignList, error) {
var list biz.CampaignList
db := p.data.db.Table("ads_report_campaign_day").
Select("campaign_id, campaign_name").
Where("customer_id = ?", customerId)
tableName := "ads_report_campaign_day"
if req.TimeGranularity == biz.StatTimeGranularityHourly {
tableName = "ads_report_campaign_hour"
}
db := p.data.db.Table(tableName).
Select("campaign_id, campaign_name, advertiser_id, date_long AS stat_datetime, cost, `show`, click, `convert`, "+
"deep_convert, attribution_convert, attribution_deep_convert, download_start, download_finish, click_install, "+
"install_finish, active, register, next_day_open, next_day_open_cost, next_day_open_rate, attribution_next_day_open_cnt,"+
" game_addiction, pay_count, in_app_uv, in_app_detail_uv, in_app_cart, in_app_pay, in_app_order, attribution_game_pay_7d_count,"+
" game_pay_count, total_play, valid_play, play_25_feed_break, play_50_feed_break, play_75_feed_break, play_100_feed_break, "+
"play_duration_sum, attribution_active_pay_intra_one_day_count, attribution_active_pay_intra_one_day_amount, gmt_modified AS timestamp").
Where("customer_id = ?", customerId).
Where("is_delete = ?", 0)
// add start_date and end_date condition
if req.StartDate != "" {
db = db.Where("date_long >= ?", req.StartDateLong)
}
if req.EndDate != "" {
db = db.Where("date_long <= ?", req.EndDateLong)
}
var total int64
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
if err := db.Limit(req.Limit()).Offset(req.Offset()).Scan(&list).Error; err != nil {
var list biz.CampaignList
if err := db.Order(req.GetOrderBy("id") + " " + req.GetOrderByMode()).
Limit(req.Limit()).Offset(req.Offset()).Scan(&list).Error; err != nil {
return total, nil, err
}
return total, list, nil
......
......@@ -5,16 +5,28 @@ import (
"github.com/gin-gonic/gin"
"demo/internal/biz/errs"
"demo/internal/util"
)
// ResponseError 公共返回错误处理
func ResponseError(c *gin.Context, err error) {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
util.RequestKey: c.GetString(util.RequestKey),
"code": http.StatusInternalServerError,
"message": err.Error(),
})
if v, ok := err.(*errs.Error); ok {
if v.Code == errs.ServiceError.Code {
v.Msg = errs.ServiceError.Msg
}
c.AbortWithStatusJSON(http.StatusOK, gin.H{
util.RequestKey: c.GetString(util.RequestKey),
"code": v.Code,
"message": v.Msg,
})
} else {
c.AbortWithStatusJSON(http.StatusOK, gin.H{
util.RequestKey: c.GetString(util.RequestKey),
"code": errs.ServiceError.Code,
"message": errs.ServiceError.Msg,
})
}
}
// ResponseSuccess 返回成功参数
......
......@@ -7,6 +7,7 @@ import (
"github.com/go-kratos/kratos/v2/log"
"demo/internal/biz"
"demo/internal/biz/errs"
"demo/internal/util"
)
......@@ -24,17 +25,20 @@ func NewReportService(uc *biz.ReportUsecase, logger log.Logger) *ReportService {
func (p *ReportService) ListCampaignData(ctx *gin.Context) {
var req biz.ListCampaignRequest
if err := ctx.ShouldBind(&req); err != nil {
ResponseError(ctx, err)
p.log.Error("ShouldBind err:", err)
ResponseError(ctx, errs.ErrorArgs)
return
}
customerIdStr := ctx.GetString(util.CustomerId)
customerId, err := strconv.Atoi(customerIdStr)
if err != nil {
p.log.Error("Atoi err:", err)
ResponseError(ctx, err)
return
}
data, err := p.uc.ListCampaignData(ctx, int64(customerId), &req)
if err != nil {
p.log.Error("ListCampaignData err:", err)
ResponseError(ctx, err)
return
}
......
package util
import (
"strconv"
"time"
)
type Second int64
func NewSecond(ts ...time.Time) Second {
t := time.Now()
if len(ts) > 0 {
t = ts[0]
}
v := t.Format(`20060102150405`)
minute, _ := strconv.Atoi(v)
return Second(minute)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment