126 lines
4.5 KiB
Go
126 lines
4.5 KiB
Go
|
|
package gas
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
func currentWallet(ctx *gin.Context, gas models.GasBasic) (models.WalletBasic, bool) {
|
||
|
|
var wallet models.WalletBasic
|
||
|
|
if err := common.ActiveRecords(impl.DBService).
|
||
|
|
Where("owner_type = ? AND owner_id = ? AND owner_identity = ?", "gas", gas.ID, gas.Identity).
|
||
|
|
First(&wallet).Error; err != nil {
|
||
|
|
common.RespondRecordError(ctx, err)
|
||
|
|
return wallet, false
|
||
|
|
}
|
||
|
|
return wallet, true
|
||
|
|
}
|
||
|
|
|
||
|
|
func ListWalletBasic(ctx *gin.Context) {
|
||
|
|
station, ok := currentGas(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
query := common.ActiveRecords(impl.DBService.Model(&models.WalletBasic{})).
|
||
|
|
Where("owner_type = ? AND owner_id = ?", "gas", station.ID)
|
||
|
|
listScoped(ctx, &models.WalletBasic{}, query, "wallet_basic.created_at desc")
|
||
|
|
}
|
||
|
|
|
||
|
|
func listWalletChild(ctx *gin.Context, model any, table string) {
|
||
|
|
station, ok := currentGas(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
query := common.ActiveRecords(impl.DBService.Model(model)).
|
||
|
|
Joins("JOIN wallet_basic ON wallet_basic.id = "+table+".wallet_basic_id").
|
||
|
|
Where("wallet_basic.owner_type = ? AND wallet_basic.owner_id = ?", "gas", station.ID)
|
||
|
|
listScoped(ctx, model, query, table+".created_at desc")
|
||
|
|
}
|
||
|
|
|
||
|
|
func ListWalletBank(ctx *gin.Context) { listWalletChild(ctx, &models.WalletBank{}, "wallet_bank") }
|
||
|
|
func ListWalletPayment(ctx *gin.Context) {
|
||
|
|
listWalletChild(ctx, &models.WalletPayment{}, "wallet_payment")
|
||
|
|
}
|
||
|
|
func ListWalletRecord(ctx *gin.Context) {
|
||
|
|
listWalletChild(ctx, &models.WalletRecord{}, "wallet_record")
|
||
|
|
}
|
||
|
|
func ListWalletRefund(ctx *gin.Context) {
|
||
|
|
listWalletChild(ctx, &models.WalletRefund{}, "wallet_refund")
|
||
|
|
}
|
||
|
|
func ListWalletApplyCash(ctx *gin.Context) {
|
||
|
|
listWalletChild(ctx, &models.WalletApplyCash{}, "wallet_apply_cash")
|
||
|
|
}
|
||
|
|
|
||
|
|
func CreateWalletApplyCash(ctx *gin.Context) {
|
||
|
|
station, ok := currentGas(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
wallet, ok := currentWallet(ctx, station)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var request struct {
|
||
|
|
WalletBankIdentity string `json:"wallet_bank_identity"`
|
||
|
|
RequestNo string `json:"request_no" binding:"required,max=128"`
|
||
|
|
Amount int64 `json:"amount" binding:"required"`
|
||
|
|
Channel string `json:"channel" binding:"required,max=32"`
|
||
|
|
Remark string `json:"remark" binding:"max=2000"`
|
||
|
|
}
|
||
|
|
if err := ctx.ShouldBindJSON(&request); err != nil || request.Amount <= 0 ||
|
||
|
|
strings.TrimSpace(request.RequestNo) == "" {
|
||
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var pendingAmount int64
|
||
|
|
if err := impl.DBService.Model(&models.WalletApplyCash{}).
|
||
|
|
Where("wallet_basic_id = ? AND status <> ? AND apply_status = ?", wallet.ID, common.StatusArchived, common.StatusPending).
|
||
|
|
Select("COALESCE(SUM(amount), 0)").Scan(&pendingAmount).Error; err != nil ||
|
||
|
|
request.Amount > wallet.WithdrawalBalance-pendingAmount {
|
||
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var bankID uint64
|
||
|
|
if request.WalletBankIdentity != "" {
|
||
|
|
var bank models.WalletBank
|
||
|
|
if err := common.ActiveRecords(impl.DBService).
|
||
|
|
Where("identity = ? AND wallet_basic_id = ?", request.WalletBankIdentity, wallet.ID).First(&bank).Error; err != nil {
|
||
|
|
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
bankID = bank.ID
|
||
|
|
}
|
||
|
|
apply := models.WalletApplyCash{
|
||
|
|
Entity: common.NewEntity(common.StatusEnable), ApplyStatus: common.StatusPending,
|
||
|
|
WalletBasicID: wallet.ID, WalletBankID: bankID, CashNo: models.NewIdentity(),
|
||
|
|
RequestNo: request.RequestNo, Amount: request.Amount, Channel: request.Channel, Remark: request.Remark,
|
||
|
|
}
|
||
|
|
if err := impl.DBService.Create(&apply).Error; err != nil {
|
||
|
|
infra.Response.Error(ctx, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
common.RespondCreatedResource(ctx, apply)
|
||
|
|
}
|
||
|
|
|
||
|
|
func ListFinSettlement(ctx *gin.Context) {
|
||
|
|
station, ok := currentGas(ctx)
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
query := common.ActiveRecords(impl.DBService.Model(&models.FinSettlement{})).
|
||
|
|
Where("subject_type = ? AND subject_id = ?", "gas", station.ID)
|
||
|
|
listScoped(ctx, &models.FinSettlement{}, query, "fin_settlement.created_at desc")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 对账表目前没有主体归属字段,气站端不得暴露平台全局渠道数据。
|
||
|
|
func ListFinReconciliation(ctx *gin.Context) {
|
||
|
|
query := common.ActiveRecords(impl.DBService.Model(&models.FinReconciliation{})).Where("1 = 0")
|
||
|
|
listScoped(ctx, &models.FinReconciliation{}, query, "fin_reconciliation.created_at desc")
|
||
|
|
}
|