This commit is contained in:
2025-07-07 20:11:59 +08:00
parent ab0fdbc447
commit 06e3aa2eb3
2009 changed files with 193082 additions and 0 deletions

View File

@ -0,0 +1,127 @@
// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期2024-05-28 15:11:36
// 生成路径: internal/app/system/logic/app_roles.go
// 生成人gfast
// desc:app角色
// company:云南奇讯科技有限公司
// ==========================================================================
package logic
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/tiger1103/gfast/v3/api/v1/system"
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tiger1103/gfast/v3/internal/app/system/model"
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
"github.com/tiger1103/gfast/v3/internal/app/system/service"
"github.com/tiger1103/gfast/v3/library/liberr"
)
func init() {
service.RegisterAppRoles(New())
}
func New() *sAppRoles {
return &sAppRoles{}
}
type sAppRoles struct{}
func (s *sAppRoles) List(ctx context.Context, req *system.AppRolesSearchReq) (listRes *system.AppRolesSearchRes, err error) {
listRes = new(system.AppRolesSearchRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.AppRoles.Ctx(ctx).WithAll()
if req.RoleId != "" {
m = m.Where(dao.AppRoles.Columns().RoleId+" = ?", req.RoleId)
}
if req.RoleName != "" {
m = m.Where(dao.AppRoles.Columns().RoleName+" like ?", "%"+req.RoleName+"%")
}
if len(req.DateRange) != 0 {
m = m.Where(dao.AppRoles.Columns().CreatedAt+" >=? AND "+dao.AppRoles.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
}
listRes.Total, err = m.Count()
liberr.ErrIsNil(ctx, err, "获取总行数失败")
if req.PageNum == 0 {
req.PageNum = 1
}
listRes.CurrentPage = req.PageNum
if req.PageSize == 0 {
req.PageSize = consts.PageSize
}
order := "role_id asc"
if req.OrderBy != "" {
order = req.OrderBy
}
var res []*model.AppRolesInfoRes
err = m.Fields(system.AppRolesSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.AppRolesListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.AppRolesListRes{
RoleId: v.RoleId,
RoleName: v.RoleName,
CreatedAt: v.CreatedAt,
}
}
})
return
}
func (s *sAppRoles) GetByRoleId(ctx context.Context, roleId uint) (res *model.AppRolesInfoRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
err = dao.AppRoles.Ctx(ctx).WithAll().Where(dao.AppRoles.Columns().RoleId, roleId).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取信息失败")
})
return
}
func (s *sAppRoles) Add(ctx context.Context, req *system.AppRolesAddReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppRoles.Ctx(ctx).Insert(do.AppRoles{
RoleName: req.RoleName,
})
liberr.ErrIsNil(ctx, err, "添加失败")
})
return
}
func (s *sAppRoles) Edit(ctx context.Context, req *system.AppRolesEditReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppRoles.Ctx(ctx).WherePri(req.RoleId).Update(do.AppRoles{
RoleName: req.RoleName,
})
liberr.ErrIsNil(ctx, err, "修改失败")
// 修改角色菜单
if len(req.MenuIds) > 0 {
insertData := make([]do.AppRoleMenus, 0, len(req.MenuIds))
for _, menuId := range req.MenuIds {
insertData = append(insertData, do.AppRoleMenus{
RoleId: req.RoleId,
MenuId: menuId,
})
}
// 删除原有的角色菜单
_, err := dao.AppRoleMenus.Ctx(ctx).Where("role_id", req.RoleId).Delete()
liberr.ErrIsNil(ctx, err, "删除角色菜单失败")
_, err = dao.AppRoleMenus.Ctx(ctx).Data(insertData).Insert()
liberr.ErrIsNil(ctx, err, "添加角色菜单失败")
}
})
return
}
func (s *sAppRoles) Delete(ctx context.Context, roleIds []uint) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppRoles.Ctx(ctx).Delete(dao.AppRoles.Columns().RoleId+" in (?)", roleIds)
liberr.ErrIsNil(ctx, err, "删除失败")
})
return
}