huangjiajun vor 9 Monaten
Ursprung
Commit
a150480919
1 geänderte Dateien mit 23 neuen und 18 gelöschten Zeilen
  1. +23
    -18
      rule/consume_integral_settlement.go

+ 23
- 18
rule/consume_integral_settlement.go Datei anzeigen

@@ -29,7 +29,7 @@ const PessimismLockKeyForConsumeIntegral = "daily_settlement_consume_integral_lo
const PessimismLockValueForConsumeIntegral = "running"

// DailySettlementBlockConsumeIntegral 每日结算“消费积分”
func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask bool) (err error) {
func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask bool) (err error, totalMap map[int]decimal.Decimal) {
session := engine.NewSession()
defer func() {
session.Close()
@@ -57,19 +57,19 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
if isTask && (now.Hour() > 8 || now.Before(settleTime)) {
session.Rollback()
//TODO::结算时间 ~ 凌晨 8 点运行
return errors.New("非运行时间")
return errors.New("非运行时间"), totalMap
}
if isTask && zhios_order_relate_utils.InArr(now.Weekday().String(), []string{"Sunday", "Saturday"}) && subsidyBase.HolidaySettleOpen != 1 {
session.Rollback()
return errors.New("周末不运行")
return errors.New("周末不运行"), totalMap
}
if err != nil {
_ = session.Rollback()
return err
return err, totalMap
}
if isTask && subsidyBase.SettlementDate == today {
_ = session.Rollback()
return errors.New("今日“消费计费”已结算")
return errors.New("今日“消费计费”已结算"), totalMap
}
key := fmt.Sprintf(PessimismLockKeyForConsumeIntegral, mid)
//TODO::增加“悲观锁”防止串行
@@ -79,7 +79,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
//}
if getString == PessimismLockValueForConsumeIntegral {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
return errors.New("上一次结算未执行完")
return errors.New("上一次结算未执行完"), totalMap
}
cache.SetEx(key, PessimismLockValueForConsumeIntegral, 3600*8) //8小时

@@ -94,12 +94,14 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask

return
}
//体验
var subsidyWithUserForExperienceList []model.SubsidyWithUser
hasExperienceTotal, err := session.Where("date <= ?", startAt).And("kind = ? and is_can_subsidy=?", 2, 1).FindAndCount(&subsidyWithUserForExperienceList)
if err != nil {
_ = session.Rollback()
return
}
//门店
var subsidyWithUserForStoreList []model.SubsidyWithUser
hasStoreTotal, err := session.Where("date <= ?", startAt).And("kind = ? and is_can_subsidy=?", 3, 1).FindAndCount(&subsidyWithUserForStoreList)
if err != nil {
@@ -115,17 +117,18 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
consumeValue, err = calcNowEverydayConsumeIntegral(subsidyBase, total)
if err != nil {
_ = session.Rollback()
return err
return err, totalMap
}
if hasConsumeTotal > 0 {
for _, v := range subsidyWithUserForConsumeList {
consumeTotalMap[v.Uid] = consumeTotalMap[v.Uid].Add(decimal.NewFromFloat(consumeValue))
totalMap[v.Uid] = totalMap[v.Uid].Add(decimal.NewFromFloat(consumeValue))
consumeTotalCountMap[v.Uid]++
consumeValueTotal = consumeValueTotal.Add(decimal.NewFromFloat(consumeValue))
err1 := DealUserConsumeIntegral(session, &v, consumeValue, mid)
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -135,7 +138,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
err1 := DealUserConsumeAmountFlow(session, k, zhios_order_relate_utils.StrToFloat64(v.String()), mid, zhios_order_relate_utils.IntToStr(consumeTotalCountMap[k]))
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -147,16 +150,17 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
experienceValue, err = calcNowEverydayExperienceIntegral(consumeValue)
if err != nil {
_ = session.Rollback()
return err
return err, totalMap
}
for _, v := range subsidyWithUserForExperienceList {
experienceTotalCountMap[v.Uid]++
totalMap[v.Uid] = totalMap[v.Uid].Add(decimal.NewFromFloat(experienceValue))
experienceTotalMap[v.Uid] = experienceTotalMap[v.Uid].Add(decimal.NewFromFloat(experienceValue))
experienceValueTotal = experienceValueTotal.Add(decimal.NewFromFloat(experienceValue))
err1 := DealUserExperienceIntegral(session, &v, experienceValue, mid)
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -166,7 +170,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
err1 := DealUserExperienceAmountFlow(session, k, zhios_order_relate_utils.StrToFloat64(v.String()), mid, zhios_order_relate_utils.IntToStr(experienceTotalCountMap[k]))
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -180,7 +184,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
storeValue, err = calcNowEverydayStoreIntegral(consumeValue, zhios_order_relate_utils.StrToFloat64(subsidyBase.StoreIntegral)/zhios_order_relate_utils.StrToFloat64(subsidyBase.ConsumptionIntegral))
if err != nil {
_ = session.Rollback()
return err
return err, totalMap
}
for _, v := range subsidyWithUserForStoreList {
storeTotalCountMap[v.Uid]++
@@ -189,7 +193,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
err1 := DealUserStoreIntegral(session, &v, storeValue, mid)
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -199,7 +203,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
err1 := DealUserStoreAmountFlow(session, k, zhios_order_relate_utils.StrToFloat64(v.String()), mid, zhios_order_relate_utils.IntToStr(experienceTotalCountMap[k]))
if err1 != nil {
_ = session.Rollback()
return err1
return err1, totalMap
}
}
}
@@ -220,19 +224,20 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask
}
if updateAffected <= 0 {
_ = session.Rollback()
return errors.New("修改 subsidy_base 失败")
return errors.New("修改 subsidy_base 失败"), totalMap
}

//4、提交事务
err = session.Commit()
if err != nil {
_ = session.Rollback()
return errors.New("事务提交失败")
return errors.New("事务提交失败"), totalMap
}

cache.Del(key)
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>消费积分结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
return nil

return nil, totalMap
}

//计算当前消费积分价值(公式 "((资金池的金额 * 浮动比例) / 消费补贴总数) + 基础补贴")


Laden…
Abbrechen
Speichern