package contact import ( "context" "wechat-api/ent" "wechat-api/ent/contact" "wechat-api/ent/contactfieldtemplate" "wechat-api/ent/custom_types" "wechat-api/internal/svc" "wechat-api/internal/types" "wechat-api/internal/utils/dberrorhandler" "github.com/suyuan32/simple-admin-common/msg/errormsg" "github.com/suyuan32/simple-admin-common/utils/pointy" "github.com/zeromicro/go-zero/core/logx" ) type GetContactByIdLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetContactByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactByIdLogic { return &GetContactByIdLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *GetContactByIdLogic) GetContactById(req *types.IDReq) (*types.ContactInfoResp, error) { organizationId := l.ctx.Value("organizationId").(uint64) data, err := l.svcCtx.DB.Contact.Query(). Where( contact.IDEQ(req.Id), // Filter by ID contact.OrganizationID(organizationId), // Additional filter by organizationId ).WithContactFields(func(query *ent.ContactFieldQuery) { query.WithFieldContact() }). Only(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } var custom_field []types.ContactFieldTemplate field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query(). Where( contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId ). Only(l.ctx) if field_template != nil && len(field_template.Template) > 0 { field_set := make(map[string][]string) fields := data.Edges.ContactFields for _, field := range fields { field_set[field.FormID] = field.Value } for _, template := range field_template.Template { template.Value = field_set[*template.Id] custom_field = append(custom_field, ConvertCustomToInternal(template)) } } return &types.ContactInfoResp{ BaseDataInfo: types.BaseDataInfo{ Code: 0, Msg: errormsg.Success, }, Data: types.ContactInfo{ BaseIDInfo: types.BaseIDInfo{ Id: &data.ID, CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()), UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()), }, Status: &data.Status, WxWxid: &data.WxWxid, Type: &data.Type, Wxid: &data.Wxid, Account: &data.Account, Nickname: &data.Nickname, Markname: &data.Markname, Headimg: &data.Headimg, Sex: &data.Sex, Starrole: &data.Starrole, Dontseeit: &data.Dontseeit, Dontseeme: &data.Dontseeme, Lag: &data.Lag, Gid: &data.Gid, Gname: &data.Gname, V3: &data.V3, Ctype: &data.Ctype, Cname: &data.Cname, Cage: &data.Cage, Carea: &data.Carea, Cc: &data.Cc, Phone: &data.Phone, Cbirthday: &data.Cbirthday, Cbirtharea: &data.Cbirtharea, CidcardNo: &data.CidcardNo, Ctitle: &data.Ctitle, CustomFields: custom_field, }, }, nil } func ConvertCustomToInternal(src custom_types.ContactFieldTemplate) types.ContactFieldTemplate { var result types.ContactFieldTemplate var options []types.ContactFieldTemplateOptions for _, o := range src.Options { options = append(options, types.ContactFieldTemplateOptions{ Label: o.Label, Value: o.Value, }) } result = types.ContactFieldTemplate{ Type: src.Type, Id: src.Id, Label: src.Label, Options: options, Value: src.Value, } return result }