diff --git a/app/svc/sys_cfg/sys_cfg_dao.go b/app/svc/sys_cfg/sys_cfg_dao.go new file mode 100644 index 0000000..4701665 --- /dev/null +++ b/app/svc/sys_cfg/sys_cfg_dao.go @@ -0,0 +1,14 @@ +package sys_cfg + +import "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + +type SysCfgDao interface { + SysCfgGetAll() (*[]model.SysCfg, error) + SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) + SysCfgGetOne(key string) (*model.SysCfg, error) + SysCfgInsert(key, val, memo string) bool + SysCfgUpdate(key, val string) bool + SysCfgGetWithDb(HKey string) string + SysCfgDel(HKey string) error + SysCfgFindWithDb(keys ...string) map[string]string +} diff --git a/app/svc/sys_cfg/sys_cfg_implement.go b/app/svc/sys_cfg/sys_cfg_implement.go new file mode 100644 index 0000000..6c27d1e --- /dev/null +++ b/app/svc/sys_cfg/sys_cfg_implement.go @@ -0,0 +1,118 @@ +package sys_cfg + +import ( + "applet/app/utils/cache" + "code.fnuoos.com/EggPlanet/egg_models.git/md" + "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx" + "fmt" + "xorm.io/xorm" +) + +func NewSysCfgDb(engine *xorm.Engine) SysCfgDao { + return &SysCfgDb{ + Db: engine, + } +} + +type SysCfgDb struct { + Db *xorm.Engine +} + +func (s SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) { + var cfgList []model.SysCfg + if err := s.Db.Cols("key,val,memo").Find(&cfgList); err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) { + var cfgList model.SysCfg + _, err := s.Db.Where("`key`=?", key).Get(&cfgList) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) { + var cfgList model.SysCfg + if has, err := s.Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgInsert(key, val, memo string) bool { + cfg := model.SysCfg{Key: key, Val: val, Memo: memo} + _, err := s.Db.InsertOne(&cfg) + if err != nil { + zhios_order_relate_logx.Error(err) + return false + } + return true +} + +func (s SysCfgDb) SysCfgUpdate(key, val string) bool { + cfg := model.SysCfg{Key: key, Val: val} + _, err := s.Db.Where("`key`=?", key).Cols("val").Update(&cfg) + if err != nil { + zhios_order_relate_logx.Error(err) + return false + } + s.SysCfgDel(key) + return true +} + +func (s SysCfgDb) SysCfgGetWithDb(HKey string) string { + cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1]) + get, err := cache.HGetString(cacheKey, HKey) + if err != nil || get == "" { + cfg, err := s.SysCfgGetOne(HKey) + if err != nil || cfg == nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + + // key是否存在 + cacheKeyExist := false + if cache.Exists(cacheKey) { + cacheKeyExist = true + } + + // 设置缓存 + _, err = cache.HSet(cacheKey, HKey, cfg.Val) + if err != nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + if !cacheKeyExist { // 如果是首次设置 设置过期时间 + _, err := cache.Expire(cacheKey, md.CfgCacheTime) + if err != nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + } + return cfg.Val + } + return get +} + +func (s SysCfgDb) SysCfgDel(HKey string) error { + cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1]) + _, err := cache.HDel(cacheKey, HKey) + if err != nil { + return err + } + return nil +} + +func (s SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string { + res := map[string]string{} + for _, v := range keys { + val := s.SysCfgGetWithDb(v) + res[v] = val + } + return res +} diff --git a/consume/aliyun_sms_record_consume.go b/consume/aliyun_sms_record_consume.go index d401c9e..19ecf00 100644 --- a/consume/aliyun_sms_record_consume.go +++ b/consume/aliyun_sms_record_consume.go @@ -3,14 +3,12 @@ package consume import ( "applet/app/cfg" "applet/app/db" - "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" + "applet/app/svc/sys_cfg" "code.fnuoos.com/EggPlanet/egg_system_rules.git" utils2 "applet/app/utils" - "applet/app/utils/cache" "applet/app/utils/logx" "applet/consume/md" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" @@ -76,14 +74,7 @@ func handleAliyunSmsRecordConsume(msgData []byte) error { return err } engine := db.Db - redisConn := cache.GetPool().Get() - sysCfgDb := implement.NewSysCfgDb(engine, redisConn) - defer func(sysCfgDb dao.SysCfgDao) { - err := sysCfgDb.Close() - if err != nil { - logx.Error("redis close err:" + err.Error()) // 记录错误信息 - } - }(sysCfgDb) + sysCfgDb := sys_cfg.NewSysCfgDb(engine) aliyunSmsId := sysCfgDb.SysCfgGetWithDb("aliyun_sms_id") aliyunSmsSecret := sysCfgDb.SysCfgGetWithDb("aliyun_sms_secret") aliyunSmsSignName := sysCfgDb.SysCfgGetWithDb("aliyun_sms_sign_name") diff --git a/consume/egg_fin_withdraw_apply_consume.go b/consume/egg_fin_withdraw_apply_consume.go index 9bd645b..eac7eb9 100644 --- a/consume/egg_fin_withdraw_apply_consume.go +++ b/consume/egg_fin_withdraw_apply_consume.go @@ -4,15 +4,14 @@ import ( "applet/app/cfg" "applet/app/db" utils2 "applet/app/utils" - "applet/app/utils/cache" "applet/app/utils/logx" "applet/consume/md" - "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "context" "encoding/json" @@ -158,13 +157,7 @@ type InitAlipayStruct struct { // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) { if initData == nil { - sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get()) - defer func(sysCfgDb dao.SysCfgDao) { - err := sysCfgDb.Close() - if err != nil { - logx.Error("redis close err:" + err.Error()) // 记录错误信息 - } - }(sysCfgDb) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent) initData = &InitAlipayStruct{ IsProd: true, diff --git a/consume/jpush_record_consume.go b/consume/jpush_record_consume.go index 8df3295..b1ed0b8 100644 --- a/consume/jpush_record_consume.go +++ b/consume/jpush_record_consume.go @@ -3,13 +3,10 @@ package consume import ( "applet/app/cfg" "applet/app/db" - "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" - + "applet/app/svc/sys_cfg" utils2 "applet/app/utils" - "applet/app/utils/cache" "applet/app/utils/logx" "applet/consume/md" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git" "code.fnuoos.com/EggPlanet/egg_system_rules.git/jPush" @@ -77,14 +74,7 @@ func handleJpushRecordConsume(msgData []byte) error { return err } engine := db.Db - redisConn := cache.GetPool().Get() - sysCfgDb := implement.NewSysCfgDb(engine, redisConn) - defer func(sysCfgDb dao.SysCfgDao) { - err := sysCfgDb.Close() - if err != nil { - logx.Error("redis close err:" + err.Error()) // 记录错误信息 - } - }(sysCfgDb) + sysCfgDb := sys_cfg.NewSysCfgDb(engine) jpushKey := sysCfgDb.SysCfgGetWithDb("jpush_key") jpushSecret := sysCfgDb.SysCfgGetWithDb("jpush_secret") if msg.Target == "0" { //广播全部 diff --git a/consume/playlet_reward_consume.go b/consume/playlet_reward_consume.go index f79ab63..7bd60fc 100644 --- a/consume/playlet_reward_consume.go +++ b/consume/playlet_reward_consume.go @@ -4,8 +4,8 @@ import ( "applet/app/cfg" "applet/app/db" "applet/app/e" + "applet/app/svc/sys_cfg" utils2 "applet/app/utils" - "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" "code.fnuoos.com/EggPlanet/egg_system_rules.git" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" @@ -88,14 +88,7 @@ func handlePlayletRewardConsume(ch *rabbit.Channel, msgData []byte) error { defer cb() // 释放锁 } eg := db.Db - redisConn := cache.GetPool().Get() - sysCfgDb := implement.NewSysCfgDb(eg, redisConn) - defer func(sysCfgDb dao.SysCfgDao) { - err := sysCfgDb.Close() - if err != nil { - logx.Error("redis close err:" + err.Error()) // 记录错误信息 - } - }(sysCfgDb) + sysCfgDb := sys_cfg.NewSysCfgDb(eg) playletBase := sysCfgDb.SysCfgGetWithDb("playlet_base") sess := eg.NewSession() defer sess.Close() diff --git a/consume/video_reward_consume.go b/consume/video_reward_consume.go index a5125ef..fa82e00 100644 --- a/consume/video_reward_consume.go +++ b/consume/video_reward_consume.go @@ -8,13 +8,13 @@ import ( "applet/app/utils/cache" "applet/app/utils/logx" "applet/consume/md" - "code.fnuoos.com/EggPlanet/egg_models.git/src/dao" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "encoding/json" "errors" @@ -87,14 +87,7 @@ func handleVideoRewardConsume(ch *rabbit.Channel, msgData []byte) error { defer cb() // 释放锁 } eg := db.Db - redisConn := cache.GetPool().Get() - sysCfgDb := implement.NewSysCfgDb(eg, redisConn) - defer func(sysCfgDb dao.SysCfgDao) { - err := sysCfgDb.Close() - if err != nil { - logx.Error("redis close err:" + err.Error()) // 记录错误信息 - } - }(sysCfgDb) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) videoBase := sysCfgDb.SysCfgGetWithDb("video_base") sess := eg.NewSession() defer sess.Close() diff --git a/go.mod b/go.mod index e741020..b1d052f 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ go 1.19 //replace code.fnuoos.com/EggPlanet/egg_system_rules.git => E:/company/Egg/egg_system_rules require ( - code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241211055111-7c47b67b46d2 - code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241212093609-6ece027b8e1d + code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241212120727-3681308aeb14 + code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241212122404-22c05dd7f73c code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/boombuler/barcode v1.0.1