From 72b382228216548033fdd09a5208327352645250 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 28 Dec 2023 17:53:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/consume_integral_settlement.go | 54 ++++++++++++++++------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/rule/consume_integral_settlement.go b/rule/consume_integral_settlement.go index b027d17..db6728d 100644 --- a/rule/consume_integral_settlement.go +++ b/rule/consume_integral_settlement.go @@ -87,50 +87,55 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask 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() subsidyBase.ConsumptionTotalMoney = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(subsidyBase.ConsumptionTotalMoney) - consumeTotal) subsidyBase.ExperienceTotalMoney = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(subsidyBase.ExperienceTotalMoney) - experienceTotal) - updateAffected, err := db.SubsidyBaseUpdate(session, subsidyBase.Id, subsidyBase, "consumption_money", "experience_money") + updateAffected, err := db.SubsidyBaseUpdate(session, subsidyBase.Id, subsidyBase, "consumption_total_money", "experience_total_money") if err != nil { _ = session.Rollback() return @@ -302,5 +307,4 @@ func DealUserExperienceIntegral(session *xorm.Session, subsidyWithUser *model.Su } return nil - return nil } From 7fcc2e77c9e9a1e052a8a8fc315ddd354f55b1a0 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 28 Dec 2023 17:55:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/consume_integral_settlement.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rule/consume_integral_settlement.go b/rule/consume_integral_settlement.go index db6728d..3070be0 100644 --- a/rule/consume_integral_settlement.go +++ b/rule/consume_integral_settlement.go @@ -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,7 +81,7 @@ 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、统计当前拥有多少份消费补贴 @@ -152,7 +153,7 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask return errors.New("事务提交失败") } - cache.Del(PessimismLockKeyForConsumeIntegral) + cache.Del(key) fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>消费积分结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") return nil } From 3fd31802ab4acfec462de6e63553650febc81918 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 28 Dec 2023 17:59:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/consume_integral_settlement.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rule/consume_integral_settlement.go b/rule/consume_integral_settlement.go index 3070be0..b7f9f23 100644 --- a/rule/consume_integral_settlement.go +++ b/rule/consume_integral_settlement.go @@ -136,7 +136,8 @@ func DailySettlementBlockConsumeIntegral(engine *xorm.Engine, mid string, isTask experienceTotal, _ := experienceValueTotal.Float64() subsidyBase.ConsumptionTotalMoney = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(subsidyBase.ConsumptionTotalMoney) - consumeTotal) subsidyBase.ExperienceTotalMoney = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(subsidyBase.ExperienceTotalMoney) - experienceTotal) - updateAffected, err := db.SubsidyBaseUpdate(session, subsidyBase.Id, subsidyBase, "consumption_total_money", "experience_total_money") + subsidyBase.SettlementDate = today + updateAffected, err := db.SubsidyBaseUpdate(session, subsidyBase.Id, subsidyBase, "consumption_total_money", "experience_total_money", "settlement_date") if err != nil { _ = session.Rollback() return