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

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