v3_1
This commit is contained in:
@@ -3,19 +3,24 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"management/internal/db/model/dto"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/model/form"
|
||||
"management/internal/erpserver/model/view"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/know"
|
||||
"management/internal/pkg/redis"
|
||||
)
|
||||
|
||||
type RoleBiz interface {
|
||||
Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error)
|
||||
Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error)
|
||||
Create(ctx context.Context, req *form.Role) error
|
||||
Update(ctx context.Context, req *form.Role) error
|
||||
CreateOrUpdate(ctx context.Context, req *form.Role) error
|
||||
All(ctx context.Context) ([]*db.SysRole, error)
|
||||
List(ctx context.Context, q dto.SearchDto) ([]*db.SysRole, int64, error)
|
||||
Get(ctx context.Context, id int32) (*db.SysRole, error)
|
||||
@@ -44,12 +49,140 @@ func NewRole(store db.Store, redis redis.IRedis) *roleBiz {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *roleBiz) Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error) {
|
||||
return b.store.CreateSysRole(ctx, arg)
|
||||
func (b *roleBiz) Create(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.CreateSysRoleParams{
|
||||
Name: req.Name,
|
||||
DisplayName: req.DisplayName,
|
||||
Vip: false,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err := b.store.CreateSysRole(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("角色名称已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *roleBiz) Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error) {
|
||||
return b.store.UpdateSysRole(ctx, arg)
|
||||
func (b *roleBiz) Update(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
role, err := b.store.GetSysRole(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysRoleParams{
|
||||
ID: role.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Status: *req.Status,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Sort: order,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.UpdateSysRole(ctx, arg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *roleBiz) CreateOrUpdate(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
if *req.ID > 0 {
|
||||
role, err := b.store.GetSysRole(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysRoleParams{
|
||||
ID: role.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Status: *req.Status,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Sort: order,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.UpdateSysRole(ctx, arg)
|
||||
return err
|
||||
} else {
|
||||
arg := &db.CreateSysRoleParams{
|
||||
Name: req.Name,
|
||||
DisplayName: req.DisplayName,
|
||||
Vip: false,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err := b.store.CreateSysRole(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("角色名称已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *roleBiz) All(ctx context.Context) ([]*db.SysRole, error) {
|
||||
|
||||
Reference in New Issue
Block a user