Skip to content

Commit d1bac1b

Browse files
author
Haihui.Wang
committed
Update accounting bill to support query by instance name
1 parent a0ccabb commit d1bac1b

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

builder/store/database/account_bill.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
"opencsg.com/csghub-server/common/errorx"
@@ -100,10 +101,16 @@ func (s *accountBillStoreImpl) ListByUserIDAndDate(ctx context.Context, req type
100101
ColumnExpr("sum(cash_value) as cash_value").
101102
ColumnExpr("sum(duration) as duration").
102103
ColumnExpr("sum(count) as count").
104+
ColumnExpr("MIN(bill_date) as bill_date").
103105
Where("bill_date >= ? and bill_date <= ?", req.StartDate, req.EndDate).
104106
Where("user_uuid = ?", req.TargetUUID).
105-
Where("scene = ?", req.Scene).
106-
Group("customer_id", "data_type", "resolution", "unit_type")
107+
Where("scene = ?", req.Scene)
108+
109+
if len(req.InstanceName) > 0 {
110+
q = q.Where("LOWER(customer_id) LIKE ?", "%"+strings.ToLower(req.InstanceName)+"%")
111+
}
112+
113+
q = q.Group("customer_id", "data_type", "resolution", "unit_type")
107114

108115
count, err := q.Count(ctx)
109116
if err != nil {
@@ -126,7 +133,7 @@ func (s *accountBillStoreImpl) ListByUserIDAndDate(ctx context.Context, req type
126133
return AccountBillRes{}, errorx.HandleDBError(err, nil)
127134
}
128135

129-
err = q.Order("customer_id").Limit(req.Per).Offset((req.Page-1)*req.Per).Scan(ctx, &res)
136+
err = q.Order("bill_date DESC").Order("customer_id").Limit(req.Per).Offset((req.Page-1)*req.Per).Scan(ctx, &res)
130137
if err != nil {
131138
return AccountBillRes{}, errorx.HandleDBError(err, nil)
132139
}
@@ -161,7 +168,7 @@ func (s *accountBillStoreImpl) ListBillsDetailByUserID(ctx context.Context, req
161168
return AccountBillDetailRes{}, errorx.HandleDBError(err, nil)
162169
}
163170

164-
err = q.Order("bill_date ASC").Limit(req.Per).Offset((req.Page-1)*req.Per).Scan(ctx, &bills)
171+
err = q.Order("customer_id").Order("bill_date DESC").Limit(req.Per).Offset((req.Page-1)*req.Per).Scan(ctx, &bills)
165172
if err != nil {
166173
return AccountBillDetailRes{}, errorx.HandleDBError(err, nil)
167174
}

builder/store/database/account_bill_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func TestAccountBillStore_List(t *testing.T) {
8888
require.Nil(t, err)
8989
require.Equal(t, 2, len(res.Data))
9090
expectedData := []types.ITEM{
91-
{Consumption: 31, InstanceName: "c1", Value: 28, PromptToken: 0, CompletionToken: 0},
9291
{Consumption: 21, InstanceName: "c2", Value: 20, PromptToken: 0, CompletionToken: 0},
92+
{Consumption: 31, InstanceName: "c1", Value: 28, PromptToken: 0, CompletionToken: 0},
9393
}
9494
require.Equal(t, expectedData, res.Data)
9595

common/types/accounting.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,14 @@ type ActStatementsReq struct {
260260
}
261261

262262
type AcctBillsReq struct {
263-
CurrentUser string `json:"current_user"`
264-
TargetUUID string `json:"target_uuid"`
265-
Scene SceneType `json:"scene"`
266-
StartDate string `json:"start_date"`
267-
EndDate string `json:"end_date"`
268-
Per int `json:"per"`
269-
Page int `json:"page"`
263+
CurrentUser string `json:"current_user"`
264+
TargetUUID string `json:"target_uuid"`
265+
Scene SceneType `json:"scene"`
266+
StartDate string `json:"start_date"`
267+
EndDate string `json:"end_date"`
268+
Per int `json:"per"`
269+
Page int `json:"page"`
270+
InstanceName string `json:"instance_name"`
270271
}
271272

272273
type AcctBillsDetailReq struct {

0 commit comments

Comments
 (0)