get_select_wx_list_logic.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. var ctype uint64 = 1
  30. if req.Ctype != nil && *req.Ctype > 0 {
  31. ctype = *req.Ctype
  32. }
  33. predicates = append(predicates, wx.Ctype(ctype))
  34. if !isAdmin {
  35. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  36. } else {
  37. if req.OrganizationId != nil && *req.OrganizationId != 0 {
  38. predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
  39. }
  40. }
  41. data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Order(ent.Desc(wx.FieldOrganizationID)).All(l.ctx)
  42. if err != nil {
  43. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  44. }
  45. resp = &types.WxSelectListResp{}
  46. resp.Msg = errormsg.Success
  47. for _, v := range data {
  48. wxid := v.Wxid
  49. if !strings.HasPrefix(wxid, "temp-") {
  50. resp.Data = append(resp.Data,
  51. types.WxSelectListInfo{
  52. BaseIDInfo: types.BaseIDInfo{
  53. Id: &v.ID,
  54. },
  55. Wxid: &wxid,
  56. Nickname: &v.Nickname,
  57. Ctype: &v.Ctype,
  58. })
  59. }
  60. }
  61. return resp, nil
  62. }