|
|
@@ -5,7 +5,6 @@ import ( |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" |
|
|
|
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/service_award_dividend/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" |
|
|
|
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/implement" |
|
|
@@ -39,158 +38,59 @@ func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.J |
|
|
|
err = errors.New("服务奖分红未开启!") |
|
|
|
return |
|
|
|
} |
|
|
|
var generalVipAllocationSetStruct md.GeneralVipAllocationSetStruct //普通会员设置 |
|
|
|
err = json.Unmarshal([]byte(base.GeneralVipAllocationSet), &generalVipAllocationSetStruct) |
|
|
|
var vipAllocationSetStruct []md.VipAllocationSetStruct //会员分配设置 |
|
|
|
err = json.Unmarshal([]byte(base.VipAllocationSet), &vipAllocationSetStruct) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
var seniorVipAllocationSetStruct []md.SeniorVipAllocationSetStruct //高级会员设置 |
|
|
|
err = json.Unmarshal([]byte(base.SeniorVipAllocationSet), &seniorVipAllocationSetStruct) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
var seniorVipIdsStruct md.SeniorVipIdsStruct //高级会员ids |
|
|
|
err = json.Unmarshal([]byte(base.SeniorVipIds), &seniorVipIdsStruct) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
var levelMap = map[int]model.UserLevel{} |
|
|
|
lvList, err := db.UserLevelInIDescByWeightLow(eg) |
|
|
|
for _, v := range lvList { |
|
|
|
levelMap[v.Id] = *v |
|
|
|
} |
|
|
|
|
|
|
|
//1、判断是否有直属推荐人 |
|
|
|
if req.ParentUid != 0 { |
|
|
|
//2、判断直属推荐人是否为销售型会员 |
|
|
|
//1、判断是否"推荐人优先" |
|
|
|
if req.ParentUid != 0 && base.AllocationPriority == 1 { |
|
|
|
//2、判断当前用户和父级用户谁的会员等级更高 |
|
|
|
parentUser, err := db.DbsUserFindByUid(eg, req.ParentUid) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if parentUser == nil { |
|
|
|
return errors.New("未查询到相应上级会员记录") |
|
|
|
var levelMap = map[int]model.UserLevel{} |
|
|
|
lvList, err := db.UserLevelInIDescByWeightLow(eg) |
|
|
|
for _, v := range lvList { |
|
|
|
levelMap[v.Id] = *v |
|
|
|
} |
|
|
|
if zhios_order_relate_utils.InArrToInt(parentUser.Level, seniorVipIdsStruct) { |
|
|
|
//3、判断当前用户和父级用户谁的会员等级更高 |
|
|
|
if levelMap[user.Level].LevelWeight >= levelMap[parentUser.Level].LevelWeight { |
|
|
|
//4、判断当前用户是"普通会员" or "高级会员" |
|
|
|
if zhios_order_relate_utils.InArrToInt(user.Level, seniorVipIdsStruct) { |
|
|
|
err = AutoAddGeneralVip(eg, user, req.ParentUid, generalVipAllocationSetStruct) |
|
|
|
|
|
|
|
if levelMap[user.Level].LevelWeight >= levelMap[parentUser.Level].LevelWeight { |
|
|
|
for _, vv := range vipAllocationSetStruct { |
|
|
|
if vv.FromVipId == user.Level { |
|
|
|
err = AutoAddVip(eg, user, req.ParentUid, vv) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
for _, vv := range seniorVipAllocationSetStruct { |
|
|
|
if vv.FromVipId == user.Level { |
|
|
|
err = AutoAddSeniorVip(eg, user, req.ParentUid, vv) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
err = AddVipToParent(eg, user, req.ParentUid) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
//5、判断当前用户是"普通会员" or "高级会员" |
|
|
|
if zhios_order_relate_utils.InArrToInt(user.Level, seniorVipIdsStruct) { |
|
|
|
err = AutoAddGeneralVip(eg, user, req.ParentUid, generalVipAllocationSetStruct) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
for _, vv := range seniorVipAllocationSetStruct { |
|
|
|
if vv.FromVipId == user.Level { |
|
|
|
err = AutoAddSeniorVip(eg, user, req.ParentUid, vv) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
//2、判断当前用户是"普通会员" or "高级会员" |
|
|
|
if zhios_order_relate_utils.InArrToInt(user.Level, seniorVipIdsStruct) { |
|
|
|
err = AutoAddGeneralVip(eg, user, req.ParentUid, generalVipAllocationSetStruct) |
|
|
|
err = AddVipToParent(eg, user, req.ParentUid) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
for _, vv := range seniorVipAllocationSetStruct { |
|
|
|
if vv.FromVipId == user.Level { |
|
|
|
err = AutoAddSeniorVip(eg, user, req.ParentUid, vv) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
for _, vv := range vipAllocationSetStruct { |
|
|
|
if vv.FromVipId == user.Level { |
|
|
|
err = AutoAddVip(eg, user, req.ParentUid, vv) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// AutoAddGeneralVip 自动添加普通会员 |
|
|
|
func AutoAddGeneralVip(engine *xorm.Engine, user *model.User, parentUid int, generalVipAllocationSet md.GeneralVipAllocationSetStruct) (err error) { |
|
|
|
//1、查询出当前应该分配给哪个上级会员 |
|
|
|
var serviceAwardDividendRelation models.ServiceAwardDividendRelation |
|
|
|
has, err := engine.In("vip_level", generalVipAllocationSet).Asc("allocation_nums").Get(&serviceAwardDividendRelation) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if has == false { |
|
|
|
return errors.New("未查询到合适的绑定上级会员用户!") |
|
|
|
} |
|
|
|
|
|
|
|
now := time.Now() |
|
|
|
sess := engine.NewSession() |
|
|
|
defer func() { |
|
|
|
sess.Close() |
|
|
|
if err := recover(); err != nil { |
|
|
|
|
|
|
|
_ = zhios_order_relate_logx.Error(err) |
|
|
|
} |
|
|
|
}() |
|
|
|
sess.Begin() |
|
|
|
|
|
|
|
//2、新增 service_award_dividend_relation 记录 |
|
|
|
serviceAwardDividendRelationDb := implement.NewServiceAwardDividendRelationDb(engine) |
|
|
|
_, err = serviceAwardDividendRelationDb.InsertServiceAwardDividendRelationBySess(sess, &models.ServiceAwardDividendRelation{ |
|
|
|
Uid: user.Uid, |
|
|
|
ParentUid: parentUid, |
|
|
|
BindUid: serviceAwardDividendRelation.Uid, |
|
|
|
VipLevel: user.Level, |
|
|
|
AllocationNums: 0, |
|
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
sess.Rollback() |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//3、修改 绑定上级的 service_award_dividend_relation 记录 |
|
|
|
serviceAwardDividendRelation.AllocationNums++ |
|
|
|
updateRelationAfftecd, err := serviceAwardDividendRelationDb.SaveServiceAwardDividendRelation(sess, serviceAwardDividendRelation.Id, &serviceAwardDividendRelation, "allocation_nums") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if updateRelationAfftecd <= 0 { |
|
|
|
return errors.New("更新 绑定上级的 service_award_dividend_relation 记录 失败!") |
|
|
|
} |
|
|
|
return sess.Commit() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// AutoAddSeniorVip 自动添加高级会员 |
|
|
|
func AutoAddSeniorVip(engine *xorm.Engine, user *model.User, parentUid int, seniorVipAllocationSet md.SeniorVipAllocationSetStruct) (err error) { |
|
|
|
// AutoAddVip 自动添加会员 |
|
|
|
func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocationSetStruct md.VipAllocationSetStruct) (err error) { |
|
|
|
//1、查询出当前应该分配给哪个上级会员 |
|
|
|
var serviceAwardDividendRelation models.ServiceAwardDividendRelation |
|
|
|
has, err := engine.In("vip_level", seniorVipAllocationSet.ToVipId).Asc("allocation_nums").Get(&serviceAwardDividendRelation) |
|
|
|
has, err := engine.In("vip_level", vipAllocationSetStruct.ToVipId).Asc("allocation_nums").Get(&serviceAwardDividendRelation) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|