144 lines
3.5 KiB
Go
144 lines
3.5 KiB
Go
package storeRole
|
|
|
|
import (
|
|
"context"
|
|
"server/internal/dao"
|
|
"server/internal/model"
|
|
"server/internal/model/do"
|
|
"server/internal/service"
|
|
"server/utility/ecode"
|
|
)
|
|
|
|
type sStoreRole struct {
|
|
}
|
|
|
|
func New() service.IStoreRole {
|
|
return &sStoreRole{}
|
|
}
|
|
func init() {
|
|
service.RegisterStoreRole(New())
|
|
}
|
|
|
|
func (s *sStoreRole) Create(ctx context.Context, in *model.StoreRoleCreateIn) (out *model.StoreRoleCreateOut, err error) {
|
|
out = &model.StoreRoleCreateOut{}
|
|
|
|
// 检查角色名称是否已存在
|
|
exist, err := dao.StoreRoles.Ctx(ctx).Where(do.StoreRoles{
|
|
StoreId: in.StoreId,
|
|
Name: in.Name,
|
|
}).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("新增角色查重出现异常")
|
|
}
|
|
if exist {
|
|
return nil, ecode.Params.Sub("该门店下角色名称已存在")
|
|
}
|
|
|
|
// 创建角色
|
|
id, err := dao.StoreRoles.Ctx(ctx).InsertAndGetId(do.StoreRoles{
|
|
StoreId: in.StoreId,
|
|
Name: in.Name,
|
|
})
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("创建角色失败")
|
|
}
|
|
|
|
out.Id = id
|
|
return out, nil
|
|
}
|
|
|
|
func (s *sStoreRole) Update(ctx context.Context, in *model.StoreRoleUpdateIn) (out *model.StoreRoleUpdateOut, err error) {
|
|
out = &model.StoreRoleUpdateOut{}
|
|
|
|
// 检查角色是否存在
|
|
exist, err := dao.StoreRoles.Ctx(ctx).WherePri(in.Id).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色失败")
|
|
}
|
|
if !exist {
|
|
return nil, ecode.Params.Sub("角色不存在")
|
|
}
|
|
|
|
// 检查角色名称是否已存在(排除自身)
|
|
exist, err = dao.StoreRoles.Ctx(ctx).
|
|
Where(do.StoreRoles{
|
|
StoreId: in.StoreID,
|
|
Name: in.Name,
|
|
}).
|
|
WhereNot(dao.StoreRoles.Columns().Id, in.Id).
|
|
Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色名称失败")
|
|
}
|
|
if exist {
|
|
return nil, ecode.Params.Sub("该门店下角色名称已存在")
|
|
}
|
|
|
|
// 更新角色
|
|
if _, err = dao.StoreRoles.Ctx(ctx).
|
|
Where(do.StoreRoles{Id: in.Id}).
|
|
Update(do.StoreRoles{
|
|
Name: in.Name,
|
|
}); err != nil {
|
|
return nil, ecode.Fail.Sub("更新角色失败")
|
|
}
|
|
|
|
out.Success = true
|
|
return out, nil
|
|
}
|
|
|
|
func (s *sStoreRole) Delete(ctx context.Context, in *model.StoreRoleDeleteIn) (out *model.StoreRoleDeleteOut, err error) {
|
|
out = &model.StoreRoleDeleteOut{}
|
|
|
|
// 检查角色是否存在
|
|
exist, err := dao.StoreRoles.Ctx(ctx).WherePri(in.Id).Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色失败")
|
|
}
|
|
if !exist {
|
|
return nil, ecode.Params.Sub("角色不存在")
|
|
}
|
|
|
|
// 检查是否有管理员使用该角色
|
|
count, err := dao.StoreAdmins.Ctx(ctx).Where(do.StoreAdmins{RoleId: in.Id}).Count()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色使用情况失败")
|
|
}
|
|
if count > 0 {
|
|
return nil, ecode.Params.Sub("该角色下存在管理员,无法删除")
|
|
}
|
|
|
|
// 软删除角色
|
|
if _, err = dao.StoreRoles.Ctx(ctx).
|
|
Where(do.StoreRoles{Id: in.Id}).
|
|
Delete(); err != nil {
|
|
return nil, ecode.Fail.Sub("删除角色失败")
|
|
}
|
|
|
|
out.Success = true
|
|
return out, nil
|
|
}
|
|
|
|
func (s *sStoreRole) List(ctx context.Context, in *model.StoreRoleListIn) (out *model.StoreRoleListOut, err error) {
|
|
out = &model.StoreRoleListOut{}
|
|
|
|
// 构建查询条件
|
|
orm := dao.StoreRoles.Ctx(ctx)
|
|
if in.StoreId > 0 {
|
|
orm = orm.Where(do.StoreRoles{StoreId: in.StoreId})
|
|
}
|
|
|
|
// 获取分页数据
|
|
list := make([]model.StoreRoleListItem, 0)
|
|
var total int
|
|
if err = orm.Page(in.Page, in.Size).
|
|
OrderDesc(dao.StoreRoles.Columns().Id).
|
|
ScanAndCount(&list, &total, false); err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色列表失败")
|
|
}
|
|
|
|
out.List = list
|
|
out.Total = total
|
|
return out, nil
|
|
}
|