|
|
@@ -10,6 +10,8 @@ package service
|
|
|
import "C"
|
|
|
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"
|
|
|
@@ -27,6 +29,8 @@ type IDictType interface {
|
|
|
List(ctx context.Context, req *system.DictTypeSearchReq) (res *system.DictTypeSearchRes, err error)
|
|
|
Add(ctx context.Context, req *system.DictTypeAddReq, userId uint64) (err error)
|
|
|
Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error)
|
|
|
+ Edit(ctx context.Context, req *system.DictTypeEditReq, userId uint64) (err error)
|
|
|
+ Delete(ctx context.Context, dictIds []int) (err error)
|
|
|
}
|
|
|
|
|
|
type dictTypeImpl struct {
|
|
|
@@ -87,6 +91,37 @@ func (s *dictTypeImpl) Add(ctx context.Context, req *system.DictTypeAddReq, user
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// Edit 修改字典类型
|
|
|
+func (s *dictTypeImpl) 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(func() {
|
|
|
+ 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, "修改字典数据失败")
|
|
|
+ //清除缓存
|
|
|
+ Cache().RemoveByTag(ctx, commonConsts.CacheSysDictTag)
|
|
|
+ })
|
|
|
+ return err
|
|
|
+ })
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func (s *dictTypeImpl) Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error) {
|
|
|
err = g.Try(func() {
|
|
|
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictId, req.DictId).Scan(&dictType)
|
|
|
@@ -111,3 +146,29 @@ func (s *dictTypeImpl) ExistsDictType(ctx context.Context, dictType string, dict
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// Delete 删除字典类型
|
|
|
+func (s *dictTypeImpl) Delete(ctx context.Context, dictIds []int) (err error) {
|
|
|
+ err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
|
|
+ err = g.Try(func() {
|
|
|
+ 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, "删除字典数据失败")
|
|
|
+ }
|
|
|
+ //清除缓存
|
|
|
+ Cache().RemoveByTag(ctx, commonConsts.CacheSysDictTag)
|
|
|
+ })
|
|
|
+ return err
|
|
|
+ })
|
|
|
+ return
|
|
|
+}
|