1
This commit is contained in:
45
internal/app/common/logic/cache/cache.go
vendored
Normal file
45
internal/app/common/logic/cache/cache.go
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @desc:缓存处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/27 16:33
|
||||
*/
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/tiger1103/gfast-cache/cache"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterCache(New())
|
||||
}
|
||||
|
||||
func New() *sCache {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
cacheContainer *cache.GfCache
|
||||
)
|
||||
prefix := g.Cfg().MustGet(ctx, "system.cache.prefix").String()
|
||||
model := g.Cfg().MustGet(ctx, "system.cache.model").String()
|
||||
if model == consts.CacheModelRedis {
|
||||
// redis
|
||||
cacheContainer = cache.NewRedis(prefix)
|
||||
} else {
|
||||
// memory
|
||||
cacheContainer = cache.New(prefix)
|
||||
}
|
||||
return &sCache{
|
||||
GfCache: cacheContainer,
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
type sCache struct {
|
||||
*cache.GfCache
|
||||
prefix string
|
||||
}
|
||||
69
internal/app/common/logic/captcha/captcha.go
Normal file
69
internal/app/common/logic/captcha/captcha.go
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* @desc:验证码处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:01
|
||||
*/
|
||||
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterCaptcha(New())
|
||||
}
|
||||
|
||||
func New() *sCaptcha {
|
||||
return &sCaptcha{
|
||||
driver: &base64Captcha.DriverString{
|
||||
Height: 80,
|
||||
Width: 240,
|
||||
NoiseCount: 50,
|
||||
ShowLineOptions: 20,
|
||||
Length: 4,
|
||||
Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
||||
Fonts: []string{"chromohv.ttf"},
|
||||
},
|
||||
store: base64Captcha.DefaultMemStore,
|
||||
}
|
||||
}
|
||||
|
||||
type sCaptcha struct {
|
||||
driver *base64Captcha.DriverString
|
||||
store base64Captcha.Store
|
||||
}
|
||||
|
||||
var (
|
||||
captcha = sCaptcha{
|
||||
driver: &base64Captcha.DriverString{
|
||||
Height: 80,
|
||||
Width: 240,
|
||||
NoiseCount: 50,
|
||||
ShowLineOptions: 20,
|
||||
Length: 4,
|
||||
Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
||||
Fonts: []string{"chromohv.ttf"},
|
||||
},
|
||||
store: base64Captcha.DefaultMemStore,
|
||||
}
|
||||
)
|
||||
|
||||
// GetVerifyImgString 获取字母数字混合验证码
|
||||
func (s *sCaptcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error) {
|
||||
driver := s.driver.ConvertFonts()
|
||||
c := base64Captcha.NewCaptcha(driver, s.store)
|
||||
idKeyC, base64stringC, _, err = c.Generate()
|
||||
return
|
||||
}
|
||||
|
||||
// VerifyString 验证输入的验证码是否正确
|
||||
func (s *sCaptcha) VerifyString(id, answer string) bool {
|
||||
c := base64Captcha.NewCaptcha(s.driver, s.store)
|
||||
answer = gstr.ToLower(answer)
|
||||
return c.Verify(id, answer, true)
|
||||
}
|
||||
17
internal/app/common/logic/eventBus/event_bus.go
Normal file
17
internal/app/common/logic/eventBus/event_bus.go
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @Company: 云南奇讯科技有限公司
|
||||
* @Author: yxf
|
||||
* @Description:
|
||||
* @Date: 2024/1/25 16:22
|
||||
*/
|
||||
|
||||
package eventBus
|
||||
|
||||
import (
|
||||
"github.com/asaskevich/EventBus"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterEventBus(EventBus.New())
|
||||
}
|
||||
15
internal/app/common/logic/logic.go
Normal file
15
internal/app/common/logic/logic.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// ==========================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/cache"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/captcha"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/eventBus"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/middleware"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysConfig"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictData"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictType"
|
||||
)
|
||||
31
internal/app/common/logic/middleware/middleware.go
Normal file
31
internal/app/common/logic/middleware/middleware.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* @desc:中间件处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:08
|
||||
*/
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterMiddleware(New())
|
||||
}
|
||||
|
||||
func New() *sMiddleware {
|
||||
return &sMiddleware{}
|
||||
}
|
||||
|
||||
type sMiddleware struct{}
|
||||
|
||||
func (s *sMiddleware) MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
// you can set options
|
||||
//corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
178
internal/app/common/logic/sysConfig/sys_config.go
Normal file
178
internal/app/common/logic/sysConfig/sys_config.go
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* @desc:配置参数管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:13
|
||||
*/
|
||||
|
||||
package sysConfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysConfig(New())
|
||||
}
|
||||
|
||||
func New() *sSysConfig {
|
||||
return &sSysConfig{}
|
||||
}
|
||||
|
||||
type sSysConfig struct {
|
||||
}
|
||||
|
||||
// List 系统参数列表
|
||||
func (s *sSysConfig) List(ctx context.Context, req *system.ConfigSearchReq) (res *system.ConfigSearchRes, err error) {
|
||||
res = new(system.ConfigSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysConfig.Ctx(ctx)
|
||||
if req != nil {
|
||||
if req.ConfigName != "" {
|
||||
m = m.Where("config_name like ?", "%"+req.ConfigName+"%")
|
||||
}
|
||||
if req.ConfigType != "" {
|
||||
m = m.Where("config_type = ", gconv.Int(req.ConfigType))
|
||||
}
|
||||
if req.ConfigKey != "" {
|
||||
m = m.Where("config_key like ?", "%"+req.ConfigKey+"%")
|
||||
}
|
||||
if len(req.DateRange) > 0 {
|
||||
m = m.Where("created_at >= ? AND created_at<=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Page(req.PageNum, req.PageSize).Order("config_id asc").Scan(&res.List)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) Add(ctx context.Context, req *system.ConfigAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.CheckConfigKeyUnique(ctx, req.ConfigKey)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysConfig.Ctx(ctx).Insert(do.SysConfig{
|
||||
ConfigName: req.ConfigName,
|
||||
ConfigKey: req.ConfigKey,
|
||||
ConfigValue: req.ConfigValue,
|
||||
ConfigType: req.ConfigType,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加系统参数失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// CheckConfigKeyUnique 验证参数键名是否存在
|
||||
func (s *sSysConfig) CheckConfigKeyUnique(ctx context.Context, configKey string, configId ...int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
data := (*entity.SysConfig)(nil)
|
||||
m := dao.SysConfig.Ctx(ctx).Fields(dao.SysConfig.Columns().ConfigId).Where(dao.SysConfig.Columns().ConfigKey, configKey)
|
||||
if len(configId) > 0 {
|
||||
m = m.Where(dao.SysConfig.Columns().ConfigId+" != ?", configId[0])
|
||||
}
|
||||
err = m.Scan(&data)
|
||||
liberr.ErrIsNil(ctx, err, "校验失败")
|
||||
if data != nil {
|
||||
liberr.ErrIsNil(ctx, errors.New("参数键名重复"))
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get 获取系统参数
|
||||
func (s *sSysConfig) Get(ctx context.Context, id int) (res *system.ConfigGetRes, err error) {
|
||||
res = new(system.ConfigGetRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysConfig.Ctx(ctx).WherePri(id).Scan(&res.Data)
|
||||
liberr.ErrIsNil(ctx, err, "获取系统参数失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改系统参数
|
||||
func (s *sSysConfig) Edit(ctx context.Context, req *system.ConfigEditReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.CheckConfigKeyUnique(ctx, req.ConfigKey, req.ConfigId)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysConfig.Ctx(ctx).WherePri(req.ConfigId).Update(do.SysConfig{
|
||||
ConfigName: req.ConfigName,
|
||||
ConfigKey: req.ConfigKey,
|
||||
ConfigValue: req.ConfigValue,
|
||||
ConfigType: req.ConfigType,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改系统参数失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除系统参数
|
||||
func (s *sSysConfig) Delete(ctx context.Context, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysConfig.Ctx(ctx).Delete(dao.SysConfig.Columns().ConfigId+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetConfigByKey 通过key获取参数(从缓存获取)
|
||||
func (s *sSysConfig) GetConfigByKey(ctx context.Context, key string) (config *entity.SysConfig, err error) {
|
||||
if key == "" {
|
||||
err = gerror.New("参数key不能为空")
|
||||
return
|
||||
}
|
||||
cache := service.Cache()
|
||||
cf := cache.Get(ctx, consts.CacheSysConfigTag+key)
|
||||
if cf != nil && !cf.IsEmpty() {
|
||||
err = gconv.Struct(cf, &config)
|
||||
return
|
||||
}
|
||||
config, err = s.GetByKey(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if config != nil {
|
||||
cache.Set(ctx, consts.CacheSysConfigTag+key, config, 0, consts.CacheSysConfigTag)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByKey 通过key获取参数(从数据库获取)
|
||||
func (s *sSysConfig) GetByKey(ctx context.Context, key string) (config *entity.SysConfig, err error) {
|
||||
err = dao.SysConfig.Ctx(ctx).Where("config_key", key).Scan(&config)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
err = gerror.New("获取配置失败")
|
||||
}
|
||||
return
|
||||
}
|
||||
279
internal/app/common/logic/sysDictData/sys_dict_data.go
Normal file
279
internal/app/common/logic/sysDictData/sys_dict_data.go
Normal file
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
* @desc:字典数据管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:22
|
||||
*/
|
||||
|
||||
package sysDictData
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysDictData(New())
|
||||
}
|
||||
|
||||
func New() *sSysDictData {
|
||||
return &sSysDictData{}
|
||||
}
|
||||
|
||||
type sSysDictData struct {
|
||||
}
|
||||
|
||||
// GetDictDataTree 根据备注查询返回树形结构
|
||||
func (s *sSysDictData) GetDictDataTree(ctx context.Context, remark string) (res *system.GetDictTreeRes, err error) {
|
||||
cache := service.Cache()
|
||||
cacheKey := consts.CacheSysDict + "_" + remark
|
||||
//从缓存获取
|
||||
iDict := cache.GetOrSetFuncLock(ctx, cacheKey, func(ctx context.Context) (value interface{}, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//从数据库获取
|
||||
var list []*entity.SysDictType
|
||||
//获取类型数据
|
||||
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().Remark, remark).
|
||||
Where(dao.SysDictType.Columns().Status, 1).Fields(model.DictTypeRes{}).Scan(&list)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
if len(list) == 0 {
|
||||
return
|
||||
}
|
||||
dict := &system.GetDictRes{}
|
||||
err = dao.SysDictData.Ctx(ctx).Fields(model.DictDataRes{}).
|
||||
Where(dao.SysDictData.Columns().Remark, remark).
|
||||
Where(dao.SysDictData.Columns().Status, 1).
|
||||
Order(dao.SysDictData.Columns().DictSort + " asc," +
|
||||
dao.SysDictData.Columns().DictCode + " asc").
|
||||
Scan(&dict.Values)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
// 构建树形结构
|
||||
|
||||
res = new(system.GetDictTreeRes)
|
||||
for _, category := range list {
|
||||
tree := new(system.GetDictRes)
|
||||
dictType := &model.DictTypeRes{
|
||||
DictName: category.DictName,
|
||||
DictType: category.DictType,
|
||||
Remark: category.Remark,
|
||||
}
|
||||
for _, v := range dict.Values {
|
||||
if category.DictType == v.DictType {
|
||||
dictData := &model.DictDataRes{
|
||||
DictValue: v.DictValue,
|
||||
DictLabel: v.DictLabel,
|
||||
DictType: v.DictType,
|
||||
Remark: v.Remark,
|
||||
IsDefault: v.IsDefault,
|
||||
}
|
||||
tree.Values = append(tree.Values, dictData)
|
||||
}
|
||||
}
|
||||
tree.Info = dictType
|
||||
res.List = append(res.List, tree)
|
||||
}
|
||||
})
|
||||
return
|
||||
}, 0, consts.CacheSysDictTag)
|
||||
if !iDict.IsEmpty() {
|
||||
err = gconv.Struct(iDict, &res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDictWithDataByType 通过字典键类型获取选项
|
||||
func (s *sSysDictData) GetDictWithDataByType(ctx context.Context, req *system.GetDictReq) (dict *system.GetDictRes,
|
||||
err error) {
|
||||
dictType := req.DictType
|
||||
defaultValue := req.DefaultValue
|
||||
cache := service.Cache()
|
||||
cacheKey := consts.CacheSysDict + "_" + dictType
|
||||
//从缓存获取
|
||||
iDict := cache.GetOrSetFuncLock(ctx, cacheKey, func(ctx context.Context) (value interface{}, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//从数据库获取
|
||||
dict = &system.GetDictRes{}
|
||||
//获取类型数据
|
||||
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictType, dictType).
|
||||
Where(dao.SysDictType.Columns().Status, 1).Fields(model.DictTypeRes{}).Scan(&dict.Info)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
if dict.Info == nil {
|
||||
return
|
||||
}
|
||||
err = dao.SysDictData.Ctx(ctx).Fields(model.DictDataRes{}).
|
||||
Where(dao.SysDictData.Columns().DictType, dictType).
|
||||
Where(dao.SysDictData.Columns().Status, 1).
|
||||
Order(dao.SysDictData.Columns().DictSort + " asc," +
|
||||
dao.SysDictData.Columns().DictCode + " asc").
|
||||
Scan(&dict.Values)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
value = dict
|
||||
return
|
||||
}, 0, consts.CacheSysDictTag)
|
||||
if !iDict.IsEmpty() {
|
||||
err = gconv.Struct(iDict, &dict)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
//设置给定的默认值
|
||||
for _, v := range dict.Values {
|
||||
if defaultValue != "" {
|
||||
if gstr.Equal(defaultValue, v.DictValue) {
|
||||
v.IsDefault = 1
|
||||
} else {
|
||||
v.IsDefault = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取字典数据
|
||||
func (s *sSysDictData) List(ctx context.Context, req *system.DictDataSearchReq) (res *system.DictDataSearchRes, err error) {
|
||||
res = new(system.DictDataSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictData.Ctx(ctx)
|
||||
if req != nil {
|
||||
if req.DictLabel != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().DictLabel+" like ?", "%"+req.DictLabel+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().Status+" = ", gconv.Int(req.Status))
|
||||
}
|
||||
if req.DictType != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().DictType+" = ?", req.DictType)
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(dao.SysDictData.Columns().DictSort + " asc," +
|
||||
dao.SysDictData.Columns().DictCode + " asc").Scan(&res.List)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// IsExists 判断同一类型下字典名次和字典键值是否存在
|
||||
func (s *sSysDictData) IsExists(ctx context.Context, dictType, dictLabel, dictValue string, dictCode ...int64) error {
|
||||
return g.Try(ctx, func(ctx context.Context) {
|
||||
var dictData []*entity.SysDictData
|
||||
err := dao.SysDictData.Ctx(ctx).Where(dao.SysDictData.Columns().DictType, dictType).
|
||||
Fields(dao.SysDictData.Columns().DictCode, dao.SysDictData.Columns().DictLabel, dao.SysDictData.Columns().DictValue).
|
||||
Scan(&dictData)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
if dictData == nil {
|
||||
return
|
||||
}
|
||||
if len(dictCode) > 0 {
|
||||
for _, v := range dictData {
|
||||
if v.DictLabel == gstr.Trim(dictLabel) && v.DictCode != dictCode[0] {
|
||||
liberr.ErrIsNil(ctx, errors.New("字典名称已存在"))
|
||||
}
|
||||
if v.DictValue == dictValue && v.DictCode != dictCode[0] {
|
||||
liberr.ErrIsNil(ctx, errors.New("字典键值已存在"))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, v := range dictData {
|
||||
if v.DictLabel == gstr.Trim(dictLabel) {
|
||||
liberr.ErrIsNil(ctx, errors.New("字典名称已存在"))
|
||||
}
|
||||
if v.DictValue == dictValue {
|
||||
liberr.ErrIsNil(ctx, errors.New("字典键值已存在"))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s *sSysDictData) Add(ctx context.Context, req *system.DictDataAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.IsExists(ctx, req.DictType, req.DictLabel, req.DictValue)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysDictData.Ctx(ctx).Insert(do.SysDictData{
|
||||
DictSort: req.DictSort,
|
||||
DictLabel: gstr.Trim(req.DictLabel),
|
||||
DictValue: gstr.Trim(req.DictValue),
|
||||
DictType: req.DictType,
|
||||
CssClass: req.CssClass,
|
||||
ListClass: req.ListClass,
|
||||
IsDefault: req.IsDefault,
|
||||
Status: req.Status,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get 获取字典数据
|
||||
func (s *sSysDictData) Get(ctx context.Context, dictCode uint) (res *system.DictDataGetRes, err error) {
|
||||
res = new(system.DictDataGetRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictData.Ctx(ctx).WherePri(dictCode).Scan(&res.Dict)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改字典数据
|
||||
func (s *sSysDictData) Edit(ctx context.Context, req *system.DictDataEditReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.IsExists(ctx, req.DictType, req.DictLabel, req.DictValue, req.DictCode)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysDictData.Ctx(ctx).WherePri(req.DictCode).Update(do.SysDictData{
|
||||
DictSort: req.DictSort,
|
||||
DictLabel: gstr.Trim(req.DictLabel),
|
||||
DictValue: gstr.Trim(req.DictValue),
|
||||
DictType: req.DictType,
|
||||
CssClass: req.CssClass,
|
||||
ListClass: req.ListClass,
|
||||
IsDefault: req.IsDefault,
|
||||
Status: req.Status,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除字典数据
|
||||
func (s *sSysDictData) Delete(ctx context.Context, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysDictData.Ctx(ctx).Where(dao.SysDictData.Columns().DictCode+" in(?)", ids).Delete()
|
||||
liberr.ErrIsNil(ctx, err, "删除字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
186
internal/app/common/logic/sysDictType/sys_dict_type.go
Normal file
186
internal/app/common/logic/sysDictType/sys_dict_type.go
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* @desc:字典类型管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:26
|
||||
*/
|
||||
|
||||
package sysDictType
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysDictType(New())
|
||||
}
|
||||
|
||||
func New() *sSysDictType {
|
||||
return &sSysDictType{}
|
||||
}
|
||||
|
||||
type sSysDictType struct {
|
||||
}
|
||||
|
||||
// List 字典类型列表
|
||||
func (s *sSysDictType) List(ctx context.Context, req *system.DictTypeSearchReq) (res *system.DictTypeSearchRes, err error) {
|
||||
res = new(system.DictTypeSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictType.Ctx(ctx)
|
||||
if req.DictName != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().DictName+" like ?", "%"+req.DictName+"%")
|
||||
}
|
||||
if req.DictType != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().DictType+" like ?", "%"+req.DictType+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().Status+" = ", gconv.Int(req.Status))
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Fields(model.SysDictTypeInfoRes{}).Page(req.PageNum, req.PageSize).
|
||||
Order(dao.SysDictType.Columns().DictId + " asc").Scan(&res.DictTypeList)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Add 添加字典类型
|
||||
func (s *sSysDictType) Add(ctx context.Context, req *system.DictTypeAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.ExistsDictType(ctx, req.DictType)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysDictType.Ctx(ctx).Insert(do.SysDictType{
|
||||
DictName: req.DictName,
|
||||
DictType: req.DictType,
|
||||
Status: req.Status,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加字典类型失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改字典类型
|
||||
func (s *sSysDictType) Edit(ctx context.Context, req *system.DictTypeEditReq, userId uint64) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.ExistsDictType(ctx, req.DictType, req.DictId)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
dictType := (*entity.SysDictType)(nil)
|
||||
e := dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictType).WherePri(req.DictId).Scan(&dictType)
|
||||
liberr.ErrIsNil(ctx, e, "获取字典类型失败")
|
||||
liberr.ValueIsNil(dictType, "字典类型不存在")
|
||||
//修改字典类型
|
||||
_, e = dao.SysDictType.Ctx(ctx).TX(tx).WherePri(req.DictId).Update(do.SysDictType{
|
||||
DictName: req.DictName,
|
||||
DictType: req.DictType,
|
||||
Status: req.Status,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, e, "修改字典类型失败")
|
||||
//修改字典数据
|
||||
_, e = dao.SysDictData.Ctx(ctx).TX(tx).Data(do.SysDictData{DictType: req.DictType}).
|
||||
Where(dao.SysDictData.Columns().DictType, dictType.DictType).Update()
|
||||
liberr.ErrIsNil(ctx, e, "修改字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysDictType) Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictId, req.DictId).Scan(&dictType)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ExistsDictType 检查类型是否已经存在
|
||||
func (s *sSysDictType) ExistsDictType(ctx context.Context, dictType string, dictId ...int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictId).
|
||||
Where(dao.SysDictType.Columns().DictType, dictType)
|
||||
if len(dictId) > 0 {
|
||||
m = m.Where(dao.SysDictType.Columns().DictId+" !=? ", dictId[0])
|
||||
}
|
||||
res, e := m.One()
|
||||
liberr.ErrIsNil(ctx, e, "sql err")
|
||||
if !res.IsEmpty() {
|
||||
liberr.ErrIsNil(ctx, gerror.New("字典类型已存在"))
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除字典类型
|
||||
func (s *sSysDictType) Delete(ctx context.Context, dictIds []int) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
discs := ([]*entity.SysDictType)(nil)
|
||||
err = dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictType).
|
||||
Where(dao.SysDictType.Columns().DictId+" in (?) ", dictIds).Scan(&discs)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
types := garray.NewStrArray()
|
||||
for _, dt := range discs {
|
||||
types.Append(dt.DictType)
|
||||
}
|
||||
if types.Len() > 0 {
|
||||
_, err = dao.SysDictType.Ctx(ctx).TX(tx).Delete(dao.SysDictType.Columns().DictId+" in (?) ", dictIds)
|
||||
liberr.ErrIsNil(ctx, err, "删除类型失败")
|
||||
_, err = dao.SysDictData.Ctx(ctx).TX(tx).Delete(dao.SysDictData.Columns().DictType+" in (?) ", types.Slice())
|
||||
liberr.ErrIsNil(ctx, err, "删除字典数据失败")
|
||||
}
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllDictType 获取所有正常状态下的字典类型
|
||||
func (s *sSysDictType) GetAllDictType(ctx context.Context) (list []*entity.SysDictType, err error) {
|
||||
cache := service.Cache()
|
||||
//从缓存获取
|
||||
data := cache.Get(ctx, consts.CacheSysDict+"_dict_type_all")
|
||||
if !data.IsNil() {
|
||||
err = data.Structs(&list)
|
||||
return
|
||||
}
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictType.Ctx(ctx).Where("status", 1).Order("dict_id ASC").Scan(&list)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型数据出错")
|
||||
//缓存
|
||||
cache.Set(ctx, consts.CacheSysDict+"_dict_type_all", list, 0, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user