From 150ec0701c2a027155e6969fdf78a585d3626e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Tue, 20 Oct 2020 13:51:30 +0800 Subject: [PATCH] cash finish --- business/jxstore/cms/order.go | 7 ++-- business/jxstore/financial/financial.go | 3 +- business/jxstore/financial/pay.go | 43 ++++++++++++++++++++++++- controllers/order_controller.go | 4 +-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/business/jxstore/cms/order.go b/business/jxstore/cms/order.go index be70bafed..a00ca9593 100644 --- a/business/jxstore/cms/order.go +++ b/business/jxstore/cms/order.go @@ -60,7 +60,7 @@ func Pay(ctx *jxcontext.Context, orderID, payType int, vendorPayType string) (er return err } -func Cash(ctx *jxcontext.Context, orderID, payType int, vendorPayType string) (err error) { +func Cash(ctx *jxcontext.Context, orderID, payType int, vendorPayType string) (errCode string, err error) { var ( db = dao.GetDB() order = &model.Order{ @@ -79,6 +79,9 @@ func Cash(ctx *jxcontext.Context, orderID, payType int, vendorPayType string) (e if userBill == nil { err = financial.AddUserBill(db, jxutils.GenBillID(), order.UserID) } + if userBill.AccountBalance < order.PayPrice { + return model.ErrCodeAccountBalanceNotEnough, err + } err = payHandler.CreateRefund() - return err + return errCode, err } diff --git a/business/jxstore/financial/financial.go b/business/jxstore/financial/financial.go index 6c23fa2a0..4456ae121 100644 --- a/business/jxstore/financial/financial.go +++ b/business/jxstore/financial/financial.go @@ -82,6 +82,7 @@ func (p *PayHandler) CreateRefund() (err error) { if authInfo, err := p.Ctx.GetV2AuthInfo(); err == nil && authInfo.GetAuthType() == weixin.AuthTypeWxApp { param.OpenID = authInfo.GetAuthID() } + globals.SugarLogger.Debugf("CreateRefund wx param: %v", utils.Format4Output(param, false)) result, err := api.WxpayAPI.Transfers(param) if err == nil { p.Order.PayFinishedAt = utils.Str2Time(result.PaymentTime) @@ -94,7 +95,7 @@ func (p *PayHandler) CreateRefund() (err error) { } dao.UpdateEntity(dao.GetDB(), p.Order) if result.ReturnMsg == "" { - err = OnPayFinished(p.Order) + err = OnCashFinished(p.Order) } } } else { diff --git a/business/jxstore/financial/pay.go b/business/jxstore/financial/pay.go index 1adc986af..729f0c904 100644 --- a/business/jxstore/financial/pay.go +++ b/business/jxstore/financial/pay.go @@ -46,7 +46,7 @@ func OnPayFinished(order *model.Order) (err error) { switch order.Type { case model.OrderTypeAccount: //如果是账户充值(发布任务等) - //1、账户收入表增加一条保证金记录 + //1、账户收入表明细 if err = AddBillIncome(db, billID, order.Type, order.PayPrice); err != nil { dao.Rollback(db) } @@ -63,3 +63,44 @@ func OnPayFinished(order *model.Order) (err error) { globals.SugarLogger.Debugf("OnPayFinished end modify account ...") return err } + +func OnCashFinished(order *model.Order) (err error) { + var ( + db = dao.GetDB() + billID int64 + ) + globals.SugarLogger.Debugf("OnCashFinished begin modify account order: %v", utils.Format4Output(order, false)) + dao.Begin(db) + defer func() { + if r := recover(); r != nil { + dao.Rollback(db) + panic(r) + } + }() + //如果用户没有对应账单信息就给他生成一条 + userBill, err := dao.GetUserBill(db, order.UserID, "") + if userBill == nil { + globals.SugarLogger.Debugf("OnCashFinished 未找到该用户的账单 order: %v", utils.Format4Output(order, false)) + return fmt.Errorf("未找到该用户的账单!%v", order.UserID) + } + //根据订单类型来操作账户 + switch order.Type { + case model.OrderTypeAccount: + //如果是账户提现 + //1、账户支出明细增加一条 + if err = AddBillExpend(db, billID, order.Type, order.PayPrice); err != nil { + dao.Rollback(db) + } + //2、账户表账户余额减少相应值 + userBill.AccountBalance -= order.PayPrice + if _, err = dao.UpdateEntity(db, userBill, "AccountBalance"); err != nil { + dao.Rollback(db) + } + default: + globals.SugarLogger.Debugf("OnPayFinished 暂不支持此订单类型 order: %v", utils.Format4Output(order, false)) + return fmt.Errorf("暂不支持此订单类型!") + } + dao.Commit(db) + globals.SugarLogger.Debugf("OnCashFinished end modify account ...") + return err +} diff --git a/controllers/order_controller.go b/controllers/order_controller.go index a9fef81d5..69bfe3c52 100644 --- a/controllers/order_controller.go +++ b/controllers/order_controller.go @@ -36,8 +36,8 @@ func (c *OrderController) Pay() { // @router /Cash [post] func (c *OrderController) Cash() { c.callCash(func(params *tOrderCashParams) (retVal interface{}, errCode string, err error) { - err = cms.Cash(params.Ctx, params.OrderID, params.PayType, params.VendorPayType) - return retVal, "", err + errCode, err = cms.Cash(params.Ctx, params.OrderID, params.PayType, params.VendorPayType) + return retVal, errCode, err }) }