@@ -0,0 +1,8 @@ | |||
<component name="InspectionProjectProfileManager"> | |||
<profile version="1.0"> | |||
<option name="myName" value="Project Default" /> | |||
<inspection_tool class="SqlResolveInspection" enabled="true" level="ERROR" enabled_by_default="true"> | |||
<option name="suppressForPossibleStringLiterals" value="true" /> | |||
</inspection_tool> | |||
</profile> | |||
</component> |
@@ -0,0 +1,9 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project version="4"> | |||
<component name="SqlDialectMappings"> | |||
<file url="file://$PROJECT_DIR$/rule/service_award_dividend/reset.go" dialect="MySQL" /> | |||
</component> | |||
<component name="SqlResolveMappings"> | |||
<file url="file://$PROJECT_DIR$/rule/service_award_dividend/reset.go" scope="{"node":{ "@negative":"1", "group":{ "@kind":"root", "node":{ "@negative":"1" } } }}" /> | |||
</component> | |||
</project> |
@@ -23,7 +23,7 @@ func Init(redisAddr string) (err error) { | |||
return | |||
} | |||
func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.JoinServiceAwardDividendRelationReq) (err error) { | |||
func JoinServiceAwardDividendRelation(eg *xorm.Engine, req md.JoinServiceAwardDividendRelationReq) (err error) { | |||
user, err := db.DbsUserFindByUid(eg, req.Uid) | |||
if err != nil { | |||
return err | |||
@@ -43,6 +43,9 @@ func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.J | |||
if err != nil { | |||
return err | |||
} | |||
if req.CreateAt == "" { | |||
req.CreateAt = time.Now().Format("2006-01-02 15:04:05") | |||
} | |||
//1、判断是否"推荐人优先" | |||
if req.ParentUid != 0 && base.AllocationPriority == 1 { | |||
@@ -60,14 +63,14 @@ func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.J | |||
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, req.RegionId) | |||
err = AutoAddVip(eg, user, req.ParentUid, vv, req.RegionId, req.CreateAt) | |||
if err != nil { | |||
return err | |||
} | |||
} | |||
} | |||
} else { | |||
err = AddVipToParent(eg, user, req.ParentUid, req.RegionId) | |||
err = AddVipToParent(eg, user, req.ParentUid, req.RegionId, req.CreateAt) | |||
if err != nil { | |||
return err | |||
} | |||
@@ -75,7 +78,7 @@ func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.J | |||
} else { | |||
for _, vv := range vipAllocationSetStruct { | |||
if vv.FromVipId == user.Level { | |||
err = AutoAddVip(eg, user, req.ParentUid, vv, req.RegionId) | |||
err = AutoAddVip(eg, user, req.ParentUid, vv, req.RegionId, req.CreateAt) | |||
if err != nil { | |||
return err | |||
} | |||
@@ -87,7 +90,7 @@ func JoinServiceAwardDividendRelation(eg *xorm.Engine, masterId string, req md.J | |||
} | |||
// AutoAddVip 自动添加会员 | |||
func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocationSetStruct md.VipAllocationSetStruct, regionId int) (err error) { | |||
func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocationSetStruct md.VipAllocationSetStruct, regionId int, createAt string) (err error) { | |||
//1、查询出当前应该分配给哪个上级会员 | |||
var serviceAwardDividendRelation models.ServiceAwardDividendRelation | |||
var hasServiceAwardDividendRelation bool | |||
@@ -126,7 +129,7 @@ func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocat | |||
VipLevel: user.Level, | |||
AllocationNums: 0, | |||
RegionId: regionId, | |||
CreateAt: now.Format("2006-01-02 15:04:05"), | |||
CreateAt: createAt, | |||
UpdateAt: now.Format("2006-01-02 15:04:05"), | |||
}) | |||
if err != nil { | |||
@@ -144,7 +147,7 @@ func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocat | |||
VipLevel: user.Level, | |||
AllocationNums: 0, | |||
RegionId: regionId, | |||
CreateAt: now.Format("2006-01-02 15:04:05"), | |||
CreateAt: createAt, | |||
UpdateAt: now.Format("2006-01-02 15:04:05"), | |||
}) | |||
if err != nil { | |||
@@ -167,7 +170,7 @@ func AutoAddVip(engine *xorm.Engine, user *model.User, parentUid int, vipAllocat | |||
} | |||
// AddVipToParent 添加会员至父级 | |||
func AddVipToParent(engine *xorm.Engine, user *model.User, parentUid int, regionId int) (err error) { | |||
func AddVipToParent(engine *xorm.Engine, user *model.User, parentUid int, regionId int, createAt string) (err error) { | |||
//1、查询出当前应该分配给哪个上级会员 | |||
var serviceAwardDividendRelation models.ServiceAwardDividendRelation | |||
has, err := engine.Where("uid =?", parentUid).Get(&serviceAwardDividendRelation) | |||
@@ -198,7 +201,7 @@ func AddVipToParent(engine *xorm.Engine, user *model.User, parentUid int, region | |||
VipLevel: user.Level, | |||
AllocationNums: 0, | |||
RegionId: regionId, | |||
CreateAt: now.Format("2006-01-02 15:04:05"), | |||
CreateAt: createAt, | |||
UpdateAt: now.Format("2006-01-02 15:04:05"), | |||
}) | |||
if err != nil { | |||
@@ -0,0 +1,8 @@ | |||
package md | |||
const ServiceAwardDividendExchange = "service.award.dividend" | |||
const ( | |||
ServiceAwardDividendRoutKeyForRelation = "service_award_dividend_relation" // 用户分配关系 | |||
) |
@@ -6,7 +6,9 @@ type VipAllocationSetStruct struct { | |||
} | |||
type JoinServiceAwardDividendRelationReq struct { | |||
Uid int `json:"uid"` //用户id | |||
ParentUid int `json:"parent_uid"` //父级id | |||
RegionId int `json:"region_id"` //用户区域id | |||
MasterId string `json:"master_id"` //站长ID | |||
Uid int `json:"uid"` //用户id | |||
ParentUid int `json:"parent_uid"` //父级id | |||
RegionId int `json:"region_id"` //用户区域id | |||
CreateAt string `json:"create_at"` //创建时间 | |||
} |
@@ -0,0 +1,133 @@ | |||
package service_award_dividend | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" | |||
"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/rule/service_award_dividend/md" | |||
"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" | |||
"errors" | |||
"fmt" | |||
"math/rand" | |||
"time" | |||
"xorm.io/xorm" | |||
) | |||
const ResetServiceAwardDividendRelationLockKey = "reset_service_award_dividend_relation_lock_key" | |||
const ResetServiceAwardDividendRelationLockValue = "running" | |||
func ResetServiceAwardDividendRelation(ch *rabbit.Channel, engine *xorm.Engine, dbName string) (err error) { | |||
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!ResetServiceAwardDividendRelation----->dbName!!!!!!!!!!!!!!!!!!!!", dbName) | |||
//TODO::增加“悲观锁”防止串行 | |||
getString, _ := cache.GetString(ResetServiceAwardDividendRelationLockKey) | |||
//if err != nil { | |||
// return err | |||
//} | |||
if getString == ResetServiceAwardDividendRelationLockValue { | |||
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") | |||
return errors.New("上一次结算未执行完") | |||
} | |||
cache.SetEx(ResetServiceAwardDividendRelationLockKey, ResetServiceAwardDividendRelationLockValue, 3600*12) //12小时 | |||
//1、查找 `service_award_dividend_basic` 基础设置 | |||
serviceAwardDividendBasicDb := implement.NewServiceAwardDividendBasicDb(engine) | |||
base, _ := serviceAwardDividendBasicDb.GetServiceAwardDividendBasic() | |||
if base.IsOpen != 1 { | |||
err = errors.New("服务奖分红未开启!") | |||
return | |||
} | |||
now := time.Now() | |||
today := now.Format("2006-01-02") | |||
if base.RefreshAllocation != today { | |||
return errors.New("非重新分配时间!") | |||
} | |||
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", now.Hour()) | |||
if now.Hour() > 1 && now.Hour() < 8 { | |||
//TODO::只在凌晨一点 ~ 凌晨 8 点运行 | |||
return errors.New("非运行时间") | |||
} | |||
//2、将现有数据全部推至 RabbitMq 消费 | |||
var page = 1 | |||
var pageSize = 1000 | |||
for { | |||
var list []models.ServiceAwardDividendRelation | |||
err = engine.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&list) | |||
if err != nil { | |||
fmt.Println("err:::::1111", err) | |||
return | |||
} | |||
if len(list) <= 0 { | |||
break | |||
} | |||
for _, v := range list { | |||
//TODO::推入rabbitmq 异步处理 | |||
ch.Publish(md.ServiceAwardDividendExchange, md.JoinServiceAwardDividendRelationReq{ | |||
MasterId: dbName, | |||
Uid: v.Uid, | |||
ParentUid: v.ParentUid, | |||
RegionId: v.RegionId, | |||
CreateAt: v.CreateAt, | |||
}, md.ServiceAwardDividendRoutKeyForRelation) | |||
} | |||
} | |||
//3、进行数据清理 | |||
session := engine.NewSession() | |||
defer func() { | |||
session.Close() | |||
if err := recover(); err != nil { | |||
_ = zhios_order_relate_logx.Error(err) | |||
} | |||
}() | |||
session.Begin() | |||
sql1 := "DROP TABLE `service_award_dividend_relation_1`" //删除备份表 | |||
sql2 := "DROP TABLE `service_award_dividend_relation_tmp`" //删除临时表 | |||
sql3 := "CREATE TABLE `service_award_dividend_relation_tmp` LIKE `service_award_dividend_relation`" //复制表结构 | |||
sql4 := "ALTER TABLE `service_award_dividend_relation` RENAME TO `service_award_dividend_relation_1`;" //将现有表重命名 -> 备份表 | |||
sql5 := "ALTER TABLE `service_award_dividend_relation_tmp` RENAME TO `service_award_dividend_relation`;" //将临时表重命名 -> 现有表 | |||
_, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql1) | |||
if err != nil { | |||
_ = session.Rollback() | |||
fmt.Println("err___SQl1", err.Error()) | |||
} | |||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) | |||
_, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql2) | |||
if err != nil { | |||
_ = session.Rollback() | |||
fmt.Println("err___SQl2", err.Error()) | |||
} | |||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) | |||
_, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql3) | |||
if err != nil { | |||
_ = session.Rollback() | |||
return err | |||
} | |||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) | |||
_, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql4) | |||
if err != nil { | |||
_ = session.Rollback() | |||
return err | |||
} | |||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) | |||
_, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql5) | |||
if err != nil { | |||
_ = session.Rollback() | |||
return err | |||
} | |||
err = session.Commit() | |||
if err != nil { | |||
_ = session.Rollback() | |||
return err | |||
} | |||
return | |||
} |