Browse Source

add 增值积分

master
DengBiao 1 year ago
parent
commit
e2396d3073
6 changed files with 207 additions and 0 deletions
  1. +76
    -0
      db/db_appreciation_flow.go
  2. +8
    -0
      db/model/appreciation_base.go
  3. +15
    -0
      db/model/appreciation_flow.go
  4. +26
    -0
      enum/appreciation.go
  5. +16
    -0
      md/appreciation.go
  6. +66
    -0
      rule/appreciation.go

+ 76
- 0
db/db_appreciation_flow.go View File

@@ -0,0 +1,76 @@
package db

import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx"
"reflect"
"xorm.io/xorm"
)

type AppreciationFlowDb struct {
Db *xorm.Engine `json:"db"`
Uid int `json:"uid"`
}

func (appreciationFlowDb *AppreciationFlowDb) Set(db *xorm.Engine, uid int) { // set方法
appreciationFlowDb.Db = db
appreciationFlowDb.Uid = uid
}

func (appreciationFlowDb *AppreciationFlowDb) GetAppreciationFlow(id int) (m *model.AppreciationFlow, err error) {
m = new(model.AppreciationFlow)
has, err := appreciationFlowDb.Db.Where("id =?", id).Get(m)
if err != nil {
return nil, zhios_order_relate_logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (appreciationFlowDb *AppreciationFlowDb) FindAppreciationFlowById(ids interface{}) (*[]model.AppreciationFlow, error) {
var m []model.AppreciationFlow
if err := appreciationFlowDb.Db.In("id", ids).Desc("id").Find(&m); err != nil {
return nil, zhios_order_relate_logx.Error(err)
}
return &m, nil
}

func (appreciationFlowDb *AppreciationFlowDb) FindAppreciationFlow(limit, start int) (*[]model.AppreciationFlow, error) {
var m []model.AppreciationFlow
if limit == 0 || start == 0 {
if err := appreciationFlowDb.Db.Where("uid =?", appreciationFlowDb.Uid).Asc("id").Find(&m); err != nil {
return nil, zhios_order_relate_logx.Error(err)
}
} else {
if err := appreciationFlowDb.Db.Where("uid =?", appreciationFlowDb.Uid).Asc("id").Limit(limit, start).Find(m); err != nil {
return nil, zhios_order_relate_logx.Error(err)
}
}
return &m, nil
}

func (appreciationFlowDb *AppreciationFlowDb) AppreciationFlowInsert(m *model.AppreciationFlow) (int64, error) {
_, err := appreciationFlowDb.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (appreciationFlowDb *AppreciationFlowDb) AppreciationFlowUpdate(m *model.AppreciationFlow, columns ...string) (int64, error) {
affected, err := appreciationFlowDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

func (appreciationFlowDb *AppreciationFlowDb) AppreciationFlowDelete(id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return appreciationFlowDb.Db.In("id", id).Delete(model.AppreciationFlow{})
} else {
return appreciationFlowDb.Db.Where("id = ?", id).Delete(model.AppreciationFlow{})
}
}

+ 8
- 0
db/model/appreciation_base.go View File

@@ -0,0 +1,8 @@
package model

type AppreciationBase struct {
Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"`
IsUse int `json:"is_use" xorm:"not null default 0 comment('是否开启(否:0;是:1)') TINYINT(1)"`
Sum string `json:"sum" xorm:"default 0.0000 comment('总资产') DECIMAL(20,4)"`
FlowSum string `json:"flow_sum" xorm:"default 0.0000 comment('流通资产') DECIMAL(20,4)"`
}

+ 15
- 0
db/model/appreciation_flow.go View File

@@ -0,0 +1,15 @@
package model

type AppreciationFlow struct {
Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"`
CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id') INT(11)"`
Uid int `json:"uid" xorm:"not null default 0 comment('用户id') INT(11)"`
Kind int `json:"kind" xorm:"not null default 1 comment('流水类型') TINYINT(1)"`
Value string `json:"value" xorm:"not null default 0.0000 comment('虚拟币值') DECIMAL(20,4)"`
BeforeFlowSum string `json:"before_flow_sum" xorm:"not null default 0.0000 comment('变动前-流通资产') DECIMAL(20,4)"`
AfterFlowSum string `json:"after_flow_sum" xorm:"not null default 0.0000 comment('变动后-流通资产') DECIMAL(20,4)"`
BeforeSum string `json:"before_sum" xorm:"not null default 0.0000 comment('变动前-总资产') DECIMAL(20,4)"`
AfterSum string `json:"after_sum" xorm:"not null default 0.0000 comment('变动后-总资产') DECIMAL(20,4)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 26
- 0
enum/appreciation.go View File

@@ -0,0 +1,26 @@
package enum

// 增值积分流水种类
type AppreciationFlowKind int

const (
TransferIn AppreciationFlowKind = iota
TransferOut
WithdrawalAndDestroy
WithdrawalAndReflux
)

func (kind AppreciationFlowKind) String() string {
switch kind {
case TransferIn:
return "转入"
case TransferOut:
return "转出"
case WithdrawalAndDestroy:
return "提现销毁"
case WithdrawalAndReflux:
return "提现回流"
default:
return "未知状态"
}
}

+ 16
- 0
md/appreciation.go View File

@@ -0,0 +1,16 @@
package md

import "github.com/shopspring/decimal"

var (
WithdrawalCommissionFee = decimal.NewFromFloat(0.15) //提现手续费
WithdrawalDestroyFee = decimal.NewFromFloat(0.05) //提现销毁增值积分比例
WithdrawalRefluxFee = decimal.NewFromFloat(0.10) //提现回流增值积分比例
)

type DealWithdrawalAndDestroyResp struct {
TransferOut float64 `json:"transfer_out"`
AmountOut float64 `json:"amount_out"`
DestroyValue float64 `json:"destroy_value"`
RefluxValue float64 `json:"reflux_value"`
}

+ 66
- 0
rule/appreciation.go View File

@@ -0,0 +1,66 @@
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"
"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
}

/*
计算增值积分当前价值
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
}
sum, _ := decimal.NewFromString(appreciationBase.Sum) //总资产
flowSum, _ := decimal.NewFromString(appreciationBase.FlowSum) //流通资产
value, _ = sum.Div(flowSum).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, transferOut float64) (err error, resp md.DealWithdrawalAndDestroyResp) {
transferOutValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalCommissionFee)
destroyValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalDestroyFee)
refluxValue := decimal.NewFromFloat(transferOut).Mul(md.WithdrawalRefluxFee)
err, nowValue := CalcAppreciationValue(session)
if err != nil {
return
}
resp.TransferOut = transferOut
resp.AmountOut, _ = transferOutValue.Mul(decimal.NewFromFloat(nowValue)).Float64()
resp.DestroyValue, _ = destroyValue.Float64()
resp.DestroyValue, _ = refluxValue.Float64()
return
}

Loading…
Cancel
Save