2021-06-07 17:22:07 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
|
|
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
|
"mayfly-go/pkg/model"
|
2021-06-07 17:22:07 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
type Resource interface {
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
GetById(id uint64, cols ...string) *entity.Resource
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetByIdIn(ids []uint64, toEntity any, cols ...string)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
|
|
|
|
|
Save(entity *entity.Resource)
|
|
|
|
|
|
|
|
|
|
|
|
Delete(id uint64)
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetAccountResources(accountId uint64, toEntity any)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func newResourceApp(resourceRepo repository.Resource) Resource {
|
|
|
|
|
|
return &resourceAppImpl{
|
|
|
|
|
|
resourceRepo: resourceRepo,
|
|
|
|
|
|
}
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type resourceAppImpl struct {
|
|
|
|
|
|
resourceRepo repository.Resource
|
2021-06-07 17:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (r *resourceAppImpl) GetResourceList(condition *entity.Resource, toEntity any, orderBy ...string) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
r.resourceRepo.GetResourceList(condition, toEntity, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *resourceAppImpl) GetById(id uint64, cols ...string) *entity.Resource {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
return r.resourceRepo.GetById(id, cols...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (r *resourceAppImpl) GetByIdIn(ids []uint64, toEntity any, orderBy ...string) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
r.resourceRepo.GetByIdIn(ids, toEntity, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *resourceAppImpl) Save(resource *entity.Resource) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
if resource.Id != 0 {
|
|
|
|
|
|
if resource.Code != "" {
|
|
|
|
|
|
oldRes := r.GetById(resource.Id, "Code")
|
|
|
|
|
|
// 如果修改了code,则校验新code是否存在
|
|
|
|
|
|
if oldRes.Code != resource.Code {
|
|
|
|
|
|
r.checkCode(resource.Code)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
model.UpdateById(resource)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if pid := resource.Pid; pid != 0 {
|
|
|
|
|
|
biz.IsTrue(r.GetById(uint64(pid)) != nil, "该父资源不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
// 默认启用状态
|
|
|
|
|
|
if resource.Status == 0 {
|
|
|
|
|
|
resource.Status = entity.ResourceStatusEnable
|
|
|
|
|
|
}
|
|
|
|
|
|
r.checkCode(resource.Code)
|
|
|
|
|
|
model.Insert(resource)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *resourceAppImpl) checkCode(code string) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
biz.IsTrue(!strings.Contains(code, ","), "code不能包含','")
|
|
|
|
|
|
biz.IsEquals(model.CountBy(&entity.Resource{Code: code}), int64(0), "该code已存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
func (r *resourceAppImpl) Delete(id uint64) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
// 查找pid == id的资源
|
|
|
|
|
|
condition := &entity.Resource{Pid: int(id)}
|
|
|
|
|
|
var resources resourceList
|
|
|
|
|
|
r.resourceRepo.GetResourceList(condition, &resources)
|
|
|
|
|
|
|
|
|
|
|
|
biz.IsTrue(len(resources) == 0, "请先删除该资源的所有子资源")
|
|
|
|
|
|
model.DeleteById(condition, id)
|
|
|
|
|
|
// 删除角色关联的资源信息
|
|
|
|
|
|
model.DeleteByCondition(&entity.RoleResource{ResourceId: id})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
|
func (r *resourceAppImpl) GetAccountResources(accountId uint64, toEntity any) {
|
2021-06-07 17:22:07 +08:00
|
|
|
|
r.resourceRepo.GetAccountResources(accountId, toEntity)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type resourceList []entity.Resource
|