|
|
@@ -0,0 +1,282 @@ |
|
|
|
package svc |
|
|
|
|
|
|
|
import ( |
|
|
|
db2 "applet/app/db" |
|
|
|
"applet/app/db/model" |
|
|
|
"applet/app/utils" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/lib/comm_plan" |
|
|
|
md2 "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/rule" |
|
|
|
svc2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/svc" |
|
|
|
"fmt" |
|
|
|
"github.com/jinzhu/copier" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
func CommGetLvUser(engine *xorm.Engine, CommissionParam md2.CommissionFirstParam, oid int64, masterId string, mapData map[string]string) { |
|
|
|
commArr := rule.GetComm(engine) |
|
|
|
plan, commission, virtualCoinMoneyRate := svc2.GetAllPlan(engine, masterId) |
|
|
|
var CommissionParam1 md2.CommissionParam |
|
|
|
comm := CommissionParam.CommissionParam |
|
|
|
err2 := copier.Copy(&CommissionParam1, &comm) |
|
|
|
fmt.Println(err2) |
|
|
|
_, _, _, _, lvUser, err := svc2.GetRewardCommission(engine, &CommissionParam1, false, CommissionParam.Uid, CommissionParam.Provider, masterId, true, mapData, commArr, plan, commission, virtualCoinMoneyRate) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if lvUser == nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
CommCommOrderRelateInsert(engine, oid, CommissionParam.Provider, int(time.Now().Unix()), lvUser, mapData) |
|
|
|
//sql := `SELECT id from user_virtual_coin_flow where ord_id='%d'` |
|
|
|
//sql = fmt.Sprintf(sql, oid) |
|
|
|
//nativeString, _ := db2.QueryNativeString(engine, sql) |
|
|
|
//has := false |
|
|
|
//if len(nativeString) > 0 && utils.StrToInt(nativeString[0]["id"]) > 0 { |
|
|
|
// has = true |
|
|
|
//} |
|
|
|
//sql1 := `SELECT id from fin_user_flow where ord_id='%d'` |
|
|
|
//sql1 = fmt.Sprintf(sql1, oid) |
|
|
|
//nativeString1, _ := db2.QueryNativeString(engine, sql1) |
|
|
|
//has1 := false |
|
|
|
//if len(nativeString1) > 0 && utils.StrToInt(nativeString1[0]["id"]) > 0 { |
|
|
|
// has1 = true |
|
|
|
//} |
|
|
|
//fmt.Println("===========任务4=============", masterId, CommissionParam.Uid, oid, has, has1) |
|
|
|
//if has == false && has1 == false { |
|
|
|
CommSettleDone(engine, CommissionParam.Provider, oid, masterId, mapData) |
|
|
|
//} |
|
|
|
} |
|
|
|
|
|
|
|
// 分佣表插入获取到的数据 |
|
|
|
func CommCommOrderRelateInsert(eg *xorm.Engine, oid int64, pvd string, createTime int, lvUser *comm_plan.LvUser, mapData map[string]string) { |
|
|
|
level := 0 |
|
|
|
oldLevel := 0 |
|
|
|
fmt.Println(lvUser) |
|
|
|
profit := utils.FloatFormat(lvUser.Profit+lvUser.SubsidyFee, 6) |
|
|
|
oldLvUser := lvUser |
|
|
|
data := []*model.CommOrdListRelate{ |
|
|
|
{ |
|
|
|
Oid: oid, |
|
|
|
Uid: lvUser.Uid, |
|
|
|
Amount: profit, |
|
|
|
Pvd: pvd, |
|
|
|
CreateAt: createTime, |
|
|
|
Level: level, |
|
|
|
}, |
|
|
|
} |
|
|
|
if mapData["reward_type"] == "2" { |
|
|
|
data = make([]*model.CommOrdListRelate, 0) |
|
|
|
} |
|
|
|
mode := mapData["mode"] |
|
|
|
for lvUser.ParentUser != nil { |
|
|
|
lvUser = lvUser.ParentUser |
|
|
|
if lvUser.Uid == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
fmt.Println(lvUser) |
|
|
|
level = level + 1 |
|
|
|
profit = utils.FloatFormat(lvUser.Profit+lvUser.SubsidyFee, 6) |
|
|
|
var additionalSubsidy float64 = 0 |
|
|
|
if utils.InArr(mode, []string{"lv_winery", "public_platoon"}) { |
|
|
|
profit = utils.FloatFormat(lvUser.Profit, 6) |
|
|
|
additionalSubsidy = lvUser.SubsidyFee |
|
|
|
} |
|
|
|
data = append(data, &model.CommOrdListRelate{ |
|
|
|
Oid: oid, |
|
|
|
Uid: lvUser.Uid, |
|
|
|
Amount: profit, |
|
|
|
Pvd: pvd, |
|
|
|
CreateAt: createTime, |
|
|
|
Level: level, |
|
|
|
Mode: mode, |
|
|
|
AdditionalSubsidy: utils.Float64ToStrByPrec(additionalSubsidy, 9), |
|
|
|
ExtendType: lvUser.ExtendType, |
|
|
|
}) |
|
|
|
} |
|
|
|
for _, v := range data { |
|
|
|
if utils.StrToInt(mapData["coin_id_type"]) > 0 { |
|
|
|
v.Amount = 0 |
|
|
|
v.Info = "任务没佣金设置,不返佣金" |
|
|
|
} |
|
|
|
} |
|
|
|
err1 := db2.DbInsertBatch(eg, data) |
|
|
|
if err1 != nil { |
|
|
|
return |
|
|
|
} else { |
|
|
|
// 插入虚拟币数据 |
|
|
|
vcrData := CommCombineVirtualCoinRelateData(oldLvUser, oid, pvd, oldLevel, mapData) |
|
|
|
err2 := db2.DbInsertBatch(eg, vcrData) |
|
|
|
for _, item := range vcrData { |
|
|
|
fmt.Println(item) |
|
|
|
} |
|
|
|
if err2 != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
func CommSettleDone(eg *xorm.Engine, pvd string, oid int64, masterId string, mapData map[string]string) bool { |
|
|
|
fmt.Println("=======================任务=============", masterId, oid, pvd) |
|
|
|
ol, err := db2.CommOrderRelateListByOid(eg, oid, pvd) |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
// 查询虚拟币 virtual_coin_relate 表 |
|
|
|
vcrList, err := db2.GetVirtualCoinRelateListWithOrdId(eg, oid, pvd) |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
session := eg.NewSession() |
|
|
|
defer session.Close() |
|
|
|
if err := session.Begin(); err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
set, _ := db2.SysCfgGetOne(eg, "app_name_cn") |
|
|
|
var appName = "" |
|
|
|
if set != nil { |
|
|
|
appName = set.Val |
|
|
|
} |
|
|
|
for _, item := range ol { |
|
|
|
if mapData["reward_type"] == "2" && item.Level == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
//佣金不为空 |
|
|
|
if item.Amount > 0 { |
|
|
|
//公排 |
|
|
|
if utils.InArr(item.Mode, []string{"lv_commission_public_platoon", "lv_price_public_platoon"}) && item.ExtendType == 5 { |
|
|
|
fmt.Println("======================555") |
|
|
|
bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, 0, item.Amount) |
|
|
|
fmt.Println("======================666", bools) |
|
|
|
if bools == false { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
continue |
|
|
|
} |
|
|
|
_ = CommCommSettleMoney(session, item, appName, masterId, mapData) |
|
|
|
} |
|
|
|
if utils.StrToFloat64(item.AdditionalSubsidy) > 0 { |
|
|
|
if item.Mode == "public_platoon" { //公排 |
|
|
|
bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, 0, utils.StrToFloat64(item.AdditionalSubsidy)) |
|
|
|
if bools == false { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 虚拟币相关操作 |
|
|
|
for _, item := range vcrList { |
|
|
|
if mapData["reward_type"] == "2" && item.Level == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
if utils.StrToFloat64(item.Amount) > 0 { |
|
|
|
if utils.InArr(item.Mode, []string{"lv_commission_public_platoon", "lv_price_public_platoon"}) && item.ExtendType == 5 { |
|
|
|
fmt.Println("======================555") |
|
|
|
bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, item.CoinId, utils.StrToFloat64(item.Amount)) |
|
|
|
fmt.Println("======================666", bools) |
|
|
|
if bools == false { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
continue |
|
|
|
} |
|
|
|
_ = CommSettleVirtualCoin(session, item, mapData) |
|
|
|
} |
|
|
|
//公排 |
|
|
|
if item.Mode == "public_platoon" && utils.StrToFloat64(item.AdditionalSubsidy) > 0 { |
|
|
|
bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, item.CoinId, utils.StrToFloat64(item.AdditionalSubsidy)) |
|
|
|
if bools == false { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 提交事务 |
|
|
|
err = session.Commit() |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
return true |
|
|
|
} |
|
|
|
func CommCommSettleMoney(session *xorm.Session, item *model.CommOrdListRelate, appName, masterId string, mapData map[string]string) bool { |
|
|
|
now := time.Now() |
|
|
|
beforeAmount := "0" |
|
|
|
afterAmount := "0" |
|
|
|
var affected int64 = 0 |
|
|
|
userProfile, err := db2.UserProfileFindByIdWithSession(session, item.Uid) |
|
|
|
if userProfile == nil { |
|
|
|
} |
|
|
|
if err != nil || userProfile == nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
// 获取余额更新锁 |
|
|
|
cb, err := HandleBalanceDistributedLock(masterId, utils.IntToStr(item.Uid), "mall_settle_order") |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err.Error()) |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
// 释放锁 |
|
|
|
if cb != nil { |
|
|
|
defer cb() |
|
|
|
} |
|
|
|
// 开始写入流水 |
|
|
|
var orderAction int |
|
|
|
if item.Level == 0 { |
|
|
|
orderAction = 10 // 自购 |
|
|
|
} else { |
|
|
|
orderAction = 11 // 推广 |
|
|
|
} |
|
|
|
var ItemTitle = mapData["title"] + "奖励" |
|
|
|
if item.Level > 0 { |
|
|
|
ItemTitle = mapData["title"] + "团队奖励" |
|
|
|
} |
|
|
|
finUserFlow := model.FinUserFlow{ |
|
|
|
Type: 0, |
|
|
|
Uid: item.Uid, |
|
|
|
Amount: utils.Float64ToStrByPrec(item.Amount, 8), |
|
|
|
BeforeAmount: beforeAmount, |
|
|
|
AfterAmount: afterAmount, |
|
|
|
OrdType: item.Pvd, |
|
|
|
OrdId: utils.AnyToString(item.Oid), |
|
|
|
OrdAction: orderAction, |
|
|
|
OrdDetail: "", |
|
|
|
State: 2, |
|
|
|
OtherId: item.Id, |
|
|
|
OrdTitle: ItemTitle, |
|
|
|
OrdTime: int(now.Unix()), |
|
|
|
CreateAt: now, |
|
|
|
UpdateAt: now, |
|
|
|
} |
|
|
|
// 更新用户余额 |
|
|
|
finUserFlow.BeforeAmount = userProfile.FinValid |
|
|
|
userProfile.FinValid = utils.Float64ToStrByPrec(utils.AnyToFloat64(userProfile.FinValid)+utils.AnyToFloat64(item.Amount), 8) |
|
|
|
userProfile.FinTotal = userProfile.FinTotal + utils.StrToFloat32(utils.Float64ToStrByPrec(item.Amount, 8)) |
|
|
|
affected, err = db2.UserProfileUpdateWithSession(session, item.Uid, userProfile) |
|
|
|
finUserFlow.AfterAmount = userProfile.FinValid |
|
|
|
has, errs := db2.InsertCommWithSession( |
|
|
|
session, &finUserFlow) |
|
|
|
if affected == 0 || err != nil || errs != nil || has == 0 { |
|
|
|
_ = session.Rollback() |
|
|
|
return false |
|
|
|
} |
|
|
|
if utils.InArr(item.Mode, []string{"public_platoon", "lv_commission_public_platoon", "lv_price_public_platoon"}) { //公排的加一条流水 |
|
|
|
kind := 4 |
|
|
|
title := "自购奖" |
|
|
|
if item.Level == 1 { |
|
|
|
kind = 5 |
|
|
|
title = "直推奖" |
|
|
|
} |
|
|
|
bools := PublicPlatoonAddRecordWithSession(session, title, utils.Int64ToStr(item.Oid), item.Uid, 0, kind, 0, item.Amount, beforeAmount) |
|
|
|
if bools == false { |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
} |