get_select_wx_list_logic.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package Wx
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "strings"
  6. "wechat-api/ent"
  7. "wechat-api/ent/predicate"
  8. "wechat-api/ent/wx"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetSelectWxListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetSelectWxListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSelectWxListLogic {
  20. return &GetSelectWxListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetSelectWxListLogic) GetSelectWxList(req *types.WxSelectListReq) (resp *types.WxSelectListResp, err error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. isAdmin := l.ctx.Value("isAdmin").(bool)
  28. var predicates []predicate.Wx
  29. ctype := uint64(1)
  30. if req.Ctype != nil && *req.Ctype > 0 {
  31. ctype = *req.Ctype
  32. }
  33. predicates = append(predicates, wx.Ctype(ctype))
  34. // 组织 ID 条件逻辑
  35. if !isAdmin {
  36. // 普通用户:只能查自己的组织
  37. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  38. } else {
  39. // 超管
  40. if req.OrganizationId == nil {
  41. // 未传,查自己的
  42. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  43. } else if *req.OrganizationId > 0 {
  44. // 传了正整数,查传入的
  45. predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
  46. }
  47. // 传了 0:不加任何条件(查全部)
  48. }
  49. data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Order(ent.Desc(wx.FieldOrganizationID)).All(l.ctx)
  50. if err != nil {
  51. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  52. }
  53. resp = &types.WxSelectListResp{}
  54. resp.Msg = errormsg.Success
  55. for _, v := range data {
  56. wxid := v.Wxid
  57. if !strings.HasPrefix(wxid, "temp-") {
  58. resp.Data = append(resp.Data,
  59. types.WxSelectListInfo{
  60. BaseIDInfo: types.BaseIDInfo{
  61. Id: &v.ID,
  62. },
  63. Wxid: &wxid,
  64. Nickname: &v.Nickname,
  65. Ctype: &v.Ctype,
  66. })
  67. }
  68. }
  69. return resp, nil
  70. }