diff --git a/db/db_appreciation_flow.go b/db/db_appreciation_flow.go new file mode 100644 index 0000000..3263b3b --- /dev/null +++ b/db/db_appreciation_flow.go @@ -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{}) + } +} diff --git a/db/model/appreciation_base.go b/db/model/appreciation_base.go new file mode 100644 index 0000000..321755a --- /dev/null +++ b/db/model/appreciation_base.go @@ -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)"` +} diff --git a/db/model/appreciation_flow.go b/db/model/appreciation_flow.go new file mode 100644 index 0000000..6dbe522 --- /dev/null +++ b/db/model/appreciation_flow.go @@ -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"` +} diff --git a/enum/appreciation.go b/enum/appreciation.go new file mode 100644 index 0000000..31abe97 --- /dev/null +++ b/enum/appreciation.go @@ -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 "未知状态" + } +} diff --git a/md/appreciation.go b/md/appreciation.go new file mode 100644 index 0000000..cec3233 --- /dev/null +++ b/md/appreciation.go @@ -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"` +} diff --git a/rule/appreciation.go b/rule/appreciation.go new file mode 100644 index 0000000..9476b4d --- /dev/null +++ b/rule/appreciation.go @@ -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 +}