get_contact_by_id_logic.go 3.6 KB

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