Bladeren bron

Merge remote-tracking branch 'origin/master'

master
DengBiao 10 maanden geleden
bovenliggende
commit
78abb61371
1 gewijzigde bestanden met toevoegingen van 34 en 29 verwijderingen
  1. +34
    -29
      rule/consume_integral_settlement.go

+ 34
- 29
rule/consume_integral_settlement.go Bestand weergeven

@@ -25,7 +25,7 @@ func InitForConsumeIntegral(redisAddr string) (err error) {
return
}

const PessimismLockKeyForConsumeIntegral = "daily_settlement_consume_integral_lock_key"
const PessimismLockKeyForConsumeIntegral = "daily_settlement_consume_integral_lock_key:%s"
const PessimismLockValueForConsumeIntegral = "running"

// DailySettlementBlockConsumeIntegral 每日结算“消费积分”
@@ -34,6 +34,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
defer func() {
session.Close()
if err := recover(); err != nil {

_ = zhios_order_relate_logx.Error(err)
}
}()
@@ -70,9 +71,9 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
_ = session.Rollback()
return errors.New("今日“消费计费”已结算")
}
key := fmt.Sprintf(PessimismLockKeyForConsumeIntegral, mid)
//TODO::增加“悲观锁”防止串行
getString, _ := cache.GetString(PessimismLockKeyForConsumeIntegral)
getString, _ := cache.GetString(key)
//if err != nil {
// return err
//}
@@ -80,51 +81,56 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
return errors.New("上一次结算未执行完")
}
cache.SetEx(PessimismLockKeyForConsumeIntegral, PessimismLockValueForConsumeIntegral, 3600*8) //8小时
cache.SetEx(key, PessimismLockValueForConsumeIntegral, 3600*8) //8小时

var consumeValueTotal, experienceValueTotal = decimal.NewFromInt(0), decimal.NewFromInt(0)
//1、统计当前拥有多少份消费补贴
startAt := time.Now().Add(time.Duration(subsidyBase.ConsumptionDay) * -24 * time.Hour).Format("2006-01-02") //起始时间
var subsidyWithUserForConsumeList []model.SubsidyWithUser
hasConsumeTotal, err := session.Where("date <= ?", startAt).And("kind = ? and is_can_subsidy=?", 1, 1).FindAndCount(&subsidyWithUserForConsumeList)
if err != nil {
return
}
consumeValue, err := calcNowEverydayConsumeIntegral(subsidyBase, hasConsumeTotal)
if err != nil {
_ = session.Rollback()
return err
}

for _, v := range subsidyWithUserForConsumeList {
consumeValueTotal = consumeValueTotal.Add(decimal.NewFromFloat(consumeValue))
err1 := DealUserConsumeIntegral(session, &v, consumeValue, mid)
if err1 != nil {
return
}
if hasConsumeTotal > 0 {
consumeValue, err := calcNowEverydayConsumeIntegral(subsidyBase, hasConsumeTotal)
if err != nil {
_ = session.Rollback()
return err1
return err
}
}

for _, v := range subsidyWithUserForConsumeList {
consumeValueTotal = consumeValueTotal.Add(decimal.NewFromFloat(consumeValue))
err1 := DealUserConsumeIntegral(session, &v, consumeValue, mid)
if err1 != nil {
_ = session.Rollback()
return err1
}
}
}
//2、统计当前拥有多少份体验补贴
var subsidyWithUserForExperienceList []model.SubsidyWithUser
hasExperienceTotal, err := session.Where("date <= ?", startAt).And("kind = ? and is_can_subsidy=?", 2, 1).FindAndCount(&subsidyWithUserForExperienceList)
if err != nil {
return
}
experienceValue, err := calcNowEverydayExperienceIntegral(subsidyBase, hasExperienceTotal)
if err != nil {
_ = session.Rollback()
return err
return
}
for _, v := range subsidyWithUserForConsumeList {
experienceValueTotal = experienceValueTotal.Add(decimal.NewFromFloat(experienceValue))
err1 := DealUserExperienceIntegral(session, &v, experienceValue, mid)
if err1 != nil {
if hasExperienceTotal > 0 {
experienceValue, err := calcNowEverydayExperienceIntegral(subsidyBase, hasExperienceTotal)
if err != nil {
_ = session.Rollback()
return err1
return err
}
for _, v := range subsidyWithUserForConsumeList {
experienceValueTotal = experienceValueTotal.Add(decimal.NewFromFloat(experienceValue))
err1 := DealUserExperienceIntegral(session, &v, experienceValue, mid)
if err1 != nil {
_ = session.Rollback()
return err1
}
}
}

//3、修改 subsidy_base 中 consumption_money、experience_money
consumeTotal, _ := consumeValueTotal.Float64()
experienceTotal, _ := experienceValueTotal.Float64()
@@ -148,7 +154,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
return errors.New("事务提交失败")
}

cache.Del(PessimismLockKeyForConsumeIntegral)
cache.Del(key)
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>消费积分结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
return nil
}
@@ -303,5 +309,4 @@ func DealUserExperienceIntegral(session *xorm.Session, subsidyWithUser *model.Su
}
return nil

return nil
}

Laden…
Annuleren
Opslaan