change_organization_logic.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package Wx
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "wechat-api/ent/wx"
  7. "wechat-api/internal/service/wechat"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type ChangeOrganizationLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewChangeOrganizationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeOrganizationLogic {
  18. return &ChangeOrganizationLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *ChangeOrganizationLogic) ChangeOrganization(req *types.WxInfo) (resp *types.BaseMsgResp, err error) {
  24. isAdmin := l.ctx.Value("isAdmin").(bool)
  25. if !isAdmin {
  26. return nil, errorx.NewDefaultError("仅管理员可调整租户")
  27. }
  28. tx, err := l.svcCtx.DB.Tx(l.ctx)
  29. if err != nil {
  30. return nil, err
  31. }
  32. change := wechat.WxSafeChange{Ctx: l.ctx, Tx: tx, WxId: *req.Wxid, OrganizationId: *req.OrganizationId}
  33. err = change.KeepAllSafe()
  34. if err != nil {
  35. _ = tx.Rollback()
  36. return nil, err
  37. }
  38. _, err = tx.Wx.Update().
  39. Where(wx.WxidEQ(*req.Wxid)).
  40. SetNotNilOrganizationID(req.OrganizationId).
  41. Save(l.ctx)
  42. if err != nil {
  43. _ = tx.Rollback()
  44. return nil, err
  45. }
  46. err = tx.Commit()
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  51. }