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

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