|
@@ -3,6 +3,7 @@ package db
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
"github.com/issue9/conv"
|
|
"github.com/issue9/conv"
|
|
|
|
+ "log"
|
|
"time"
|
|
"time"
|
|
"youngee_b_api/model/gorm_model"
|
|
"youngee_b_api/model/gorm_model"
|
|
"youngee_b_api/model/http_model"
|
|
"youngee_b_api/model/http_model"
|
|
@@ -62,16 +63,35 @@ func GetEnterpriseBalance(ctx context.Context, EnterpriseID int64) (*http_model.
|
|
return nil, err
|
|
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{
|
|
res := &http_model.EnterpriseBalanceData{
|
|
Balance: enterprise.Balance,
|
|
Balance: enterprise.Balance,
|
|
FrozenBalance: enterprise.FrozenBalance,
|
|
FrozenBalance: enterprise.FrozenBalance,
|
|
AvailableBalance: enterprise.AvailableBalance,
|
|
AvailableBalance: enterprise.AvailableBalance,
|
|
|
|
+ Recharging: enterprise.Recharging,
|
|
|
|
+ BillableAmount: BillableAmount,
|
|
|
|
+ Invoicing: Invoicing,
|
|
}
|
|
}
|
|
return res, nil
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
|
|
// 支付-修改企业账户余额
|
|
// 支付-修改企业账户余额
|
|
-func UpdateEnterpriseBalance(ctx context.Context, EnterpriseID int64, balance int64, availableBalance int64, frozenBalance int64) (*int64, error) {
|
|
|
|
|
|
+func UpdateEnterpriseBalance(ctx context.Context, EnterpriseID int64, balance float64, availableBalance float64, frozenBalance float64) (*float64, error) {
|
|
db := GetReadDB(ctx)
|
|
db := GetReadDB(ctx)
|
|
err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
|
|
err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
|
|
Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", balance), "available_balance": gorm.Expr("available_balance + ?", availableBalance), "frozen_balance": gorm.Expr("frozen_balance + ?", frozenBalance)}).Error
|
|
Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", balance), "available_balance": gorm.Expr("available_balance + ?", availableBalance), "frozen_balance": gorm.Expr("frozen_balance + ?", frozenBalance)}).Error
|
|
@@ -87,18 +107,8 @@ func UpdateEnterpriseBalance(ctx context.Context, EnterpriseID int64, balance in
|
|
return &enterprise.Balance, nil
|
|
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)
|
|
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、年月日
|
|
// 1、年月日
|
|
year := time.Now().Year()
|
|
year := time.Now().Year()
|
|
month := time.Now().Month()
|
|
month := time.Now().Month()
|
|
@@ -118,7 +128,7 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
last := ""
|
|
last := ""
|
|
rechargeIdPrefix := "8" + conv.MustString(EnterpriseID)[len(conv.MustString(EnterpriseID))-2:] + conv.MustString(sum)
|
|
rechargeIdPrefix := "8" + conv.MustString(EnterpriseID)[len(conv.MustString(EnterpriseID))-2:] + conv.MustString(sum)
|
|
var rechargeIdLast string
|
|
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
|
|
Last(&rechargeIdLast).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
last = "0"
|
|
last = "0"
|
|
@@ -132,11 +142,27 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
} else {
|
|
} else {
|
|
last = conv.MustString(conv.MustInt(last) + 1)
|
|
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{
|
|
err = db.Model(gorm_model.YounggeeRechargeRecord{}).Create(&gorm_model.YounggeeRechargeRecord{
|
|
- RechargeID: rechargeIdPrefix + last,
|
|
|
|
|
|
+ RechargeID: rechargeId,
|
|
RechargeAmount: Amount,
|
|
RechargeAmount: Amount,
|
|
EnterpriseID: EnterpriseID,
|
|
EnterpriseID: EnterpriseID,
|
|
- Status: 1,
|
|
|
|
|
|
+ Status: 2,
|
|
InvoiceStatus: 1,
|
|
InvoiceStatus: 1,
|
|
CommitAt: time.Now(),
|
|
CommitAt: time.Now(),
|
|
RechargeMethod: 3,
|
|
RechargeMethod: 3,
|
|
@@ -148,3 +174,31 @@ func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) err
|
|
}
|
|
}
|
|
return nil
|
|
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
|
|
|
|
+}
|