|
@@ -3,13 +3,13 @@ package db
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "github.com/issue9/conv"
|
|
|
+ "log"
|
|
|
"time"
|
|
|
"youngee_b_api/model/gorm_model"
|
|
|
"youngee_b_api/model/http_model"
|
|
|
"youngee_b_api/util"
|
|
|
|
|
|
- "github.com/issue9/conv"
|
|
|
-
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
|
|
@@ -64,10 +64,29 @@ func GetEnterpriseBalance(ctx context.Context, EnterpriseID int64) (*http_model.
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
+ var Invoicings []float64
|
|
|
+ var Invoicing float64
|
|
|
+ var BillableAmounts []float64
|
|
|
+ var BillableAmount float64
|
|
|
+ db1 := GetReadDB(ctx)
|
|
|
+ db1.Model(gorm_model.YounggeeInvoiceRecord{}).Select("invoice_amount").
|
|
|
+ Where("enterprise_id = ? AND status = 1", EnterpriseID).Find(&Invoicings)
|
|
|
+ for _, v := range Invoicings {
|
|
|
+ Invoicing += v
|
|
|
+ }
|
|
|
+ db2 := GetReadDB(ctx)
|
|
|
+ db2.Model(gorm_model.YounggeeRechargeRecord{}).Select("recharge_amount").
|
|
|
+ Where("enterprise_id = ? AND status != 3", EnterpriseID).Find(&BillableAmounts)
|
|
|
+ for _, v := range BillableAmounts {
|
|
|
+ BillableAmount += v
|
|
|
+ }
|
|
|
res := &http_model.EnterpriseBalanceData{
|
|
|
Balance: enterprise.Balance,
|
|
|
FrozenBalance: enterprise.FrozenBalance,
|
|
|
AvailableBalance: enterprise.AvailableBalance,
|
|
|
+ Recharging: enterprise.Recharging,
|
|
|
+ BillableAmount: BillableAmount,
|
|
|
+ Invoicing: Invoicing,
|
|
|
}
|
|
|
return res, nil
|
|
|
}
|
|
@@ -89,18 +108,8 @@ func UpdateEnterpriseBalance(ctx context.Context, EnterpriseID int64, balance fl
|
|
|
return &enterprise.Balance, nil
|
|
|
}
|
|
|
|
|
|
-func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) error {
|
|
|
+func MakeRechargeId(ctx context.Context, EnterpriseID int64) string {
|
|
|
db := GetReadDB(ctx)
|
|
|
- err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
|
|
|
- Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", Amount), "available_balance": gorm.Expr("available_balance + ?", Amount)}).Error
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- enterprise := gorm_model.Enterprise{}
|
|
|
- err = db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).Scan(&enterprise).Error
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
// 1、年月日
|
|
|
year := time.Now().Year()
|
|
|
month := time.Now().Month()
|
|
@@ -120,7 +129,7 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
|
last := ""
|
|
|
rechargeIdPrefix := "8" + conv.MustString(EnterpriseID)[len(conv.MustString(EnterpriseID))-2:] + conv.MustString(sum)
|
|
|
var rechargeIdLast string
|
|
|
- err = db.Model(gorm_model.YounggeeRechargeRecord{}).Select("recharge_id").Where("recharge_id like ?", rechargeIdPrefix+"%").
|
|
|
+ err := db.Model(gorm_model.YounggeeRechargeRecord{}).Select("recharge_id").Where("recharge_id like ?", rechargeIdPrefix+"%").
|
|
|
Last(&rechargeIdLast).Error
|
|
|
if err != nil {
|
|
|
last = "0"
|
|
@@ -134,11 +143,27 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
|
} else {
|
|
|
last = conv.MustString(conv.MustInt(last) + 1)
|
|
|
}
|
|
|
+ return rechargeIdPrefix + last
|
|
|
+}
|
|
|
+
|
|
|
+func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) error {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
|
|
|
+ Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", Amount), "available_balance": gorm.Expr("available_balance + ?", Amount)}).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ //enterprise := gorm_model.Enterprise{}
|
|
|
+ //err = db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).Scan(&enterprise).Error
|
|
|
+ //if err != nil {
|
|
|
+ // return err
|
|
|
+ //}
|
|
|
+ rechargeId := MakeRechargeId(ctx, EnterpriseID)
|
|
|
err = db.Model(gorm_model.YounggeeRechargeRecord{}).Create(&gorm_model.YounggeeRechargeRecord{
|
|
|
- RechargeID: rechargeIdPrefix + last,
|
|
|
+ RechargeID: rechargeId,
|
|
|
RechargeAmount: Amount,
|
|
|
EnterpriseID: EnterpriseID,
|
|
|
- Status: 1,
|
|
|
+ Status: 2,
|
|
|
InvoiceStatus: 1,
|
|
|
CommitAt: time.Now(),
|
|
|
RechargeMethod: 3,
|
|
@@ -151,6 +176,34 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func TransferToPublic(ctx context.Context, Amount float64, phone string, EnterpriseID int64, transferVoucherUrl string) error {
|
|
|
+ db := GetReadDB(ctx)
|
|
|
+ rechargeId := MakeRechargeId(ctx, EnterpriseID)
|
|
|
+ err := db.Model(gorm_model.YounggeeRechargeRecord{}).Create(&gorm_model.YounggeeRechargeRecord{
|
|
|
+ RechargeID: rechargeId,
|
|
|
+ RechargeAmount: Amount,
|
|
|
+ EnterpriseID: EnterpriseID,
|
|
|
+ Status: 1,
|
|
|
+ InvoiceStatus: 1,
|
|
|
+ CommitAt: time.Now(),
|
|
|
+ Phone: phone,
|
|
|
+ RechargeMethod: 1,
|
|
|
+ TransferVoucherUrl: transferVoucherUrl,
|
|
|
+ ConfirmAt: time.Now(),
|
|
|
+ }).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ db1 := GetReadDB(ctx)
|
|
|
+ err = db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", EnterpriseID).Updates(
|
|
|
+ map[string]interface{}{"recharging": gorm.Expr("recharging + ?", Amount)}).Error
|
|
|
+ if err != nil {
|
|
|
+ log.Println("[TransferToPublic] recharging modify failed:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func UpdateEnterprise(ctx context.Context, EnterpriseID int64, BusinessName string) error {
|
|
|
db := GetReadDB(ctx)
|
|
|
err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id=?", EnterpriseID).Update("business_name", BusinessName).Error
|