|
@@ -18,7 +18,7 @@ import (
|
|
|
/**
|
|
|
扣账户余额
|
|
|
*/
|
|
|
-func costByAccountBalance(getData func() ([]map[string]interface{}, int, error), appID string, productID int, userProduct *model.UserProduct, product *model.Product, param, ip string) ([]map[string]interface{}, error) {
|
|
|
+func costByAccountBalance(getData func() ([]map[string]interface{}, int, error), appID string, productID int, userProduct *model.UserProduct, product *model.Product, param, ip string) ([]map[string]interface{}, string, error) {
|
|
|
productType := product.ProductType
|
|
|
//productUnit := product.UnitPrice
|
|
|
// if productType == 0 {
|
|
@@ -30,11 +30,12 @@ func costByAccountBalance(getData func() ([]map[string]interface{}, int, error),
|
|
|
// }
|
|
|
datas := []map[string]interface{}{}
|
|
|
var err error
|
|
|
+ orderCode := ""
|
|
|
data, statusCode, _ := execute(getData, appID, productID)
|
|
|
beforeJudge, payMoney := beforeCheck(productType, product.UnitPrice, len(data), userProduct)
|
|
|
if beforeJudge {
|
|
|
global.Logger.Info("交易金额", zap.Any("payMoney:", payMoney))
|
|
|
- err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
|
|
|
+ orderCode, err = afterCheck(len(data), payMoney, userProduct, statusCode, param, ip)
|
|
|
if err != nil {
|
|
|
global.Logger.Error("数据库操作失败", getUserProductError(appID, productID, err)...)
|
|
|
} else {
|
|
@@ -43,7 +44,7 @@ func costByAccountBalance(getData func() ([]map[string]interface{}, int, error),
|
|
|
} else {
|
|
|
err = errors.New("剩余余额不足")
|
|
|
}
|
|
|
- return datas, err
|
|
|
+ return datas, orderCode, err
|
|
|
}
|
|
|
|
|
|
func beforeCheck(productType, unitPrice, dataLen int, userProduct *model.UserProduct) (bool, int) {
|
|
@@ -67,13 +68,14 @@ func beforeCheck(productType, unitPrice, dataLen int, userProduct *model.UserPro
|
|
|
return true, money
|
|
|
}
|
|
|
|
|
|
-func afterCheck(dataLen, payMoney int, userProduct *model.UserProduct, statusCode int, param, ip string) error {
|
|
|
+func afterCheck(dataLen, payMoney int, userProduct *model.UserProduct, statusCode int, param, ip string) (string, error) {
|
|
|
appID := userProduct.AppID
|
|
|
productID := userProduct.ProductID
|
|
|
userProductID := userProduct.ID
|
|
|
userAccount := &model.UserAccount{}
|
|
|
db.GetSFISDB().First(userAccount, &model.UserAccount{AppID: userProduct.AppID})
|
|
|
var errs error
|
|
|
+ orderCode := fmt.Sprint(time.Now().Year()) + fmt.Sprint(utils.GetRandom(8))
|
|
|
//按次扣费-(每调一次剩余量-1)
|
|
|
errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
|
|
|
orderBefore := userAccount.Money
|
|
@@ -85,7 +87,6 @@ func afterCheck(dataLen, payMoney int, userProduct *model.UserProduct, statusCod
|
|
|
tx.Rollback()
|
|
|
return err
|
|
|
}
|
|
|
- orderCode := fmt.Sprint(time.Now().Year()) + fmt.Sprint(utils.GetRandom(8))
|
|
|
//生调用记录
|
|
|
err = tx.Exec("insert into user_call_record (app_id,user_product_id,status,ip,param,order_code) values (?,?,?,?,?,?)", appID, userProductID, statusCode, ip, param, orderCode).Error
|
|
|
if err != nil {
|
|
@@ -103,5 +104,5 @@ func afterCheck(dataLen, payMoney int, userProduct *model.UserProduct, statusCod
|
|
|
//存历史数据
|
|
|
return nil
|
|
|
})
|
|
|
- return errs
|
|
|
+ return orderCode, errs
|
|
|
}
|