42 lines
1.7 KiB
Go
42 lines
1.7 KiB
Go
// 功能描述:供气订单支付完成后原子生成正式押金事实;版本:1.0.0。
|
||
package payment
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
)
|
||
|
||
// CompleteGasDeposits 将下单时锁定的押金快照转为用户押金记录,重复回调不会重复入账。
|
||
func CompleteGasDeposits(tx *gorm.DB, orderIdentity string, paidAt time.Time) error {
|
||
var order models.GasorderBasic
|
||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("identity = ?", orderIdentity).First(&order).Error; err != nil {
|
||
return err
|
||
}
|
||
var snapshots []models.GasorderDeposit
|
||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("gasorder_basic_id = ? AND deposit_status = ?", order.ID, 10).Find(&snapshots).Error; err != nil {
|
||
return err
|
||
}
|
||
for _, snapshot := range snapshots {
|
||
var existing int64
|
||
if err := tx.Model(&models.DepositRecord{}).Where("gasorder_id = ? AND product_info_id = ? AND status <> ?", order.ID, snapshot.ProductInfoID, 3).Count(&existing).Error; err != nil {
|
||
return err
|
||
}
|
||
if existing == 0 {
|
||
record := models.DepositRecord{Entity: models.Entity{Identity: models.NewIdentity(), Status: 1}, DepositStatus: 10,
|
||
DepositNo: fmt.Sprintf("DEP%s-%s", paidAt.Format("20060102150405.000000"), snapshot.Identity), UserAccountID: order.UserAccountID, ProductInfoID: snapshot.ProductInfoID,
|
||
PolicyID: snapshot.PolicyID, GasorderID: order.ID, Amount: snapshot.Amount, PaidAt: paidAt}
|
||
if err := tx.Create(&record).Error; err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if err := tx.Model(&snapshot).Where("deposit_status = ?", 10).Update("deposit_status", 23).Error; err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|