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

67 lines
2.2 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. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache"
  6. "errors"
  7. "github.com/shopspring/decimal"
  8. "xorm.io/xorm"
  9. )
  10. func InitForAppreciation(redisAddr string) (err error) {
  11. if redisAddr != "" {
  12. cache.NewRedis(redisAddr)
  13. }
  14. _, err = cache.SelectDb(md.RedisDataBase)
  15. return
  16. }
  17. /*
  18. 计算增值积分当前价值
  19. TODO:: 公式【 总资产/流通资产=当前积分价值 】
  20. */
  21. func CalcAppreciationValue(session *xorm.Session) (err error, value float64) {
  22. var appreciationBase model.AppreciationBase
  23. //1、查询增值积分资产总值
  24. has, err := session.Table("appreciation_base").Where("is_use =1").Get(&appreciationBase)
  25. if err != nil {
  26. return err, value
  27. }
  28. if !has {
  29. return errors.New("未查询到`增值积分资产总值`记录"), value
  30. }
  31. sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
  32. flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
  33. value, _ = sum.Div(flowSum).Float64()
  34. return
  35. }
  36. //DealTransferIn 处理转入
  37. func DealTransferIn(session *xorm.Session, amount float64) (err error, value float64) {
  38. amountValue := decimal.NewFromFloat(amount)
  39. err, nowValue := CalcAppreciationValue(session)
  40. if err != nil {
  41. return
  42. }
  43. nowValueF := decimal.NewFromFloat(nowValue)
  44. value, _ = amountValue.Div(nowValueF).Float64()
  45. return
  46. }
  47. // DealWithdrawalAndDestroy 处理给用户提现
  48. func DealWithdrawalAndDestroy(session *xorm.Session, transferOut float64) (err error, resp md.DealWithdrawalAndDestroyResp) {
  49. transferOutValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalCommissionFee)
  50. destroyValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalDestroyFee)
  51. refluxValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalRefluxFee)
  52. err, nowValue := CalcAppreciationValue(session)
  53. if err != nil {
  54. return
  55. }
  56. resp.TransferOut = transferOut
  57. resp.AmountOut, _ = transferOutValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
  58. resp.DestroyValue, _ = destroyValue.Float64()
  59. resp.DestroyValue, _ = refluxValue.Float64()
  60. return
  61. }