相关条件的统计
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_reward.go 5.0 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package svc
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model"
  5. zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils"
  6. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/service_award_dividend"
  7. "errors"
  8. "fmt"
  9. "time"
  10. "xorm.io/xorm"
  11. )
  12. func AddCouponRecord(eg *xorm.Engine, user *model.User, pvdType, coupon string) bool {
  13. changeUid := user.Uid
  14. //接收的用户
  15. transformUserTotal, err := db.GetCouponUserTotalByOneEg(eg, changeUid, pvdType)
  16. if err != nil {
  17. return false
  18. }
  19. if transformUserTotal == nil {
  20. transformUserTotal = &model.CommCouponUserTotal{
  21. Uid: changeUid,
  22. UsePvd: "",
  23. CouponAmountValue: "",
  24. CreateAt: time.Now(),
  25. UpdateAt: time.Now(),
  26. LeaveCouponAmountValue: "",
  27. PvdType: pvdType,
  28. }
  29. has, err := eg.InsertOne(transformUserTotal)
  30. if has == 0 || err != nil {
  31. fmt.Println("优惠券错误:", err)
  32. return false
  33. }
  34. }
  35. leaveAmount := zhios_condition_statistics_utils.StrToFloat64(coupon)
  36. beforeTotal := zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.LeaveCouponAmountValue)
  37. transformUserTotal.CouponAmountValue = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.CouponAmountValue)+leaveAmount, 9)
  38. transformUserTotal.LeaveCouponAmountValue = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.LeaveCouponAmountValue)+leaveAmount, 9)
  39. eg.Where("id=?", transformUserTotal.Id).Update(transformUserTotal)
  40. //写入明细
  41. var ord1 = &model.CommCouponUserOrd{
  42. Uid: user.Uid,
  43. OrdType: 50,
  44. PackageId: 0,
  45. CouponAmountValue: zhios_condition_statistics_utils.Float64ToStr(leaveAmount),
  46. CreateAt: time.Now(),
  47. UpdateAt: time.Now(),
  48. PvdType: pvdType,
  49. UsePvd: "[\"GUIDE\",\"SELF_MALL\"]",
  50. GoodsTitle: "升级赠送",
  51. BeforeAmout: zhios_condition_statistics_utils.Float64ToStrByPrec(beforeTotal, 9),
  52. AfterAmout: transformUserTotal.LeaveCouponAmountValue,
  53. }
  54. eg.Insert(ord1)
  55. return true
  56. }
  57. func AddCoin(eg *xorm.Engine, money float64, Title, ordId string, types, transferType, uid, coinId int) (int64, error) {
  58. //定制客户存入购物金分期
  59. req := map[string]string{
  60. "coin_id": zhios_condition_statistics_utils.IntToStr(coinId),
  61. "pvd": "2",
  62. "uid": zhios_condition_statistics_utils.IntToStr(uid),
  63. "oid": ordId,
  64. "amount": zhios_condition_statistics_utils.Float64ToStr(money),
  65. }
  66. state := service_award_dividend.AddPeriodEg(eg, req)
  67. if state == 3 {
  68. return 0, errors.New("失败")
  69. }
  70. if state == 1 {
  71. return 1, nil
  72. }
  73. UserVirtualAmount, err := db.UserVirtualAmountFindById(eg, uid, coinId)
  74. if err != nil || UserVirtualAmount == nil {
  75. if err == nil {
  76. err = errors.New("获取用户余额信息失败")
  77. }
  78. return 0, err
  79. }
  80. beforeAmount := UserVirtualAmount.Amount
  81. afterAmount := ""
  82. if types == 1 {
  83. afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 6)
  84. UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 5), 4)
  85. } else if types == 2 {
  86. afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 6)
  87. UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 5), 4)
  88. if zhios_condition_statistics_utils.StrToInt64(UserVirtualAmount.Amount) < 0 {
  89. err = errors.New("余额不足")
  90. return 0, err
  91. }
  92. }
  93. affected, err := db.UserVirtualAmountUpdate(eg, uid, coinId, UserVirtualAmount, "amount")
  94. if err != nil || affected == 0 {
  95. if err == nil {
  96. err = errors.New("更新用户余额信息失败")
  97. }
  98. return 0, err
  99. }
  100. id, err := virtualCoinFlowInsert(eg, uid, coinId, zhios_condition_statistics_utils.Float64ToStrByPrec(money, 8), ordId, Title, types, transferType, beforeAmount, afterAmount)
  101. if err != nil {
  102. return 0, err
  103. }
  104. return id, nil
  105. }
  106. func virtualCoinFlowInsert(eg *xorm.Engine, uid, coinId int, money string, ordId string, ItemTitle string, types, transferType int, beforeAmount string, afterAmount string) (int64, error) {
  107. now := time.Now()
  108. data := &model.UserVirtualCoinFlow{
  109. Uid: uid,
  110. OrdId: ordId,
  111. CoinId: coinId,
  112. Direction: types,
  113. Title: ItemTitle,
  114. Amout: money,
  115. BeforeAmout: beforeAmount,
  116. AfterAmout: afterAmount,
  117. CreateTime: now,
  118. TransferType: transferType,
  119. Date: now.Format("2006-01"),
  120. }
  121. eg.Insert(data)
  122. return data.Id, nil
  123. }