|
@@ -0,0 +1,289 @@ |
|
|
|
|
|
package service_award_dividend |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db" |
|
|
|
|
|
"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" |
|
|
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/models" |
|
|
|
|
|
"encoding/json" |
|
|
|
|
|
"errors" |
|
|
|
|
|
"time" |
|
|
|
|
|
"xorm.io/xorm" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func Init(redisAddr string) (err error) { |
|
|
|
|
|
if redisAddr != "" { |
|
|
|
|
|
cache.NewRedis(redisAddr) |
|
|
|
|
|
} |
|
|
|
|
|
_, err = cache.SelectDb(md2.RedisDataBase) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.JoinServiceAwardDividendRelationReq) (err error) { |
|
|
|
|
|
user, err := db.DbsUserFindByUid(eg, req.Uid) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
if user == nil { |
|
|
|
|
|
return errors.New("未查询到相应会员记录") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
serviceAwardDividendBasicDb := implement.NewServiceAwardDividendBasicDb(eg) |
|
|
|
|
|
base, _ := serviceAwardDividendBasicDb.GetServiceAwardDividendBasic() |
|
|
|
|
|
if base.IsOpen != 1 { |
|
|
|
|
|
err = errors.New("服务奖分红未开启!") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
var generalVipAllocationSetStruct md.GeneralVipAllocationSetStruct //普通会员设置 |
|
|
|
|
|
err = json.Unmarshal([]byte(base.GeneralVipAllocationSet), &generalVipAllocationSetStruct) |
|
|
|
|
|
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、判断直属推荐人是否为销售型会员 |
|
|
|
|
|
parentUser, err := db.DbsUserFindByUid(eg, req.ParentUid) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
if parentUser == nil { |
|
|
|
|
|
return errors.New("未查询到相应上级会员记录") |
|
|
|
|
|
} |
|
|
|
|
|
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 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) |
|
|
|
|
|
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 |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
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() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AutoAddSeniorVip 自动添加高级会员 |
|
|
|
|
|
func AutoAddSeniorVip(engine *xorm.Engine, user *model.User, parentUid int, seniorVipAllocationSet md.SeniorVipAllocationSetStruct) (err error) { |
|
|
|
|
|
//1、查询出当前应该分配给哪个上级会员 |
|
|
|
|
|
var serviceAwardDividendRelation models.ServiceAwardDividendRelation |
|
|
|
|
|
has, err := engine.In("vip_level", seniorVipAllocationSet.ToVipId).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() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AddVipToParent 添加会员至父级 |
|
|
|
|
|
func AddVipToParent(engine *xorm.Engine, user *model.User, parentUid int) (err error) { |
|
|
|
|
|
//1、查询出当前应该分配给哪个上级会员 |
|
|
|
|
|
var serviceAwardDividendRelation models.ServiceAwardDividendRelation |
|
|
|
|
|
has, err := engine.Where("uid =?", parentUid).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() |
|
|
|
|
|
} |