updateapplicationstatuslogic.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.2
  3. package application
  4. import (
  5. "context"
  6. "errors"
  7. "udeServer/internal/apierr"
  8. "udeServer/internal/log"
  9. "udeServer/internal/model"
  10. "udeServer/internal/svc"
  11. "udeServer/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type UpdateApplicationStatusLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. // 切换应用凭证状态(启用/禁用互换)
  20. func NewUpdateApplicationStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApplicationStatusLogic {
  21. return &UpdateApplicationStatusLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *UpdateApplicationStatusLogic) UpdateApplicationStatus(req *types.ApplicationCredentialsUpdateStatusReq) (*types.ApplicationCredentialsUpdateStatusResp, error) {
  28. if err := checkId(req.Id); err != nil {
  29. return nil, err
  30. }
  31. cred, err := l.svcCtx.ApplicationCredModel.FindOne(l.ctx, req.Id)
  32. if errors.Is(err, model.ErrNotFound) {
  33. return nil, apierr.NotFound("application credential", req.Id)
  34. }
  35. if err != nil {
  36. log.RecordError(l.ctx, err)
  37. return nil, apierr.New(500, "查询应用凭证失败")
  38. }
  39. // 启用/禁用互换:1<->2;其他状态视为数据异常
  40. toggle := map[int64]int64{1: 2, 2: 1}
  41. newStatus, ok := toggle[cred.Status]
  42. if !ok {
  43. return nil, apierr.BadRequest("当前应用状态(status=%d)不支持切换", cred.Status)
  44. }
  45. cred.Status = newStatus
  46. cred.UpdatedBy = operatorName
  47. if err := l.svcCtx.ApplicationCredModel.Update(l.ctx, cred); err != nil {
  48. log.RecordError(l.ctx, err)
  49. return nil, apierr.New(500, "更新应用凭证状态失败")
  50. }
  51. return &types.ApplicationCredentialsUpdateStatusResp{Iden: cred.CredIden}, nil
  52. }