update_contact_logic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package contact
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/contact"
  6. "wechat-api/ent/contactfield"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type UpdateContactLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewUpdateContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateContactLogic {
  19. return &UpdateContactLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *UpdateContactLogic) UpdateContact(req *types.ContactInfo) (*types.BaseMsgResp, error) {
  26. tx, err := l.svcCtx.DB.Tx(l.ctx)
  27. if err != nil {
  28. return nil, err
  29. }
  30. organizationId := l.ctx.Value("organizationId").(uint64)
  31. err = tx.Contact.UpdateOneID(*req.Id).
  32. Where(contact.OrganizationID(organizationId)).
  33. SetNotNilStatus(req.Status).
  34. SetNotNilWxWxid(req.WxWxid).
  35. SetNotNilType(req.Type).
  36. SetNotNilWxid(req.Wxid).
  37. SetNotNilAccount(req.Account).
  38. SetNotNilNickname(req.Nickname).
  39. SetNotNilMarkname(req.Markname).
  40. SetNotNilHeadimg(req.Headimg).
  41. SetNotNilSex(req.Sex).
  42. SetNotNilStarrole(req.Starrole).
  43. SetNotNilDontseeit(req.Dontseeit).
  44. SetNotNilDontseeme(req.Dontseeme).
  45. SetNotNilLag(req.Lag).
  46. SetNotNilGid(req.Gid).
  47. SetNotNilGname(req.Gname).
  48. SetNotNilV3(req.V3).
  49. SetNotNilCname(req.Cname).
  50. SetNotNilCarea(req.Carea).
  51. SetNotNilCage(req.Cage).
  52. SetNotNilCbirthday(req.Cbirthday).
  53. SetNotNilCbirtharea(req.Cbirtharea).
  54. SetNotNilCc(req.Cc).
  55. SetNotNilPhone(req.Phone).
  56. SetNotNilCidcardNo(req.CidcardNo).
  57. SetNotNilCtitle(req.Ctitle).
  58. Exec(l.ctx)
  59. if err != nil {
  60. _ = tx.Rollback()
  61. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  62. }
  63. err = UpdateContactFields(l.ctx, tx, *req.Id, req.CustomFields)
  64. if err != nil {
  65. _ = tx.Rollback()
  66. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  67. }
  68. _ = tx.Commit()
  69. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  70. }
  71. func UpdateContactFields(ctx context.Context, tx *ent.Tx, contact_id uint64, fields []types.ContactFieldTemplate) error {
  72. for _, field := range fields {
  73. f, _ := tx.ContactField.Query().Where(contactfield.ContactID(contact_id), contactfield.FormID(*field.Id)).First(ctx)
  74. logx.Debug("f: ", f)
  75. if f == nil {
  76. if field.Label != nil && len(field.Value) > 0 {
  77. _, err := tx.ContactField.Create().
  78. SetContactID(contact_id).
  79. SetFormID(*field.Id).
  80. SetValue(field.Value).
  81. Save(ctx)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. } else {
  87. if field.Label != nil {
  88. if len(field.Value) == 0 {
  89. field.Value = []string{""}
  90. }
  91. _, err := tx.ContactField.UpdateOneID(f.ID).
  92. SetValue(field.Value).
  93. Save(ctx)
  94. if err != nil {
  95. return err
  96. }
  97. }
  98. }
  99. }
  100. return nil
  101. }