附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_balance.go 2.0 KiB

3 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. func BalancePay(c *gin.Context, money, oid string, types string) error {
  14. user, err := CheckUser(c)
  15. if user == nil || err != nil {
  16. return err
  17. }
  18. // 获取余额更新锁
  19. cb, err := HandleBalanceDistributedLock(c.GetString("mid"), utils.IntToStr(user.Info.Uid), "balance_pay")
  20. if err != nil {
  21. return err
  22. }
  23. // 释放锁
  24. if cb != nil {
  25. defer cb()
  26. }
  27. finValid := utils.AnyToFloat64(user.Profile.FinValid)
  28. needMoney := utils.AnyToFloat64(money)
  29. if finValid < needMoney {
  30. return e.NewErrCode(e.ERR_BALANCE_NOT_ENOUGH)
  31. }
  32. user.Profile.FinValid = utils.AnyToString(finValid - needMoney)
  33. affect, err := db.UserProfileUpdate(db.DBs[c.GetString("mid")], user.Profile.Uid, user.Profile, "fin_valid")
  34. if err != nil || affect != 1 {
  35. return err
  36. }
  37. var str = ""
  38. if types == md.CommunityTeam {
  39. str = "小店下单"
  40. }
  41. flowInsert(db.DBs[c.GetString("mid")], user.Profile.Uid, money, 21, utils.StrToInt64(oid), 0, 0, str+"余额支付", types, 1, utils.Float64ToStr(finValid), user.Profile.FinValid)
  42. return nil
  43. }
  44. func flowInsert(eg *xorm.Engine, uid int, paidPrice string, orderAction int, ordId int64, id int64, goodsId int, ItemTitle string, ordType string, types int, beforeAmount string, afterAmount string) {
  45. session := eg.NewSession()
  46. now := time.Now()
  47. if err := db.FinUserFlowInsertOneWithSession(
  48. session,
  49. &model.FinUserFlow{
  50. Type: types,
  51. Uid: uid,
  52. Amount: paidPrice,
  53. BeforeAmount: beforeAmount,
  54. AfterAmount: afterAmount,
  55. OrdType: ordType,
  56. OrdId: utils.Int64ToStr(ordId),
  57. OrdAction: orderAction,
  58. OrdDetail: utils.IntToStr(goodsId),
  59. State: 2,
  60. OtherId: id,
  61. OrdTitle: ItemTitle,
  62. OrdTime: int(now.Unix()),
  63. CreateAt: now,
  64. UpdateAt: now,
  65. }); err != nil {
  66. _ = session.Rollback()
  67. _ = logx.Warn(err)
  68. return
  69. }
  70. }