2021-06-07 17:22:07 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-10-12 21:50:55 +08:00
|
|
|
|
"mayfly-go/internal/common/consts"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-07-01 14:34:42 +08:00
|
|
|
|
"mayfly-go/pkg/gormx"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"strings"
|
2023-06-15 19:18:29 +08:00
|
|
|
|
"time"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type Resource interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.Resource]
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Save(entity *entity.Resource) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Delete(id uint64) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
ChangeStatus(resourceId uint64, status int8) error
|
2023-06-15 19:18:29 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
Sort(re *entity.Resource) error
|
2023-06-15 19:18:29 +08:00
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetAccountResources(accountId uint64, toEntity any) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func newResourceApp(resourceRepo repository.Resource) Resource {
|
|
|
|
|
|
return &resourceAppImpl{
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Resource, repository.Resource]{Repo: resourceRepo},
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type resourceAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Resource, repository.Resource]
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) Save(resource *entity.Resource) error {
|
2023-06-15 19:18:29 +08:00
|
|
|
|
// 更新操作
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if resource.Id != 0 {
|
|
|
|
|
|
if resource.Code != "" {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
oldRes, err := r.GetById(new(entity.Resource), resource.Id, "Code")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("更新失败, 该资源不存在")
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 如果修改了code,则校验新code是否存在
|
|
|
|
|
|
if oldRes.Code != resource.Code {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err := r.checkCode(resource.Code); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.UpdateById(resource)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 生成随机八位唯一标识符
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ui := stringx.Rand(8)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
if pid := resource.Pid; pid != 0 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
pResource, err := r.GetById(new(entity.Resource), uint64(pid))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("该父资源不存在")
|
|
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
resource.UiPath = pResource.UiPath + ui + entity.ResourceUiPathSp
|
2021-06-07 17:22:07 +08:00
|
|
|
|
} else {
|
2023-06-15 19:18:29 +08:00
|
|
|
|
resource.UiPath = ui + entity.ResourceUiPathSp
|
|
|
|
|
|
}
|
|
|
|
|
|
// 默认启用状态
|
|
|
|
|
|
if resource.Status == 0 {
|
|
|
|
|
|
resource.Status = entity.ResourceStatusEnable
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err := r.checkCode(resource.Code); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
resource.Weight = int(time.Now().Unix())
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.Insert(resource)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) ChangeStatus(resourceId uint64, status int8) error {
|
|
|
|
|
|
resource, err := r.GetById(new(entity.Resource), resourceId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("资源不存在")
|
|
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
resource.Status = status
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return r.GetRepo().UpdateByUiPathLike(resource)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) Sort(sortResource *entity.Resource) error {
|
|
|
|
|
|
resource, err := r.GetById(new(entity.Resource), sortResource.Id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("资源不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-15 19:18:29 +08:00
|
|
|
|
// 未改变父节点,则更新排序值即可
|
|
|
|
|
|
if sortResource.Pid == resource.Pid {
|
|
|
|
|
|
saveE := &entity.Resource{Weight: sortResource.Weight}
|
|
|
|
|
|
saveE.Id = sortResource.Id
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return r.Save(saveE)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 若资源原本唯一标识路径为:xxxx/yyyy/zzzz/,则获取其父节点路径标识 xxxx/yyyy/ 与自身节点标识 zzzz/
|
|
|
|
|
|
splitStr := strings.Split(resource.UiPath, entity.ResourceUiPathSp)
|
|
|
|
|
|
// 获取 zzzz/
|
|
|
|
|
|
resourceUi := splitStr[len(splitStr)-2] + entity.ResourceUiPathSp
|
|
|
|
|
|
// 获取父资源路径 xxxx/yyyy/
|
|
|
|
|
|
var parentResourceUiPath string
|
|
|
|
|
|
if len(splitStr) > 2 {
|
|
|
|
|
|
parentResourceUiPath = strings.Split(resource.UiPath, resourceUi)[0]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
parentResourceUiPath = resourceUi
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
newParentResourceUiPath := ""
|
|
|
|
|
|
if sortResource.Pid != 0 {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
newParentResource, err := r.GetById(new(entity.Resource), uint64(sortResource.Pid))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("父资源不存在")
|
|
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
newParentResourceUiPath = newParentResource.UiPath
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
children := r.GetRepo().GetChildren(resource.UiPath)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
for _, v := range children {
|
|
|
|
|
|
if v.Id == sortResource.Id {
|
|
|
|
|
|
continue
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
updateUiPath := &entity.Resource{}
|
|
|
|
|
|
updateUiPath.Id = v.Id
|
|
|
|
|
|
if parentResourceUiPath == resourceUi {
|
|
|
|
|
|
updateUiPath.UiPath = newParentResourceUiPath + v.UiPath
|
|
|
|
|
|
} else {
|
|
|
|
|
|
updateUiPath.UiPath = strings.ReplaceAll(v.UiPath, parentResourceUiPath, newParentResourceUiPath)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
r.Save(updateUiPath)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新零值使用map,因为pid=0表示根节点
|
|
|
|
|
|
updateMap := map[string]interface{}{
|
|
|
|
|
|
"pid": sortResource.Pid,
|
|
|
|
|
|
"weight": sortResource.Weight,
|
|
|
|
|
|
"ui_path": newParentResourceUiPath + resourceUi,
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
condition := new(entity.Resource)
|
|
|
|
|
|
condition.Id = sortResource.Id
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return gormx.Updates(condition, updateMap)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) checkCode(code string) error {
|
|
|
|
|
|
if strings.Contains(code, ",") {
|
|
|
|
|
|
return errorx.NewBiz("code不能包含','")
|
|
|
|
|
|
}
|
|
|
|
|
|
if gormx.CountBy(&entity.Resource{Code: code}) == 0 {
|
|
|
|
|
|
return errorx.NewBiz("该code已存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) Delete(id uint64) error {
|
|
|
|
|
|
resource, err := r.GetById(new(entity.Resource), id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("资源不存在")
|
|
|
|
|
|
}
|
2023-06-15 19:18:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除当前节点及其所有子节点
|
2023-10-26 17:15:49 +08:00
|
|
|
|
children := r.GetRepo().GetChildren(resource.UiPath)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
for _, v := range children {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
r.GetRepo().DeleteById(v.Id)
|
2023-06-15 19:18:29 +08:00
|
|
|
|
// 删除角色关联的资源信息
|
2023-10-26 17:15:49 +08:00
|
|
|
|
gormx.DeleteBy(&entity.RoleResource{ResourceId: v.Id})
|
2023-06-15 19:18:29 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return nil
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *resourceAppImpl) GetAccountResources(accountId uint64, toEntity any) error {
|
2023-10-12 21:50:55 +08:00
|
|
|
|
// 超级管理员返回所有
|
|
|
|
|
|
if accountId == consts.AdminId {
|
|
|
|
|
|
cond := &entity.Resource{
|
|
|
|
|
|
Status: entity.ResourceStatusEnable,
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return r.ListByCondOrder(cond, toEntity, "pid asc", "weight asc")
|
2023-10-12 21:50:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return r.GetRepo().GetAccountResources(accountId, toEntity)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|