订单分佣规则
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.

91 lines
3.3 KiB

  1. package rule
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md"
  5. zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
  6. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache"
  7. "errors"
  8. "github.com/shopspring/decimal"
  9. "xorm.io/xorm"
  10. )
  11. func InitForAppreciation(redisAddr string) (err error) {
  12. if redisAddr != "" {
  13. cache.NewRedis(redisAddr)
  14. }
  15. _, err = cache.SelectDb(md.RedisDataBase)
  16. return
  17. }
  18. func CalcAppreciationValueEg(eg *xorm.Engine) (err error, value float64) {
  19. var appreciationBase model.AppreciationBase
  20. //1、查询增值积分资产总值
  21. has, err := eg.Table("appreciation_base").Where("is_use =1").Get(&appreciationBase)
  22. if err != nil {
  23. return err, value
  24. }
  25. if !has {
  26. return errors.New("未查询到`增值积分资产总值`记录"), value
  27. }
  28. if zhios_order_relate_utils.StrToFloat64(appreciationBase.Sum) == 0 { //默认是1
  29. return nil, 1
  30. }
  31. sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
  32. flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
  33. value, _ = sum.Div(flowSum).RoundFloor(4).Float64()
  34. return
  35. }
  36. /*
  37. 计算增值积分当前价值
  38. TODO:: 公式【 总资产/流通资产=当前积分价值 】
  39. */
  40. func CalcAppreciationValue(session *xorm.Session) (err error, value float64) {
  41. var appreciationBase model.AppreciationBase
  42. //1、查询增值积分资产总值
  43. has, err := session.Table("appreciation_base").Where("is_use =1").Get(&appreciationBase)
  44. if err != nil {
  45. return err, value
  46. }
  47. if !has {
  48. return errors.New("未查询到`增值积分资产总值`记录"), value
  49. }
  50. if zhios_order_relate_utils.StrToFloat64(appreciationBase.Sum) == 0 { //默认是1
  51. return nil, 1
  52. }
  53. sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
  54. flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
  55. value, _ = sum.Div(flowSum).RoundFloor(4).Float64()
  56. return
  57. }
  58. //DealTransferIn 处理转入
  59. func DealTransferIn(session *xorm.Session, amount float64) (err error, value float64) {
  60. amountValue := decimal.NewFromFloat(amount)
  61. err, nowValue := CalcAppreciationValue(session)
  62. if err != nil {
  63. return
  64. }
  65. nowValueF := decimal.NewFromFloat(nowValue)
  66. value, _ = amountValue.Div(nowValueF).Float64()
  67. return
  68. }
  69. // DealWithdrawalAndDestroy 处理给用户提现
  70. func DealWithdrawalAndDestroy(session *xorm.Session, feeMap md.DealWithdrawalFeeResp, transferOut float64) (err error, resp md.DealWithdrawalAndDestroyResp) {
  71. transferOutValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalCommissionFee))
  72. destroyValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalDestroyFee))
  73. refluxValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalRefluxFee))
  74. err, nowValue := CalcAppreciationValue(session)
  75. if err != nil {
  76. return
  77. }
  78. resp.TransferOut = transferOut
  79. resp.Price = nowValue
  80. resp.TransferOutValue, _ = decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(nowValue)).Float64()
  81. resp.AmountOut, _ = transferOutValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
  82. resp.DestroyValue, _ = destroyValue.Float64()
  83. resp.RefluxValue, _ = refluxValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
  84. return
  85. }