@@ -10,3 +10,9 @@ func QueryNativeStringSess(sess *xorm.Session, sql string, args ...interface{}) | |||
results, err := sess.SQL(sql, args...).QueryString() | |||
return results, err | |||
} | |||
// InsertCommWithSession common insert | |||
func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) { | |||
row, err := session.InsertOne(model) | |||
return row, err | |||
} |
@@ -36,6 +36,14 @@ func AddCapitalPool(engine *xorm.Engine, pool *model.CapitalPoolAgent) (int64, e | |||
one, err := engine.InsertOne(pool) | |||
return one, err | |||
} | |||
func AddCapitalPoolSess(sess *xorm.Session, pool *model.NewCapitalPool) (int64, error) { | |||
one, err := sess.InsertOne(pool) | |||
return one, err | |||
} | |||
func AddCapitalPoolNew(eg *xorm.Engine, pool *model.NewCapitalPool) (int64, error) { | |||
one, err := eg.InsertOne(pool) | |||
return one, err | |||
} | |||
//编辑资金池基础设置 | |||
func UpdateCapitalPool(engine *xorm.Engine, pool *model.CapitalPoolAgent) (int64, error) { | |||
@@ -54,7 +62,7 @@ func UpdateCapitalPool(engine *xorm.Engine, pool *model.CapitalPoolAgent) (int64 | |||
return update, err | |||
} | |||
func InitNewCapitalPool(engine *xorm.Engine) error { | |||
pool := &model.CapitalPoolAgent{ | |||
pool := &model.NewCapitalPool{ | |||
IsUse: 0, | |||
IsAuto: 0, | |||
BonusType: "1", | |||
@@ -66,7 +74,29 @@ func InitNewCapitalPool(engine *xorm.Engine) error { | |||
UpdateAt: time.Now(), | |||
SettleCardinality: "commission", | |||
} | |||
addCapitalPool, err := AddCapitalPool(engine, pool) | |||
addCapitalPool, err := AddCapitalPoolNew(engine, pool) | |||
if err != nil { | |||
return err | |||
} | |||
if addCapitalPool != 1 { | |||
return errors.New("插入条数有误!") | |||
} | |||
return nil | |||
} | |||
func InitNewCapitalPoolSess(sess *xorm.Session) error { | |||
pool := &model.NewCapitalPool{ | |||
IsUse: 0, | |||
IsAuto: 0, | |||
BonusType: "1", | |||
BonusDateType: 1, | |||
BonusTime: "", | |||
BonusLevelType: 1, | |||
UserLevelGroup: "", | |||
CreateAt: time.Now(), | |||
UpdateAt: time.Now(), | |||
SettleCardinality: "commission", | |||
} | |||
addCapitalPool, err := AddCapitalPoolSess(sess, pool) | |||
if err != nil { | |||
return err | |||
} | |||
@@ -129,3 +159,29 @@ func GetNewCapitalPool(engine *xorm.Engine) (pool model.NewCapitalPool, err erro | |||
} | |||
return | |||
} | |||
func GetNewCapitalPoolSess(sess *xorm.Session) (pool model.NewCapitalPool, err error) { | |||
get, err := sess.Where("1 = 1").Get(&pool) | |||
if err != nil { | |||
return model.NewCapitalPool{}, err | |||
} | |||
if !get { | |||
err := InitNewCapitalPoolSess(sess) | |||
if err != nil { | |||
return model.NewCapitalPool{}, err | |||
} | |||
capitalPool, err := GetNewCapitalPoolSess(sess) | |||
if err != nil { | |||
return model.NewCapitalPool{}, err | |||
} | |||
if capitalPool.SettleCardinality == "" { | |||
capitalPool.SettleCardinality = "commission" | |||
} | |||
return capitalPool, err | |||
} else { | |||
if pool.SettleCardinality == "" { | |||
pool.SettleCardinality = "commission" | |||
} | |||
} | |||
return | |||
} |
@@ -2,6 +2,7 @@ package db | |||
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" | |||
"errors" | |||
"time" | |||
@@ -55,3 +56,38 @@ func GetCapitalPoolLossMoney(sess *xorm.Session, uid string) (*model.CapitalPool | |||
return &data, nil | |||
} | |||
func SelfBuyStatisticalBonusValue(sess *xorm.Session) (poolDepositSum, poolBonusSum, nowDepositSum, poolBonusSumOut float64) { | |||
var ( | |||
capitalPool model.NewCapitalPoolOrd | |||
bonusCapital model.NewCapitalPoolBonus | |||
bonusCapitalOut model.NewCapitalPoolBonus | |||
err error | |||
) | |||
poolDepositSum, err = sess.Where("commission_type = ?", "cny").Sum(capitalPool, "deposit_value") | |||
poolBonusSum, err = sess.Sum(bonusCapital, "bonus_value") | |||
poolBonusSumOut, err = sess.Sum(bonusCapitalOut, "out_bonus_value") | |||
if err != nil { | |||
return | |||
} | |||
nowDepositSum = poolDepositSum - (poolBonusSum - poolBonusSumOut) | |||
return | |||
} | |||
func GetBonusValueAndCoin(sess *xorm.Session, capitalPoolCfg *model.NewCapitalPool, money string) *md.PoolBonus { | |||
var ( | |||
bonus md.PoolBonus | |||
) | |||
_, _, nowDepositSum, _ := SelfBuyStatisticalBonusValue(sess) | |||
bonus.OldBonusValue = nowDepositSum | |||
if zhios_order_relate_utils.StrToFloat64(money) > 0 { | |||
nowDepositSum = zhios_order_relate_utils.StrToFloat64(money) | |||
} | |||
if nowDepositSum == 0 { | |||
return nil | |||
} | |||
if capitalPoolCfg != nil { | |||
bonus.SecuritiesId = capitalPoolCfg.SecuritiesId | |||
bonus.BonusValue = nowDepositSum | |||
bonus.BonusLevelType = capitalPoolCfg.BonusLevelType | |||
} | |||
return &bonus | |||
} |
@@ -42,3 +42,18 @@ func UserProfileFindByIDWithSession(sess *xorm.Session, id interface{}) (*model. | |||
} | |||
return &m, nil | |||
} | |||
func UserProfileUpdateWithSession(session *xorm.Session, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) { | |||
var ( | |||
affected int64 | |||
err error | |||
) | |||
if forceCols != nil { | |||
affected, err = session.Where("uid=?", uid).Cols(forceCols...).Update(userProfile) | |||
} else { | |||
affected, err = session.Where("uid=?", uid).Update(userProfile) | |||
} | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} |
@@ -0,0 +1,32 @@ | |||
package model | |||
import ( | |||
"time" | |||
) | |||
type NewCapitalPoolBonus struct { | |||
Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"` | |||
BonusPeriod int `json:"bonus_period" xorm:"not null comment('分红期数') INT(11)"` | |||
BonusValue string `json:"bonus_value" xorm:"not null default 0.000000 comment('分红资金') DECIMAL(12,6)"` | |||
RealBonusValue string `json:"real_bonus_value" xorm:"not null default 0.000000 comment('分红资金') DECIMAL(12,6)"` | |||
LossBonusValue string `json:"loss_bonus_value" xorm:"not null default 0.000000 comment('分红资金') DECIMAL(12,6)"` | |||
OutBonusValue string `json:"out_bonus_value" xorm:"not null default 0.000000 comment('分红资金') DECIMAL(12,6)"` | |||
BonusType int `json:"bonus_type" xorm:"not null default 1 comment('分红类型(1自动,2手动)') TINYINT(1)"` | |||
BonusCoinType string `json:"bonus_coin_type" xorm:"not null default '0' comment('佣金类型(CNY,虚拟币1Id,虚拟币2Id)多个以逗号隔开') VARCHAR(100)"` | |||
BonusUserLevelRecord string `json:"bonus_user_level_record" xorm:"not null comment('分红时资金池设置用户等级分红的json') TEXT"` | |||
BonusUserRecord string `json:"bonus_user_record" xorm:"not null comment('分红时资金池设置用户分红的json') LONGTEXT"` | |||
BonusLevelType int `json:"bonus_level_type" xorm:"not null default 0 comment('用户等级分红类型(1,指定等级,2大于或等于指定等级)') TINYINT(1)"` | |||
BonusUserNum string `json:"bonus_user_num" xorm:"not null comment('分红时,用户分红用户等级,人数,分红类型,平均金额') TEXT"` | |||
Massage string `json:"massage" xorm:"not null comment('分红时错误日志信息') TEXT"` | |||
Comment string `json:"comment" xorm:"default '0' comment('备注') VARCHAR(255)"` | |||
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` | |||
SecondBonusLevelType int `json:"second_bonus_level_type" xorm:"not null default 0 comment('用户等级分红类型(1,指定等级,2大于或等于指定等级)') TINYINT(1)"` | |||
SecondBonusUserLevelRecord string `json:"second_bonus_user_level_record" xorm:"not null comment('分红时资金池设置用户等级分红的json') TEXT"` | |||
SecondBonusUserRecord string `json:"second_bonus_user_record" xorm:"not null comment('分红时资金池设置用户分红的json') LONGTEXT"` | |||
SecondBonusUserNum string `json:"second_bonus_user_num" xorm:"not null comment('分红时,用户分红用户等级,人数,分红类型,平均金额') TEXT"` | |||
SecondMassage string `json:"second_massage" xorm:"not null comment('分红时错误日志信息') TEXT"` | |||
FirstBili string `json:"first_bili" xorm:"not null comment('') DECIMAL(5,2)"` | |||
SecondBili string `json:"second_bili" xorm:"not null comment('') DECIMAL(5,2)"` | |||
QuantityAllocation int `json:"quantity_allocation" xorm:"not null comment('数量分配 0按人数 1按份数') TINYINT(1)"` | |||
} |
@@ -8,6 +8,7 @@ type PoolBonus struct { | |||
QuantityAllocation string `json:"quantity_allocation"` | |||
BonusUserLevelGroup []*BonusUserLevel //分红的前置条件 | |||
BonusUserGroup []*BonusUserGroup //分红的前置条件 | |||
OldBonusValue float64 `json:"old_bonus_value"` //分红金额 | |||
} | |||
type BonusUserLevel struct { | |||
@@ -47,6 +47,35 @@ func Pool(eg *xorm.Engine, price string) error { | |||
session.Rollback() | |||
return err | |||
} | |||
pool, err := db.GetNewCapitalPoolSess(session) | |||
if err != nil || pool.Id == 0 { | |||
session.Rollback() | |||
return errors.New("没有记录") | |||
} | |||
if pool.IsUse == 0 { | |||
session.Rollback() | |||
return errors.New("没有开启") | |||
} | |||
poolBonus := db.GetBonusValueAndCoin(session, &pool, price) | |||
if poolBonus == nil { | |||
session.Rollback() | |||
return errors.New("没有可分配金额") | |||
} | |||
//生成分红记录初始数据 | |||
var bonus model.NewCapitalPoolBonus | |||
bonus.BonusType = 2 | |||
bonus.CreateAt = time.Now() | |||
bonus.BonusValue = price | |||
if poolBonus.BonusValue-poolBonus.OldBonusValue > 0 { | |||
bonus.OutBonusValue = zhios_order_relate_utils.AnyToString(poolBonus.BonusValue - poolBonus.OldBonusValue) | |||
} | |||
one, err := session.InsertOne(&bonus) | |||
if err != nil || one == 0 { | |||
session.Rollback() | |||
return errors.New("失败") | |||
} | |||
//统计数量 | |||
//礼包的 | |||
sql := `SELECT SUM(sum) as sum,uid FROM mall_ord_capital_pool_total WHERE leave_price>0 and type=1 GROUP BY uid;` | |||
@@ -81,16 +110,18 @@ func Pool(eg *xorm.Engine, price string) error { | |||
oneMoney := zhios_order_relate_utils.StrToFloat64(price) / float64(sum) | |||
//金额小的先扣 扣不完 给下一个 | |||
var userMap = make(map[string]float64) | |||
var userSumMap = make(map[string]float64) | |||
for _, v := range ids { | |||
//读取用户的记录 | |||
var leave float64 = 0 | |||
var data []model.MallOrdCapitalPoolTotal | |||
err := eg.Where("uid=? and type=? and leave_price>?", v, 1, 0).Find(&data) | |||
err := session.Where("uid=? and type=? and leave_price>?", v, 1, 0).Find(&data) | |||
if err != nil { | |||
session.Rollback() | |||
return err | |||
} | |||
for _, v1 := range data { | |||
userSumMap[v] += oneMoney | |||
leave, err = OneDoing(session, v1, oneMoney, leave) | |||
if err != nil { | |||
session.Rollback() | |||
@@ -138,6 +169,7 @@ func Pool(eg *xorm.Engine, price string) error { | |||
leave = money | |||
} | |||
if zhios_order_relate_utils.InArr(v["uid"], ids) == false || len(ids) == 0 { | |||
userSumMap[v["uid"]] += oneMoney | |||
var oneData model.MallOrdCapitalPoolTotal | |||
_, err := session.Where("uid=? and type=? ", v["uid"], 0).Get(&oneData) | |||
if err != nil { | |||
@@ -175,7 +207,6 @@ func Pool(eg *xorm.Engine, price string) error { | |||
} | |||
} | |||
} | |||
} | |||
if leave > 0 { | |||
//剩下的进损失 | |||
@@ -184,8 +215,24 @@ func Pool(eg *xorm.Engine, price string) error { | |||
session.Rollback() | |||
return err | |||
} | |||
userSumMap[v["uid"]] -= oneMoney | |||
} | |||
} | |||
for k, v := range userSumMap { | |||
bools := Finflow(session, k, v) | |||
if bools == false { | |||
session.Rollback() | |||
return errors.New("失败") | |||
} | |||
} | |||
bonus.BonusUserNum = zhios_order_relate_utils.IntToStr(sum) | |||
bonus.BonusUserRecord = zhios_order_relate_utils.SerializeStr(userSumMap) | |||
bonus.BonusPeriod = bonus.Id | |||
update, err := session.ID(bonus.Id).Update(&bonus) | |||
if err != nil || update == 0 { | |||
session.Rollback() | |||
return errors.New("失败") | |||
} | |||
session.Commit() | |||
return nil | |||
} | |||
@@ -236,3 +283,44 @@ func AddBonusOrd(engine *xorm.Engine, t *md.BonusOrdParam) error { | |||
} | |||
return nil | |||
} | |||
func Finflow(session *xorm.Session, uid string, amount float64) bool { | |||
beforeAmount := "0" | |||
afterAmount := "0" | |||
var affected int64 = 0 | |||
userProfile, err := db.UserProfileFindByIDWithSession(session, uid) | |||
if err != nil || userProfile == nil { | |||
return false | |||
} | |||
now := time.Now() | |||
ItemTitle := "消费补贴" | |||
finUserFlow := model.FinUserFlow{ | |||
Type: 0, | |||
Uid: zhios_order_relate_utils.StrToInt(uid), | |||
Amount: zhios_order_relate_utils.Float64ToStrByPrec(amount, 9), | |||
BeforeAmount: beforeAmount, | |||
AfterAmount: afterAmount, | |||
OrdType: "self_buy_capital_pool", | |||
OrdId: "", | |||
OrdAction: 104, | |||
OrdDetail: "", | |||
State: 2, | |||
OrdTitle: ItemTitle, | |||
CreateAt: now, | |||
UpdateAt: now, | |||
} | |||
// 更新用户余额 | |||
finUserFlow.BeforeAmount = userProfile.FinValid | |||
userProfile.FinValid = zhios_order_relate_utils.Float64ToStrByPrec(zhios_order_relate_utils.AnyToFloat64(userProfile.FinValid)+zhios_order_relate_utils.AnyToFloat64(amount), 9) | |||
userProfile.FinTotal = userProfile.FinTotal + zhios_order_relate_utils.StrToFloat32(zhios_order_relate_utils.Float64ToStrByPrec(amount, 9)) | |||
affected, err = db.UserProfileUpdateWithSession(session, uid, userProfile, "fin_total", "fin_valid") | |||
finUserFlow.AfterAmount = userProfile.FinValid | |||
has, errs := db.InsertCommWithSession( | |||
session, &finUserFlow) | |||
if affected == 0 || err != nil || errs != nil || has == 0 { | |||
return false | |||
} | |||
return true | |||
} |