get_contact_by_id_logic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Wxid(data.WxWxid),
  43. ).
  44. Only(l.ctx)
  45. if err != nil {
  46. if ent.IsNotFound(err) {
  47. return nil
  48. }
  49. return err
  50. }
  51. var custom_field []types.ContactFieldTemplate
  52. field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query().
  53. Where(
  54. contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId
  55. ).
  56. Only(l.ctx)
  57. if field_template != nil && len(field_template.Template) > 0 {
  58. field_set := make(map[string][]string)
  59. fields := data.Edges.ContactFields
  60. for _, field := range fields {
  61. field_set[field.FormID] = field.Value
  62. }
  63. for _, template := range field_template.Template {
  64. template.Value = field_set[*template.Id]
  65. custom_field = append(custom_field, ConvertCustomToInternal(template))
  66. }
  67. }
  68. return &types.ContactInfoResp{
  69. BaseDataInfo: types.BaseDataInfo{
  70. Code: 0,
  71. Msg: errormsg.Success,
  72. },
  73. Data: types.ContactInfo{
  74. BaseIDInfo: types.BaseIDInfo{
  75. Id: &data.ID,
  76. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  77. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  78. },
  79. Status: &data.Status,
  80. WxWxid: &data.WxWxid,
  81. Type: &data.Type,
  82. Wxid: &data.Wxid,
  83. Account: &data.Account,
  84. Nickname: &data.Nickname,
  85. Markname: &data.Markname,
  86. Headimg: &data.Headimg,
  87. Sex: &data.Sex,
  88. Starrole: &data.Starrole,
  89. Dontseeit: &data.Dontseeit,
  90. Dontseeme: &data.Dontseeme,
  91. Lag: &data.Lag,
  92. Gid: &data.Gid,
  93. Gname: &data.Gname,
  94. V3: &data.V3,
  95. Ctype: &data.Ctype,
  96. Cname: &data.Cname,
  97. Cage: &data.Cage,
  98. Carea: &data.Carea,
  99. Cc: &data.Cc,
  100. Phone: &data.Phone,
  101. Cbirthday: &data.Cbirthday,
  102. Cbirtharea: &data.Cbirtharea,
  103. CidcardNo: &data.CidcardNo,
  104. Ctitle: &data.Ctitle,
  105. CustomFields: custom_field,
  106. },
  107. }, nil
  108. }
  109. func ConvertCustomToInternal(src custom_types.ContactFieldTemplate) types.ContactFieldTemplate {
  110. var result types.ContactFieldTemplate
  111. var options []types.ContactFieldTemplateOptions
  112. for _, o := range src.Options {
  113. options = append(options, types.ContactFieldTemplateOptions{
  114. Label: o.Label,
  115. Value: o.Value,
  116. })
  117. }
  118. result = types.ContactFieldTemplate{
  119. Type: src.Type,
  120. Id: src.Id,
  121. Label: src.Label,
  122. Options: options,
  123. Value: src.Value,
  124. }
  125. return result
  126. }