|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package rule
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md"
- zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache"
- "errors"
- "github.com/shopspring/decimal"
- "xorm.io/xorm"
- )
-
- func InitForAppreciation(redisAddr string) (err error) {
- if redisAddr != "" {
- cache.NewRedis(redisAddr)
- }
- _, err = cache.SelectDb(md.RedisDataBase)
- return
- }
- func CalcAppreciationValueEg(eg *xorm.Engine) (err error, value float64) {
- var appreciationBase model.AppreciationBase
- //1、查询增值积分资产总值
- has, err := eg.Table("appreciation_base").Where("is_use =1").Get(&appreciationBase)
- if err != nil {
- return err, value
- }
- if !has {
- return errors.New("未查询到`增值积分资产总值`记录"), value
- }
- if zhios_order_relate_utils.StrToFloat64(appreciationBase.Sum) == 0 { //默认是1
- return nil, 1
- }
- sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
- flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
- value, _ = sum.Div(flowSum).RoundFloor(4).Float64()
- return
- }
-
- /*
- 计算增值积分当前价值
- TODO:: 公式【 总资产/流通资产=当前积分价值 】
- */
- func CalcAppreciationValue(session *xorm.Session) (err error, value float64) {
- var appreciationBase model.AppreciationBase
- //1、查询增值积分资产总值
- has, err := session.Table("appreciation_base").Where("is_use =1").Get(&appreciationBase)
- if err != nil {
- return err, value
- }
- if !has {
- return errors.New("未查询到`增值积分资产总值`记录"), value
- }
- if zhios_order_relate_utils.StrToFloat64(appreciationBase.Sum) == 0 { //默认是1
- return nil, 1
- }
- sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
- flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
- value, _ = sum.Div(flowSum).RoundFloor(4).Float64()
- return
- }
-
- //DealTransferIn 处理转入
- func DealTransferIn(session *xorm.Session, amount float64) (err error, value float64) {
- amountValue := decimal.NewFromFloat(amount)
- err, nowValue := CalcAppreciationValue(session)
- if err != nil {
- return
- }
- nowValueF := decimal.NewFromFloat(nowValue)
- value, _ = amountValue.Div(nowValueF).Float64()
- return
- }
-
- // DealWithdrawalAndDestroy 处理给用户提现
- func DealWithdrawalAndDestroy(session *xorm.Session, feeMap md.DealWithdrawalFeeResp, transferOut float64) (err error, resp md.DealWithdrawalAndDestroyResp) {
- transferOutValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalCommissionFee))
- destroyValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalDestroyFee))
- refluxValue := decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(feeMap.WithdrawalRefluxFee))
- err, nowValue := CalcAppreciationValue(session)
- if err != nil {
- return
- }
- resp.TransferOut = transferOut
- resp.Price = nowValue
- resp.TransferOutValue, _ = decimal.NewFromFloat(transferOut).Mul(decimal.NewFromFloat(nowValue)).Float64()
- resp.AmountOut, _ = transferOutValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
- resp.DestroyValue, _ = destroyValue.Float64()
- resp.RefluxValue, _ = refluxValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
- return
- }
|