update_wx_logic.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package Wx
  2. import (
  3. "context"
  4. "wechat-api/ent/wx"
  5. "wechat-api/internal/svc"
  6. "wechat-api/internal/types"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type UpdateWxLogic struct {
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. logx.Logger
  15. }
  16. func NewUpdateWxLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWxLogic {
  17. return &UpdateWxLogic{
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. Logger: logx.WithContext(ctx),
  21. }
  22. }
  23. func (l *UpdateWxLogic) UpdateWx(req *types.WxInfo) (resp *types.BaseMsgResp, err error) {
  24. isAdmin := l.ctx.Value("isAdmin").(bool)
  25. if isAdmin {
  26. err = l.svcCtx.DB.Wx.UpdateOneID(*req.Id).
  27. SetNotNilCallback(req.Callback).
  28. SetNotNilAgentID(req.AgentId).
  29. SetNotNilOrganizationID(req.OrganizationId).
  30. SetNotNilAPIBase(req.ApiBase).
  31. SetNotNilAPIKey(req.ApiKey).
  32. Exec(l.ctx)
  33. } else {
  34. organizationId := l.ctx.Value("organizationId").(uint64)
  35. err = l.svcCtx.DB.Wx.UpdateOneID(*req.Id).
  36. Where(wx.OrganizationID(organizationId)).
  37. SetNotNilAgentID(req.AgentId).
  38. Exec(l.ctx)
  39. }
  40. if err != nil {
  41. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  42. }
  43. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  44. }