From d5f0b4006a34f1c6aca634c5ad7a3b8d60c8b0f3 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 22 May 2024 21:22:55 +0800 Subject: [PATCH] =?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 | 17 ++++- .../happy_orchard_user_seed_record.go | 1 + .../happy_orchard_user_waters.go | 75 ++++++++++++++++--- rule/micro_applications/md/happy_orchard.go | 6 +- 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/rule/consume_integral_settlement.go b/rule/consume_integral_settlement.go index ee6eeaa..7d8d661 100644 --- a/rule/consume_integral_settlement.go +++ b/rule/consume_integral_settlement.go @@ -371,8 +371,14 @@ func DealUserConsumeIntegral(session *xorm.Session, subsidyWithUser *model.Subsi afterAmount = 0 subsidyWithUser.State = 3 //补贴完 } + balanceAmount1, _ := decimal.NewFromString(subsidyWithUser.BalanceAmount) + afterAmount1, _ := balanceAmount1.Sub(decimal.NewFromFloat(consumeIntegralValue)).Float64() + if afterAmount1 <= 0 { + afterAmount1 = 0 + } + subsidyWithUser.BalanceAmount = zhios_order_relate_utils.Float64ToStr(afterAmount1) subsidyWithUser.LeaveDay = afterAmount - updateAffected, err := db.SubsidyWithUserUpdate(session, subsidyWithUser.Id, subsidyWithUser, "leave_day", "state") + updateAffected, err := db.SubsidyWithUserUpdate(session, subsidyWithUser.Id, subsidyWithUser, "leave_day", "state", "balance_amount") if err != nil { return err } @@ -450,8 +456,15 @@ func DealUserExperienceIntegral(session *xorm.Session, subsidyWithUser *model.Su afterAmount = 0 subsidyWithUser.State = 3 //补贴完 } + balanceAmount1, _ := decimal.NewFromString(subsidyWithUser.BalanceAmount) + afterAmount1, _ := balanceAmount1.Sub(decimal.NewFromFloat(experienceIntegralValue)).Float64() + if afterAmount1 <= 0 { + afterAmount1 = 0 + } + subsidyWithUser.BalanceAmount = zhios_order_relate_utils.Float64ToStr(afterAmount1) + subsidyWithUser.LeaveDay = afterAmount - updateAffected, err := db.SubsidyWithUserUpdate(session, subsidyWithUser.Id, subsidyWithUser, "leave_day", "state") + updateAffected, err := db.SubsidyWithUserUpdate(session, subsidyWithUser.Id, subsidyWithUser, "leave_day", "state", "balance_amount") if err != nil { return err } diff --git a/rule/micro_applications/happy_orchard_user_seed_record.go b/rule/micro_applications/happy_orchard_user_seed_record.go index dd3b2de..42b8c65 100644 --- a/rule/micro_applications/happy_orchard_user_seed_record.go +++ b/rule/micro_applications/happy_orchard_user_seed_record.go @@ -50,6 +50,7 @@ func GetHappyOrchardUserSeedRecordData(engine *xorm.Engine, record models.HappyO resp.RecordPlantingNextStage = resp.RecordPlantingStage + 1 resp.RecordPlantingNextStageZh = stageNameCustomData[resp.RecordPlantingNextStage] resp.RecordPlantingNextStageNeedWaterNums = nextSeedStateWaters / happyOrchardBasicSetting.WateringEveryTimeWaterDroplet + resp.RecordPlantingNextStageNeedWater = nextSeedStateWaters } resp.RecordId = record.Id resp.RecordName = happyOrchardSeed.Name diff --git a/rule/micro_applications/happy_orchard_user_waters.go b/rule/micro_applications/happy_orchard_user_waters.go index 69625cd..bbe8b3f 100644 --- a/rule/micro_applications/happy_orchard_user_waters.go +++ b/rule/micro_applications/happy_orchard_user_waters.go @@ -146,7 +146,6 @@ func DealUserWaters(engine *xorm.Engine, kind string, args interface{}) (err err return } - func handelWatering(engine *xorm.Engine, req md.DealUserWatersKindForWateringReq, basicSetting models.HappyOrchardBasicSetting) (err error) { session := engine.NewSession() session.Begin() @@ -156,22 +155,17 @@ func handelWatering(engine *xorm.Engine, req md.DealUserWatersKindForWateringReq _ = zhios_order_relate_logx.Error(err) } }() - err = CommWatering(engine, session, req, basicSetting) - if err != nil { - _ = session.Rollback() - return - } - return session.Commit() -} -func CommWatering(engine *xorm.Engine, session *xorm.Session, req md.DealUserWatersKindForWateringReq, basicSetting models.HappyOrchardBasicSetting) (err error) { now := time.Now() + //1、查找 happy_orchard_user_waters 数据 happyOrchardUserWatersSess := implement.NewHappyOrchardUserWatersSess(session) happyOrchardUserWaters, err := happyOrchardUserWatersSess.GetHappyOrchardUserWatersSess(req.Uid) if err != nil { + _ = session.Rollback() return } if happyOrchardUserWaters == nil { + _ = session.Rollback() return errors.New("用户数据不存在") } @@ -179,14 +173,17 @@ func CommWatering(engine *xorm.Engine, session *xorm.Session, req md.DealUserWat beforeWaterNums := happyOrchardUserWaters.WaterNums happyOrchardUserWaters.WaterNums -= basicSetting.WateringEveryTimeWaterDroplet if happyOrchardUserWaters.WaterNums < 0 { + _ = session.Rollback() return errors.New("用户水滴不够") } //3、更新 happy_orchard_user_waters 数据 _, err = happyOrchardUserWatersSess.UpdateHappyOrchardUserWatersBySess(req.Uid, happyOrchardUserWaters, "water_nums") if err != nil { + _ = session.Rollback() return err } + //4、新增 happy_orchard_user_waters_log 数据 happyOrchardUserWatersLogDb := implement.NewHappyOrchardUserWatersLogDb(engine) _, err = happyOrchardUserWatersLogDb.InsertHappyOrchardUserWatersLogBySess(session, &models.HappyOrchardUserWatersLog{ @@ -200,9 +197,11 @@ func CommWatering(engine *xorm.Engine, session *xorm.Session, req md.DealUserWat UpdateAt: now.Format("2006-01-02 15:04:05"), }) if err != nil { + _ = session.Rollback() return err } - return nil + + return session.Commit() } func handelSevenDaysSign(engine *xorm.Engine, req md.DealUserWatersKindForSevenDaysSignReq, basicSetting models.HappyOrchardBasicSetting) (err error) { @@ -910,3 +909,59 @@ func generateRandomInt(min, max int) int { rand.Seed(time.Now().UnixNano()) // 初始化随机种子 return rand.Intn(max-min+1) + min // 生成[min,max]范围内的随机整数 } +func CommWatering(engine *xorm.Engine, session *xorm.Session, req md.DealUserWatersKindForWateringReq) (err error) { + now := time.Now() + //1、查找 happy_orchard_user_waters 数据 + happyOrchardUserWatersSess := implement.NewHappyOrchardUserWatersSess(session) + happyOrchardUserWaters, err := happyOrchardUserWatersSess.GetHappyOrchardUserWatersSess(req.Uid) + if err != nil { + return + } + if happyOrchardUserWaters == nil { + if req.Kind == 1 { + happyOrchardUserWaters = &models.HappyOrchardUserWaters{ + Uid: req.Uid, + CreateAt: time.Now().Format("2006-01-02 15:04:05"), + UpdateAt: time.Now().Format("2006-01-02 15:04:05"), + } + _, err = session.Insert(happyOrchardUserWaters) + if err != nil { + return + } + } else { + return errors.New("用户数据不存在") + } + } + //2、判断用户水滴是否足够 + beforeWaterNums := happyOrchardUserWaters.WaterNums + if req.Kind == 1 { + happyOrchardUserWaters.WaterNums += req.RewardWatersValue + } else { + happyOrchardUserWaters.WaterNums -= req.RewardWatersValue + if happyOrchardUserWaters.WaterNums < 0 { + return errors.New("用户水滴不够") + } + } + + //3、更新 happy_orchard_user_waters 数据 + _, err = happyOrchardUserWatersSess.UpdateHappyOrchardUserWatersBySess(req.Uid, happyOrchardUserWaters, "water_nums") + if err != nil { + return err + } + //4、新增 happy_orchard_user_waters_log 数据 + happyOrchardUserWatersLogDb := implement.NewHappyOrchardUserWatersLogDb(engine) + _, err = happyOrchardUserWatersLogDb.InsertHappyOrchardUserWatersLogBySess(session, &models.HappyOrchardUserWatersLog{ + Uid: req.Uid, + WaterNums: req.RewardWatersValue, + Title: enum.DealUserWatersKind(req.KindType).String(), + Kind: req.Kind, + BeforeWaterNums: beforeWaterNums, + AfterWaterNums: happyOrchardUserWaters.WaterNums, + CreateAt: now.Format("2006-01-02 15:04:05"), + UpdateAt: now.Format("2006-01-02 15:04:05"), + }) + if err != nil { + return err + } + return nil +} diff --git a/rule/micro_applications/md/happy_orchard.go b/rule/micro_applications/md/happy_orchard.go index 82b6e2d..c6cdcc6 100644 --- a/rule/micro_applications/md/happy_orchard.go +++ b/rule/micro_applications/md/happy_orchard.go @@ -36,13 +36,17 @@ type GetHappyOrchardUserSeedRecordDataResp struct { RecordPlantingNextStage int `json:"record_planting_next_stage"` //种子种植下一阶段 RecordPlantingNextStageZh string `json:"record_planting_next_stage_zh"` RecordPlantingNextStageNeedWaterNums int `json:"record_planting_next_stage_need_water_nums"` //种子种植下一阶段需浇水次数 + RecordPlantingNextStageNeedWater int `json:"record_planting_next_stage_need_water"` //种子种植下一阶段水滴数 NowRecordWaters int `json:"now_record_waters"` //当前种子水滴数 TotalNeedWaters int `json:"total_need_waters"` //总计需要水滴数 PlantingRate float64 `json:"planting_rate"` //种植进度百分比 } type DealUserWatersKindForWateringReq struct { - Uid int `json:"uid"` //用户id + Uid int `json:"uid"` //用户id + KindType string `json:"kind_type"` + Kind int `json:"kind"` + RewardWatersValue int `json:"reward_waters_value"` } type DealUserWatersKindForSevenDaysSignReq struct { Uid int `json:"uid"` //用户id