createRoleLogic.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package role
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "perms-system-server/internal/consts"
  7. authHelper "perms-system-server/internal/logic/auth"
  8. roleModel "perms-system-server/internal/model/role"
  9. "perms-system-server/internal/response"
  10. "perms-system-server/internal/svc"
  11. "perms-system-server/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type CreateRoleLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
  20. return &CreateRoleLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleReq) (resp *types.IdResp, err error) {
  27. if err := authHelper.RequireProductAdmin(l.ctx); err != nil {
  28. return nil, err
  29. }
  30. now := time.Now().Unix()
  31. result, err := l.svcCtx.SysRoleModel.Insert(l.ctx, &roleModel.SysRole{
  32. ProductCode: req.ProductCode,
  33. Name: req.Name,
  34. Remark: req.Remark,
  35. Status: consts.StatusEnabled,
  36. PermsLevel: req.PermsLevel,
  37. CreateTime: now,
  38. UpdateTime: now,
  39. })
  40. if err != nil {
  41. if strings.Contains(err.Error(), "1062") || strings.Contains(err.Error(), "Duplicate entry") {
  42. return nil, response.ErrConflict("该产品下角色名已存在")
  43. }
  44. return nil, err
  45. }
  46. id, _ := result.LastInsertId()
  47. return &types.IdResp{Id: id}, nil
  48. }