From f64b4b5498f3b0c5c300d41797b69170ac081009 Mon Sep 17 00:00:00 2001 From: sugarBecomer Date: Tue, 9 Dec 2025 16:19:45 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=85=8D=E7=BD=AE=E5=8D=95?= =?UTF-8?q?=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/v1/price_config_req.go | 29 ++++++++ internal/common/cst/base_point.go | 5 -- internal/common/cst/config.go | 6 ++ internal/controller/config/config_ctrlV1.go | 12 ++++ internal/controller/config/i_config_ctrl.go | 2 + internal/service/config/config_service.go | 77 +++++++++++++++++++++ internal/service/course/course.go | 11 +++ internal/service/course/smartedu_sc.go | 11 +++ internal/service/cpdaily/cpleave.go | 5 +- internal/service/cpdaily/cptask.go | 3 +- internal/service/cpdaily/cpuser.go | 8 ++- 11 files changed, 158 insertions(+), 11 deletions(-) create mode 100644 internal/api/v1/price_config_req.go delete mode 100644 internal/common/cst/base_point.go diff --git a/internal/api/v1/price_config_req.go b/internal/api/v1/price_config_req.go new file mode 100644 index 0000000..128373b --- /dev/null +++ b/internal/api/v1/price_config_req.go @@ -0,0 +1,29 @@ +package v1 + +import ( + "github.com/gogf/gf/v2/frame/g" +) + +// PriceConfigGetReq 获取所有费率配置 +type PriceConfigGetReq struct { + g.Meta `path:"/config/price" method:"get" tags:"PriceConfig" summary:"获取所有费率配置"` +} + +// PriceConfigUpdateReq 更新费率配置 +type PriceConfigUpdateReq struct { + g.Meta `path:"/config/price" method:"put" tags:"PriceConfig" summary:"更新费率配置" permission:"sys:config:edit"` + CpdailyTask string `json:"cpdaily_task"` // 今日校园签到单价 + CpdailyLeave string `json:"cpdaily_leave"` // 今日校园请假单价 + CpdailyPhone string `json:"cpdaily_phone"` // 今日校园手机号单价 + Xxt string `json:"xxt"` // 学习通单价 + SmarteduSc string `json:"smartedu_sc"` // 四川智慧教育单价 +} + +// PriceConfigResp 费率配置响应 +type PriceConfigResp struct { + CpdailyTask string `json:"cpdaily_task"` + CpdailyLeave string `json:"cpdaily_leave"` + CpdailyPhone string `json:"cpdaily_phone"` + Xxt string `json:"xxt"` + SmarteduSc string `json:"smartedu_sc"` +} diff --git a/internal/common/cst/base_point.go b/internal/common/cst/base_point.go deleted file mode 100644 index f5f59aa..0000000 --- a/internal/common/cst/base_point.go +++ /dev/null @@ -1,5 +0,0 @@ -package cst - -const ( - BasePointCpdaily = 1 -) diff --git a/internal/common/cst/config.go b/internal/common/cst/config.go index 97c1948..76b15d5 100644 --- a/internal/common/cst/config.go +++ b/internal/common/cst/config.go @@ -43,6 +43,12 @@ const ( // 系统配置 ConfigSystemAllowRegister = "system.allowRegister" // 是否允许注册 + + ConfigRedisCpdailyTask = "price.cpdaily_task" + ConfigRedisCpdailyLeave = "price.cpdaily_leave" + ConfigRedisCpdailyPhone = "price.cpdaily_phone" + ConfigRedisXxt = "price.xxt" + ConfigRedisSmarteduSc = "price.smartedu_sc" ) const ( diff --git a/internal/controller/config/config_ctrlV1.go b/internal/controller/config/config_ctrlV1.go index 91de434..5e4c755 100644 --- a/internal/controller/config/config_ctrlV1.go +++ b/internal/controller/config/config_ctrlV1.go @@ -31,6 +31,18 @@ func (c ConfigCtrlV1) Update(ctx context.Context, req *v1.ConfigUpdateReq) (res return service.Global.Config.Update(ctx, req), nil } +// GetPriceConfig 获取费率配置 +func (c ConfigCtrlV1) GetPriceConfig(ctx context.Context, req *v1.PriceConfigGetReq) (res *utils.Response[*v1.PriceConfigResp], err error) { + defer utils.Pass(&err) + return service.Global.Config.GetPriceConfig(ctx, req), nil +} + +// UpdatePriceConfig 更新费率配置 +func (c ConfigCtrlV1) UpdatePriceConfig(ctx context.Context, req *v1.PriceConfigUpdateReq) (res *utils.Response[any], err error) { + defer utils.Pass(&err) + return service.Global.Config.UpdatePriceConfig(ctx, req), nil +} + func NewConfigCtrlV1() IConfigCtrlV1 { return &ConfigCtrlV1{} } diff --git a/internal/controller/config/i_config_ctrl.go b/internal/controller/config/i_config_ctrl.go index afcd374..b8bbd96 100644 --- a/internal/controller/config/i_config_ctrl.go +++ b/internal/controller/config/i_config_ctrl.go @@ -12,4 +12,6 @@ type IConfigCtrlV1 interface { PublicGet(ctx context.Context, req *v1.ConfigPublicGetReq) (*utils.Response[*ent.SysConfig], error) List(ctx context.Context, req *v1.ConfigListReq) (*utils.Response[[]*ent.SysConfig], error) Update(ctx context.Context, req *v1.ConfigUpdateReq) (*utils.Response[any], error) + GetPriceConfig(ctx context.Context, req *v1.PriceConfigGetReq) (*utils.Response[*v1.PriceConfigResp], error) + UpdatePriceConfig(ctx context.Context, req *v1.PriceConfigUpdateReq) (*utils.Response[any], error) } diff --git a/internal/service/config/config_service.go b/internal/service/config/config_service.go index 963a879..67ce4e5 100644 --- a/internal/service/config/config_service.go +++ b/internal/service/config/config_service.go @@ -112,3 +112,80 @@ func (s *ConfigServiceV1) Update(ctx context.Context, req *v1.ConfigUpdateReq) * return utils.SuccessWithMsg("配置更新成功") } + +// 费率配置键常量 +const ( + PriceKeyCpdailyTask = "price.cpdaily_task" + PriceKeyCpdailyLeave = "price.cpdaily_leave" + PriceKeyCpdailyPhone = "price.cpdaily_phone" + PriceKeyXxt = "price.xxt" + PriceKeySmarteduSc = "price.smartedu_sc" +) + +// GetPriceConfig 获取费率配置 +func (s *ConfigServiceV1) GetPriceConfig(ctx context.Context, req *v1.PriceConfigGetReq) *utils.Response[*v1.PriceConfigResp] { + resp := &v1.PriceConfigResp{ + CpdailyTask: s.getConfigValue(ctx, PriceKeyCpdailyTask, "0.5"), + CpdailyLeave: s.getConfigValue(ctx, PriceKeyCpdailyLeave, "4"), + CpdailyPhone: s.getConfigValue(ctx, PriceKeyCpdailyPhone, "0.25"), + Xxt: s.getConfigValue(ctx, PriceKeyXxt, "0"), + SmarteduSc: s.getConfigValue(ctx, PriceKeySmarteduSc, "0"), + } + return utils.SuccessWithData[*v1.PriceConfigResp](resp) +} + +// getConfigValue 获取配置值,不存在则返回默认值 +func (s *ConfigServiceV1) getConfigValue(ctx context.Context, key, defaultValue string) string { + config, err := db.Global.SysConfig.Query(). + Where(sysconfig.KeyEQ(key)). + Only(ctx) + if err != nil { + return defaultValue + } + return config.Value +} + +// UpdatePriceConfig 更新费率配置 +func (s *ConfigServiceV1) UpdatePriceConfig(ctx context.Context, req *v1.PriceConfigUpdateReq) *utils.Response[any] { + // 更新各项费率配置 + if req.CpdailyTask != "" { + s.upsertConfig(ctx, PriceKeyCpdailyTask, req.CpdailyTask, "今日校园签到单价") + } + if req.CpdailyLeave != "" { + s.upsertConfig(ctx, PriceKeyCpdailyLeave, req.CpdailyLeave, "今日校园请假单价") + } + if req.CpdailyPhone != "" { + s.upsertConfig(ctx, PriceKeyCpdailyPhone, req.CpdailyPhone, "今日校园手机号单价") + } + if req.Xxt != "" { + s.upsertConfig(ctx, PriceKeyXxt, req.Xxt, "学习通单价") + } + if req.SmarteduSc != "" { + s.upsertConfig(ctx, PriceKeySmarteduSc, req.SmarteduSc, "四川智慧教育单价") + } + return utils.SuccessWithMsg("费率配置更新成功") +} + +// upsertConfig 更新或创建配置 +func (s *ConfigServiceV1) upsertConfig(ctx context.Context, key, value, description string) { + config, err := db.Global.SysConfig.Query(). + Where(sysconfig.KeyEQ(key)). + Only(ctx) + + if err != nil { + if ent.IsNotFound(err) { + db.Global.SysConfig.Create(). + SetKey(key). + SetValue(value). + SetDescription(description). + SetType("number"). + SaveX(ctx) + return + } + utils.OkOrPanic(err) + } + + db.Global.SysConfig.UpdateOne(config). + SetValue(value). + ExecX(ctx) +} diff --git a/internal/service/course/course.go b/internal/service/course/course.go index a961c6b..9905275 100644 --- a/internal/service/course/course.go +++ b/internal/service/course/course.go @@ -6,14 +6,18 @@ import ( v1 "likedream-goframe/internal/api/v1" "likedream-goframe/internal/application/xuexitong" "likedream-goframe/internal/application/xuexitong/entity" + "likedream-goframe/internal/common/cst" db2 "likedream-goframe/internal/common/db" errors2 "likedream-goframe/internal/common/errors" "likedream-goframe/internal/ent" "likedream-goframe/internal/ent/courseentity" + "likedream-goframe/internal/ent/sysconfig" "likedream-goframe/internal/ent/sysuser" "likedream-goframe/internal/framework/logger" + "likedream-goframe/internal/service/balance" "likedream-goframe/internal/utils" + "github.com/shopspring/decimal" "go.uber.org/zap" "resty.dev/v3" ) @@ -89,6 +93,13 @@ func (s CourseServiceV1) AddCourse(ctx context.Context, req *v1.AddCourseReq) *u if targetCourse == nil { utils.OkOrPanic(errors2.NewBusinessError(400, "未找到该课程")) } + basePoint, err := decimal.NewFromString(db2.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst.ConfigRedisXxt)).OnlyX(ctx).Value) + utils.OkOrPanic(err) + if user.Edges.Agent != nil { + basePoint = basePoint.Mul(user.Edges.Agent.Rate) + } + // 先扣款 + balance.BalanceService.ChangeUserBalance(ctx, user.ID, basePoint.Neg(), "学习通刷课") // 添加课程时不获取任务点统计,由定时任务处理 db2.Global.CourseEntity.Create(). diff --git a/internal/service/course/smartedu_sc.go b/internal/service/course/smartedu_sc.go index c6f709d..be79830 100644 --- a/internal/service/course/smartedu_sc.go +++ b/internal/service/course/smartedu_sc.go @@ -5,14 +5,18 @@ import ( v1 "likedream-goframe/internal/api/v1" "likedream-goframe/internal/application/smartedu/model" "likedream-goframe/internal/application/smartedu/sc" + "likedream-goframe/internal/common/cst" db2 "likedream-goframe/internal/common/db" errors2 "likedream-goframe/internal/common/errors" "likedream-goframe/internal/ent" "likedream-goframe/internal/ent/courseentity" + "likedream-goframe/internal/ent/sysconfig" "likedream-goframe/internal/ent/sysuser" "likedream-goframe/internal/framework/logger" + "likedream-goframe/internal/service/balance" "likedream-goframe/internal/utils" + "github.com/shopspring/decimal" "go.uber.org/zap" ) @@ -99,6 +103,13 @@ func (s SmartEduSCServiceV1) AddCourse(ctx context.Context, req *v1.SmartEduSCAd if targetCourse == nil { utils.OkOrPanic(errors2.NewBusinessError(400, "未找到该课程")) } + basePoint, err := decimal.NewFromString(db2.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst.ConfigRedisSmarteduSc)).OnlyX(ctx).Value) + utils.OkOrPanic(err) + if user.Edges.Agent != nil { + basePoint = basePoint.Mul(user.Edges.Agent.Rate) + } + // 先扣款 + balance.BalanceService.ChangeUserBalance(ctx, user.ID, basePoint.Neg(), "四川中小学教师刷课") // 创建数据库记录 entity := db2.Global.CourseEntity.Create(). diff --git a/internal/service/cpdaily/cpleave.go b/internal/service/cpdaily/cpleave.go index 4c37c70..9fff3bc 100644 --- a/internal/service/cpdaily/cpleave.go +++ b/internal/service/cpdaily/cpleave.go @@ -13,11 +13,11 @@ import ( "likedream-goframe/internal/ent" "likedream-goframe/internal/ent/cpleave" "likedream-goframe/internal/ent/cpuser" + "likedream-goframe/internal/ent/sysconfig" "likedream-goframe/internal/ent/sysuser" "likedream-goframe/internal/service/balance" "likedream-goframe/internal/utils" - "github.com/gogf/gf/v2/frame/g" "github.com/shopspring/decimal" ) @@ -111,7 +111,8 @@ func (v CpLeaveServiceV1) Report(ctx context.Context, req *v1.CpLeaveReportReq) } // 计算扣款金额 - basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst2.ConfigBasePointCpdailyLeave).String()) + //basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst2.ConfigBasePointCpdailyLeave).String()) + basePoint, err := decimal.NewFromString(db2.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst2.ConfigRedisCpdailyLeave)).OnlyX(ctx).Value) utils.OkOrPanic(err) if user.Edges.Agent != nil { basePoint = basePoint.Mul(user.Edges.Agent.Rate) diff --git a/internal/service/cpdaily/cptask.go b/internal/service/cpdaily/cptask.go index d5a2f50..b22bb69 100644 --- a/internal/service/cpdaily/cptask.go +++ b/internal/service/cpdaily/cptask.go @@ -16,6 +16,7 @@ import ( "likedream-goframe/internal/ent" "likedream-goframe/internal/ent/cptask" "likedream-goframe/internal/ent/cpuser" + "likedream-goframe/internal/ent/sysconfig" "likedream-goframe/internal/ent/sysuser" "likedream-goframe/internal/framework/queue" "likedream-goframe/internal/framework/schedule" @@ -180,7 +181,7 @@ func (v CpTaskServiceV1) PostExecute(ctx context.Context, req *v1.PostCpTaskExec } //扣积分 - basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst2.ConfigBasePointCpdailyTask).String()) + basePoint, err := decimal.NewFromString(db2.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst2.ConfigRedisCpdailyTask)).OnlyX(ctx).Value) utils.OkOrPanic(err) if user.Edges.Agent != nil { basePoint = basePoint.Mul(user.Edges.Agent.Rate) diff --git a/internal/service/cpdaily/cpuser.go b/internal/service/cpdaily/cpuser.go index 02823f9..99801e1 100644 --- a/internal/service/cpdaily/cpuser.go +++ b/internal/service/cpdaily/cpuser.go @@ -11,6 +11,7 @@ import ( "likedream-goframe/internal/ent" "likedream-goframe/internal/ent/cpschool" "likedream-goframe/internal/ent/cpuser" + "likedream-goframe/internal/ent/sysconfig" "likedream-goframe/internal/ent/sysuser" "likedream-goframe/internal/framework/redis" "likedream-goframe/internal/service/balance" @@ -19,7 +20,6 @@ import ( "time" "github.com/bytedance/sonic" - "github.com/gogf/gf/v2/frame/g" "github.com/samber/oops" "github.com/shopspring/decimal" ) @@ -182,7 +182,8 @@ func (v CpUserServiceV1) AuthorizeRandom(ctx context.Context, req *v1.CpUserAuth func (v CpUserServiceV1) LoginGetCode(ctx context.Context, req *v1.LoginGetCode) *utils.Response[any] { user := utils.GetUserInfoFromCtx(ctx) // 扣费 - basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst.ConfigBasePointCpdailyPhone).String()) + //basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst.ConfigBasePointCpdailyPhone).String()) + basePoint, err := decimal.NewFromString(db.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst.ConfigRedisCpdailyPhone)).OnlyX(ctx).Value) utils.OkOrPanic(err) if user.Edges.Agent != nil { basePoint = basePoint.Mul(user.Edges.Agent.Rate) @@ -203,7 +204,8 @@ func (v CpUserServiceV1) AddByCode(ctx context.Context, req *v1.AddByCode) *util data := loginExecutor.LoginByCode(context.Background(), req.Mobile, req.Code, jar) loginExecutor.SetLoginCookie(context.Background(), jar, data) // 扣费 - basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst.ConfigBasePointCpdailyPhone).String()) + //basePoint, err := decimal.NewFromString(g.Cfg().MustGet(ctx, cst.ConfigBasePointCpdailyPhone).String()) + basePoint, err := decimal.NewFromString(db.Global.SysConfig.Query().Where(sysconfig.KeyEQ(cst.ConfigRedisCpdailyPhone)).OnlyX(ctx).Value) utils.OkOrPanic(err) if user.Edges.Agent != nil { basePoint = basePoint.Mul(user.Edges.Agent.Rate)