@@ -196,6 +196,7 @@ func (as *accountStatementStoreImpl) deductFeeStatement(ctx context.Context, inp
196196
197197 var err error
198198 var changed ChangedValue
199+ initEventValue := input .Value
199200
200201 if utils .IsGetTokenID (input .Scene ) && len (input .APIKey ) > 0 {
201202 token , err := findByTokenValue (ctx , tx , input .APIKey )
@@ -217,11 +218,11 @@ func (as *accountStatementStoreImpl) deductFeeStatement(ctx context.Context, inp
217218 }
218219 return fmt .Errorf ("deduct account fee, error: %w" , err )
219220 }
220- eventValue := input . Value
221- calcValue := eventValue - changed .Voucher
221+
222+ calcValue := initEventValue - changed .Voucher
222223 consumption := 0.0
223- if eventValue != 0 {
224- consumption = math .Abs (calcValue / eventValue ) * input .Consumption
224+ if initEventValue != 0 {
225+ consumption = math .Abs (calcValue / initEventValue ) * input .Consumption
225226 } else {
226227 consumption = input .Consumption
227228 }
@@ -255,6 +256,7 @@ func (as *accountStatementStoreImpl) deductFeeStatement(ctx context.Context, inp
255256func DeductAccountFee (ctx context.Context , tx bun.Tx , input AccountStatement , checkBalance bool ) (ChangedValue , error ) {
256257 var err error
257258 var acctUser AccountUser
259+ initEventValue := input .Value
258260 changedValue := ChangedValue {
259261 Cash : 0.0 ,
260262 Voucher : 0.0 ,
@@ -282,7 +284,6 @@ func DeductAccountFee(ctx context.Context, tx bun.Tx, input AccountStatement, ch
282284 }
283285 }
284286
285- initValue := input .Value
286287 remainValue := input .Value
287288 cashChangePart1 := 0.0
288289 cashChangePart2 := 0.0
@@ -302,7 +303,7 @@ func DeductAccountFee(ctx context.Context, tx bun.Tx, input AccountStatement, ch
302303 if err != nil {
303304 return changedValue , fmt .Errorf ("deduct voucher, error:%w" , err )
304305 }
305- changedValue .Voucher = initValue - remainValue
306+ changedValue .Voucher = initEventValue - remainValue
306307 }
307308
308309 // 2. check and reduce cash
@@ -561,47 +562,48 @@ func updateFeeBill(ctx context.Context, tx bun.Tx, input AccountStatement, billV
561562 if ! utils .IsNeedCalculateBill (input .Scene ) {
562563 return nil
563564 }
564- // calculate bill
565- bill := AccountBill {
566- BillDate : input .EventDate ,
567- UserUUID : input .UserUUID ,
568- Scene : input .Scene ,
569- CustomerID : input .CustomerID ,
570- Value : billValues .TotalValue ,
571- Consumption : billValues .Consumption ,
572- PromptToken : input .PromptToken ,
573- CompletionToken : input .CompletionToken ,
574- Count : 1 ,
575- TokenID : input .TokenID ,
576- DataType : input .DataType ,
577- Resolution : input .Resolution ,
578- Duration : input .Duration ,
579- VoucherNo : input .VoucherNo ,
580- VoucherValue : billValues .VoucherValue ,
581- CashValue : billValues .CashValue ,
582- }
583- if input .Scene == types .SceneMultiModalServerless {
584- bill .UnitType = input .SkuUnitType
585- }
586-
587- // depend on unique index for update
588- _ , err := tx .NewInsert ().Model (& bill ).
589- On ("CONFLICT (bill_date, user_uuid, scene, customer_id, token_id, data_type, resolution, voucher_no, unit_type) DO UPDATE" ).
590- Set ("value = account_bill.value + ?" , input .Value ).
591- Set ("consumption = account_bill.consumption + ?" , billValues .Consumption ).
592- Set ("prompt_token = account_bill.prompt_token + ?" , input .PromptToken ).
593- Set ("completion_token = account_bill.completion_token + ?" , input .CompletionToken ).
594- Set ("duration = account_bill.duration + ?" , input .Duration ).
595- Set ("count = account_bill.count + ?" , 1 ).
596- Set ("voucher_value = account_bill.voucher_value + ?" , billValues .VoucherValue ).
597- Set ("cash_value = account_bill.cash_value + ?" , billValues .CashValue ).
598- Set ("updated_at = current_timestamp" ).
599- Exec (ctx )
565+ if billValues .TotalValue != 0 {
566+ // calculate bill
567+ bill := AccountBill {
568+ BillDate : input .EventDate ,
569+ UserUUID : input .UserUUID ,
570+ Scene : input .Scene ,
571+ CustomerID : input .CustomerID ,
572+ Value : billValues .TotalValue ,
573+ Consumption : billValues .Consumption ,
574+ PromptToken : input .PromptToken ,
575+ CompletionToken : input .CompletionToken ,
576+ Count : 1 ,
577+ TokenID : input .TokenID ,
578+ DataType : input .DataType ,
579+ Resolution : input .Resolution ,
580+ Duration : input .Duration ,
581+ VoucherNo : input .VoucherNo ,
582+ VoucherValue : billValues .VoucherValue ,
583+ CashValue : billValues .CashValue ,
584+ }
585+ if input .Scene == types .SceneMultiModalServerless {
586+ bill .UnitType = input .SkuUnitType
587+ }
588+
589+ // depend on unique index for update
590+ _ , err := tx .NewInsert ().Model (& bill ).
591+ On ("CONFLICT (bill_date, user_uuid, scene, customer_id, token_id, data_type, resolution, voucher_no, unit_type) DO UPDATE" ).
592+ Set ("value = account_bill.value + ?" , billValues .TotalValue ).
593+ Set ("consumption = account_bill.consumption + ?" , billValues .Consumption ).
594+ Set ("prompt_token = account_bill.prompt_token + ?" , input .PromptToken ).
595+ Set ("completion_token = account_bill.completion_token + ?" , input .CompletionToken ).
596+ Set ("duration = account_bill.duration + ?" , input .Duration ).
597+ Set ("count = account_bill.count + ?" , 1 ).
598+ Set ("voucher_value = account_bill.voucher_value + ?" , billValues .VoucherValue ).
599+ Set ("cash_value = account_bill.cash_value + ?" , billValues .CashValue ).
600+ Set ("updated_at = current_timestamp" ).
601+ Exec (ctx )
600602
601- if err != nil {
602- return fmt .Errorf ("update bill for %s user %s, error:%w" , input .EventUUID , input .UserUUID , err )
603+ if err != nil {
604+ return fmt .Errorf ("update bill for %s user %s, error:%w" , input .EventUUID , input .UserUUID , err )
605+ }
603606 }
604-
605607 if len (input .APIKey ) > 0 {
606608 err := UpdateAPIKeyUsage (ctx , tx , input )
607609 if err != nil {
0 commit comments