Bladeren bron

字典类型增删改查

yxh 4 jaren geleden
bovenliggende
commit
76c7ac2443

+ 20 - 0
api/v1/system/sys_dict_type.go

@@ -48,3 +48,23 @@ type DictTypeGetRes struct {
 	g.Meta   `mime:"application/json"`
 	g.Meta   `mime:"application/json"`
 	DictType *entity.SysDictType `json:"dictType"`
 	DictType *entity.SysDictType `json:"dictType"`
 }
 }
+
+type DictTypeEditReq struct {
+	g.Meta   `path:"/system/dict/type/edit" tags:"字典管理" method:"put" summary:"修改字典类型"`
+	DictId   int64  `p:"dictId" v:"required|min:1#主键ID不能为空|主键ID必须为大于0的值"`
+	DictName string `p:"dictName"  v:"required#字典名称不能为空"`
+	DictType string `p:"dictType"  v:"required#字典类型不能为空"`
+	Status   uint   `p:"status"  v:"required|in:0,1#状态不能为空|状态只能为0或1"`
+	Remark   string `p:"remark"`
+}
+
+type DictTypeEditRes struct {
+}
+
+type DictTypeDeleteReq struct {
+	g.Meta  `path:"/system/dict/type/delete" tags:"字典管理" method:"delete" summary:"删除字典类型"`
+	DictIds []int `p:"dictIds" v:"required#字典类型id不能为空"`
+}
+
+type DictTypeDeleteRes struct {
+}

+ 61 - 0
internal/app/common/service/sys_dict_type.go

@@ -10,6 +10,8 @@ package service
 import "C"
 import "C"
 import (
 import (
 	"context"
 	"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/errors/gerror"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/util/gconv"
 	"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)
 	List(ctx context.Context, req *system.DictTypeSearchReq) (res *system.DictTypeSearchRes, err error)
 	Add(ctx context.Context, req *system.DictTypeAddReq, userId uint64) (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)
 	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 {
 type dictTypeImpl struct {
@@ -87,6 +91,37 @@ func (s *dictTypeImpl) Add(ctx context.Context, req *system.DictTypeAddReq, user
 	return
 	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) {
 func (s *dictTypeImpl) Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error) {
 	err = g.Try(func() {
 	err = g.Try(func() {
 		err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictId, req.DictId).Scan(&dictType)
 		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
 	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
+}

+ 13 - 0
internal/app/system/controller/sys_dict_type.go

@@ -31,7 +31,20 @@ func (c *SysDictTypeController) Add(ctx context.Context, req *system.DictTypeAdd
 	return
 	return
 }
 }
 
 
+// Get 获取字典类型
 func (c *SysDictTypeController) Get(ctx context.Context, req *system.DictTypeGetReq) (res *system.DictTypeGetRes, err error) {
 func (c *SysDictTypeController) Get(ctx context.Context, req *system.DictTypeGetReq) (res *system.DictTypeGetRes, err error) {
+	res = new(system.DictTypeGetRes)
+	res.DictType, err = commonService.DictType().Get(ctx, req)
+	return
+}
+
+// Edit 修改字典数据
+func (c *SysDictTypeController) Edit(ctx context.Context, req *system.DictTypeEditReq) (res *system.DictTypeEditRes, err error) {
+	err = commonService.DictType().Edit(ctx, req, service.Context().GetUserId(ctx))
+	return
+}
 
 
+func (c *SysDictTypeController) Delete(ctx context.Context, req *system.DictTypeDeleteReq) (res *system.DictTypeDeleteRes, err error) {
+	err = commonService.DictType().Delete(ctx, req.DictIds)
 	return
 	return
 }
 }