update_agent_logic.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package agent
  2. import (
  3. "context"
  4. "errors"
  5. "wechat-api/ent/agent"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateAgentLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewUpdateAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAgentLogic {
  18. return &UpdateAgentLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. func (l *UpdateAgentLogic) UpdateAgent(req *types.AgentInfo) (*types.BaseMsgResp, error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. if *req.Id <= 0 {
  27. l.Error("记录ID不存在")
  28. return nil, errors.New("记录ID不存在")
  29. }
  30. item, err := l.svcCtx.DB.Agent.Query().Where(agent.ID(*req.Id), agent.OrganizationID(organizationId)).First(l.ctx)
  31. if item.ID == 0 {
  32. return nil, errors.New("记录不存在")
  33. }
  34. err = l.svcCtx.DB.Agent.UpdateOneID(*req.Id).
  35. SetNotNilName(req.Name).
  36. SetNotNilRole(req.Role).
  37. SetNotNilStatus(req.Status).
  38. SetNotNilBackground(req.Background).
  39. SetNotNilExamples(req.Examples).
  40. Exec(l.ctx)
  41. if err != nil {
  42. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  43. }
  44. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  45. }