get_contact_by_id_logic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package contact
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/contact"
  6. "wechat-api/ent/contactfieldtemplate"
  7. "wechat-api/ent/custom_types"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  12. "github.com/suyuan32/simple-admin-common/utils/pointy"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetContactByIdLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewGetContactByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactByIdLogic {
  21. return &GetContactByIdLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. func (l *GetContactByIdLogic) GetContactById(req *types.IDReq) (*types.ContactInfoResp, error) {
  28. organizationId := l.ctx.Value("organizationId").(uint64)
  29. data, err := l.svcCtx.DB.Contact.Query().
  30. Where(
  31. contact.IDEQ(req.Id), // Filter by ID
  32. contact.OrganizationID(organizationId), // Additional filter by organizationId
  33. ).WithContactFields(func(query *ent.ContactFieldQuery) {
  34. query.WithFieldContact()
  35. }).
  36. Only(l.ctx)
  37. if err != nil {
  38. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  39. }
  40. var custom_field []types.ContactFieldTemplate
  41. field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query().
  42. Where(
  43. contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId
  44. ).
  45. Only(l.ctx)
  46. if field_template != nil && len(field_template.Template) > 0 {
  47. field_set := make(map[string][]string)
  48. fields := data.Edges.ContactFields
  49. for _, field := range fields {
  50. field_set[field.FormID] = field.Value
  51. }
  52. for _, template := range field_template.Template {
  53. template.Value = field_set[*template.Id]
  54. custom_field = append(custom_field, ConvertCustomToInternal(template))
  55. }
  56. }
  57. return &types.ContactInfoResp{
  58. BaseDataInfo: types.BaseDataInfo{
  59. Code: 0,
  60. Msg: errormsg.Success,
  61. },
  62. Data: types.ContactInfo{
  63. BaseIDInfo: types.BaseIDInfo{
  64. Id: &data.ID,
  65. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  66. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  67. },
  68. Status: &data.Status,
  69. WxWxid: &data.WxWxid,
  70. Type: &data.Type,
  71. Wxid: &data.Wxid,
  72. Account: &data.Account,
  73. Nickname: &data.Nickname,
  74. Markname: &data.Markname,
  75. Headimg: &data.Headimg,
  76. Sex: &data.Sex,
  77. Starrole: &data.Starrole,
  78. Dontseeit: &data.Dontseeit,
  79. Dontseeme: &data.Dontseeme,
  80. Lag: &data.Lag,
  81. Gid: &data.Gid,
  82. Gname: &data.Gname,
  83. V3: &data.V3,
  84. Ctype: &data.Ctype,
  85. Cname: &data.Cname,
  86. Cage: &data.Cage,
  87. Carea: &data.Carea,
  88. Cc: &data.Cc,
  89. Phone: &data.Phone,
  90. Cbirthday: &data.Cbirthday,
  91. Cbirtharea: &data.Cbirtharea,
  92. CidcardNo: &data.CidcardNo,
  93. Ctitle: &data.Ctitle,
  94. CustomFields: custom_field,
  95. },
  96. }, nil
  97. }
  98. func ConvertCustomToInternal(src custom_types.ContactFieldTemplate) types.ContactFieldTemplate {
  99. var result types.ContactFieldTemplate
  100. var options []types.ContactFieldTemplateOptions
  101. for _, o := range src.Options {
  102. options = append(options, types.ContactFieldTemplateOptions{
  103. Label: o.Label,
  104. Value: o.Value,
  105. })
  106. }
  107. result = types.ContactFieldTemplate{
  108. Type: src.Type,
  109. Id: src.Id,
  110. Label: src.Label,
  111. Options: options,
  112. Value: src.Value,
  113. }
  114. return result
  115. }