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

129 lines
5.0 KiB

  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, amount string) (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. "price": amount,
  66. }
  67. state := service_award_dividend.AddPeriodEg(eg, req)
  68. if state == 3 {
  69. return 0, errors.New("失败")
  70. }
  71. if state == 1 {
  72. return 1, nil
  73. }
  74. UserVirtualAmount, err := db.UserVirtualAmountFindById(eg, uid, coinId)
  75. if err != nil || UserVirtualAmount == nil {
  76. if err == nil {
  77. err = errors.New("获取用户余额信息失败")
  78. }
  79. return 0, err
  80. }
  81. beforeAmount := UserVirtualAmount.Amount
  82. afterAmount := ""
  83. if types == 1 {
  84. afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 6)
  85. UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 5), 4)
  86. } else if types == 2 {
  87. afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 6)
  88. UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 5), 4)
  89. if zhios_condition_statistics_utils.StrToInt64(UserVirtualAmount.Amount) < 0 {
  90. err = errors.New("余额不足")
  91. return 0, err
  92. }
  93. }
  94. affected, err := db.UserVirtualAmountUpdate(eg, uid, coinId, UserVirtualAmount, "amount")
  95. if err != nil || affected == 0 {
  96. if err == nil {
  97. err = errors.New("更新用户余额信息失败")
  98. }
  99. return 0, err
  100. }
  101. id, err := virtualCoinFlowInsert(eg, uid, coinId, zhios_condition_statistics_utils.Float64ToStrByPrec(money, 8), ordId, Title, types, transferType, beforeAmount, afterAmount)
  102. if err != nil {
  103. return 0, err
  104. }
  105. return id, nil
  106. }
  107. func virtualCoinFlowInsert(eg *xorm.Engine, uid, coinId int, money string, ordId string, ItemTitle string, types, transferType int, beforeAmount string, afterAmount string) (int64, error) {
  108. now := time.Now()
  109. data := &model.UserVirtualCoinFlow{
  110. Uid: uid,
  111. OrdId: ordId,
  112. CoinId: coinId,
  113. Direction: types,
  114. Title: ItemTitle,
  115. Amout: money,
  116. BeforeAmout: beforeAmount,
  117. AfterAmout: afterAmount,
  118. CreateTime: now,
  119. TransferType: transferType,
  120. Date: now.Format("2006-01"),
  121. }
  122. eg.Insert(data)
  123. return data.Id, nil
  124. }