From 758cc858a1caf6bf2869d5400370185d11cbf455 Mon Sep 17 00:00:00 2001 From: yxh Date: Thu, 7 Apr 2022 18:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/system/sys_dept.go | 40 +++++++-- internal/app/system/consts/cache.go | 4 +- internal/app/system/controller/sys_dept.go | 11 +++ internal/app/system/service/sys_auth_rule.go | 6 +- internal/app/system/service/sys_dept.go | 95 ++++++++++++++++++-- 5 files changed, 134 insertions(+), 22 deletions(-) diff --git a/api/v1/system/sys_dept.go b/api/v1/system/sys_dept.go index 90a37bd..87cb633 100644 --- a/api/v1/system/sys_dept.go +++ b/api/v1/system/sys_dept.go @@ -24,16 +24,38 @@ type DeptSearchRes struct { } type DeptAddReq struct { - g.Meta `path:"/dept/add" tags:"部门管理" method:"post" summary:"添加部门"` - ParentID int `p:"parentId" v:"required#父级不能为空"` - DeptName string `p:"deptName" v:"required#部门名称不能为空"` - OrderNum int `p:"orderNum" v:"required#排序不能为空"` - Leader string `p:"leader"` - Phone string `p:"phone"` - Email string `p:"email" v:"email#邮箱格式不正确"` - Status uint `p:"status" v:"required#状态必须"` - Ancestors string `p:"ancestors"` + g.Meta `path:"/dept/add" tags:"部门管理" method:"post" summary:"添加部门"` + ParentID int `p:"parentId" v:"required#父级不能为空"` + DeptName string `p:"deptName" v:"required#部门名称不能为空"` + OrderNum int `p:"orderNum" v:"required#排序不能为空"` + Leader string `p:"leader"` + Phone string `p:"phone"` + Email string `p:"email" v:"email#邮箱格式不正确"` + Status uint `p:"status" v:"required#状态必须"` } type DeptAddRes struct { } + +type DeptEditReq struct { + g.Meta `path:"/dept/edit" tags:"部门管理" method:"put" summary:"修改部门"` + DeptId int `p:"deptId" v:"required#deptId不能为空"` + ParentID int `p:"parentId" v:"required#父级不能为空"` + DeptName string `p:"deptName" v:"required#部门名称不能为空"` + OrderNum int `p:"orderNum" v:"required#排序不能为空"` + Leader string `p:"leader"` + Phone string `p:"phone"` + Email string `p:"email" v:"email#邮箱格式不正确"` + Status uint `p:"status" v:"required#状态必须"` +} + +type DeptEditRes struct { +} + +type DeptDeleteReq struct { + g.Meta `path:"/dept/delete" tags:"部门管理" method:"delete" summary:"删除部门"` + Id int `p:"id" v:"required#id不能为空"` +} + +type DeptDeleteRes struct { +} diff --git a/internal/app/system/consts/cache.go b/internal/app/system/consts/cache.go index 3cf9764..7bc5ea4 100644 --- a/internal/app/system/consts/cache.go +++ b/internal/app/system/consts/cache.go @@ -8,8 +8,10 @@ package consts const ( - // CacheSysAuthMenu 缓存菜单KEY + // CacheSysAuthMenu 缓存菜单key CacheSysAuthMenu = "sysAuthMenu" + // CacheSysDept 缓存部门key + CacheSysDept = "sysDept" // CacheSysRole 角色缓存key CacheSysRole = "sysRole" diff --git a/internal/app/system/controller/sys_dept.go b/internal/app/system/controller/sys_dept.go index 928048d..e7c453b 100644 --- a/internal/app/system/controller/sys_dept.go +++ b/internal/app/system/controller/sys_dept.go @@ -31,3 +31,14 @@ func (c *sysDeptController) Add(ctx context.Context, req *system.DeptAddReq) (re err = service.Dept().Add(ctx, req) return } + +// Edit 修改部门 +func (c *sysDeptController) Edit(ctx context.Context, req *system.DeptEditReq) (res *system.DeptEditRes, err error) { + err = service.Dept().Edit(ctx, req) + return +} + +func (c *sysDeptController) Delete(ctx context.Context, req *system.DeptDeleteReq) (res *system.DeptDeleteRes, err error) { + + return +} diff --git a/internal/app/system/service/sys_auth_rule.go b/internal/app/system/service/sys_auth_rule.go index 57f9382..ab86678 100644 --- a/internal/app/system/service/sys_auth_rule.go +++ b/internal/app/system/service/sys_auth_rule.go @@ -15,7 +15,6 @@ import ( "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/service" commonService "github.com/tiger1103/gfast/v3/internal/app/common/service" "github.com/tiger1103/gfast/v3/internal/app/system/consts" "github.com/tiger1103/gfast/v3/internal/app/system/model" @@ -79,11 +78,12 @@ func (s *ruleImpl) GetIsMenuList(ctx context.Context) ([]*model.SysAuthRuleInfoR // GetMenuList 获取所有菜单 func (s *ruleImpl) GetMenuList(ctx context.Context) (list []*model.SysAuthRuleInfoRes, err error) { - cache := service.Cache() + cache := commonService.Cache() //从缓存获取 iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysAuthMenu, s.getMenuListFromDb, 0, consts.CacheSysAuthTag) if iList != nil { err = gconv.Struct(iList, &list) + liberr.ErrIsNil(ctx, err) } return } @@ -248,7 +248,7 @@ func (s *ruleImpl) Update(ctx context.Context, req *system.RuleUpdateReq) (err e func (s *ruleImpl) UpdateRoleRule(ctx context.Context, ruleId uint, roleIds []uint) (err error) { err = g.Try(func() { - enforcer, e := service.CasbinEnforcer(ctx) + enforcer, e := commonService.CasbinEnforcer(ctx) liberr.ErrIsNil(ctx, e) //删除旧权限 _, e = enforcer.RemoveFilteredPolicy(1, gconv.String(ruleId)) diff --git a/internal/app/system/service/sys_dept.go b/internal/app/system/service/sys_dept.go index a2e3887..f2b7cf9 100644 --- a/internal/app/system/service/sys_dept.go +++ b/internal/app/system/service/sys_dept.go @@ -10,6 +10,7 @@ package service import ( "context" "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" commonService "github.com/tiger1103/gfast/v3/internal/app/common/service" @@ -23,6 +24,8 @@ import ( type IDept interface { GetList(ctx context.Context, req *system.DeptSearchReq) (list []*entity.SysDept, err error) Add(ctx context.Context, req *system.DeptAddReq) (err error) + Edit(ctx context.Context, req *system.DeptEditReq) (err error) + GetFromCache(ctx context.Context) (list []*entity.SysDept, err error) } var deptService = deptImpl{} @@ -35,16 +38,40 @@ type deptImpl struct { } func (s *deptImpl) GetList(ctx context.Context, req *system.DeptSearchReq) (list []*entity.SysDept, err error) { + list, err = s.GetFromCache(ctx) + if err != nil { + return + } + rList := make([]*entity.SysDept, 0, len(list)) + if req.DeptName != "" || req.Status != "" { + for _, v := range list { + if req.DeptName != "" && !gstr.ContainsI(v.DeptName, req.DeptName) { + continue + } + if req.Status != "" && v.Status != gconv.Uint(req.Status) { + continue + } + rList = append(rList, v) + } + list = rList + } + return +} + +func (s *deptImpl) GetFromCache(ctx context.Context) (list []*entity.SysDept, err error) { err = g.Try(func() { - m := dao.SysDept.Ctx(ctx) - if req.DeptName != "" { - m = m.Where("dept_name like ?", "%"+req.DeptName+"%") + cache := commonService.Cache() + //从缓存获取 + iList := cache.GetOrSetFuncLock(ctx, consts.CacheSysDept, func(ctx context.Context) (value interface{}, err error) { + err = dao.SysDept.Ctx(ctx).Scan(&list) + liberr.ErrIsNil(ctx, err, "获取部门列表失败") + value = list + return + }, 0, consts.CacheSysAuthTag) + if iList != nil { + err = gconv.Struct(iList, &list) + liberr.ErrIsNil(ctx, err) } - if req.Status != "" { - m = m.Where("status", gconv.Int(req.Status)) - } - err = m.Scan(&list) - liberr.ErrIsNil(ctx, err, "获取角色列表失败") }) return } @@ -54,7 +81,6 @@ func (s *deptImpl) Add(ctx context.Context, req *system.DeptAddReq) (err error) err = g.Try(func() { _, err = dao.SysDept.Ctx(ctx).Insert(do.SysDept{ ParentId: req.ParentID, - Ancestors: req.Ancestors, DeptName: req.DeptName, OrderNum: req.OrderNum, Leader: req.Leader, @@ -69,3 +95,54 @@ func (s *deptImpl) Add(ctx context.Context, req *system.DeptAddReq) (err error) }) return } + +// Edit 部门修改 +func (s *deptImpl) Edit(ctx context.Context, req *system.DeptEditReq) (err error) { + err = g.Try(func() { + _, err = dao.SysDept.Ctx(ctx).WherePri(req.DeptId).Update(do.SysDept{ + ParentId: req.ParentID, + DeptName: req.DeptName, + OrderNum: req.OrderNum, + Leader: req.Leader, + Phone: req.Phone, + Email: req.Email, + Status: req.Status, + CreatedBy: Context().GetUserId(ctx), + }) + liberr.ErrIsNil(ctx, err, "修改部门失败") + // 删除缓存 + commonService.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag) + }) + return +} + +func (s *deptImpl) Delete(ctx context.Context, id int64) (err error) { + err = g.Try(func() { + var list []*entity.SysDept + err = dao.SysDept.Ctx(ctx).Scan(&list) + liberr.ErrIsNil(ctx, err, "不存在部门信息") + children := s.FindSonByParentId(list, id) + ids := make([]int64, 0, len(list)) + for _, v := range children { + ids = append(ids, v.DeptId) + } + ids = append(ids, id) + _, err = dao.SysDept.Ctx(ctx).Where(dao.SysDept.Columns().DeptId+" in (?)", ids).Delete() + liberr.ErrIsNil(ctx, err, "删除部门失败") + // 删除缓存 + commonService.Cache().RemoveByTag(ctx, consts.CacheSysAuthTag) + }) + return +} + +func (s *deptImpl) FindSonByParentId(deptList []*entity.SysDept, deptId int64) []*entity.SysDept { + children := make([]*entity.SysDept, 0, len(deptList)) + for _, v := range deptList { + if v.ParentId == deptId { + children = append(children, v) + fChildren := s.FindSonByParentId(deptList, v.DeptId) + children = append(children, fChildren...) + } + } + return children +}