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

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