update_contact_logic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. cage := 0
  32. if req.Cage != nil && *req.Cage > 0 {
  33. cage = *req.Cage
  34. }
  35. err = tx.Contact.UpdateOneID(*req.Id).
  36. Where(contact.OrganizationID(organizationId)).
  37. SetNotNilStatus(req.Status).
  38. SetNotNilWxWxid(req.WxWxid).
  39. SetNotNilType(req.Type).
  40. SetNotNilWxid(req.Wxid).
  41. SetNotNilAccount(req.Account).
  42. SetNotNilNickname(req.Nickname).
  43. SetNotNilMarkname(req.Markname).
  44. SetNotNilHeadimg(req.Headimg).
  45. SetNotNilSex(req.Sex).
  46. SetNotNilStarrole(req.Starrole).
  47. SetNotNilDontseeit(req.Dontseeit).
  48. SetNotNilDontseeme(req.Dontseeme).
  49. SetNotNilLag(req.Lag).
  50. SetNotNilGid(req.Gid).
  51. SetNotNilGname(req.Gname).
  52. SetNotNilV3(req.V3).
  53. SetNotNilCname(req.Cname).
  54. SetNotNilCarea(req.Carea).
  55. SetNotNilCage(&cage).
  56. SetNotNilCbirthday(req.Cbirthday).
  57. SetNotNilCbirtharea(req.Cbirtharea).
  58. SetNotNilCc(req.Cc).
  59. SetNotNilPhone(req.Phone).
  60. SetNotNilCidcardNo(req.CidcardNo).
  61. SetNotNilCtitle(req.Ctitle).
  62. Exec(l.ctx)
  63. if err != nil {
  64. _ = tx.Rollback()
  65. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  66. }
  67. err = UpdateContactFields(l.ctx, tx, *req.Id, req.CustomFields)
  68. if err != nil {
  69. _ = tx.Rollback()
  70. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  71. }
  72. _ = tx.Commit()
  73. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  74. }
  75. func UpdateContactFields(ctx context.Context, tx *ent.Tx, contact_id uint64, fields []types.ContactFieldTemplate) error {
  76. for _, field := range fields {
  77. f, _ := tx.ContactField.Query().Where(contactfield.ContactID(contact_id), contactfield.FormID(*field.Id)).First(ctx)
  78. if f == nil {
  79. if field.Value != nil && len(field.Value) > 0 {
  80. _, err := tx.ContactField.Create().
  81. SetContactID(contact_id).
  82. SetFormID(*field.Id).
  83. SetValue(field.Value).
  84. Save(ctx)
  85. if err != nil {
  86. return err
  87. }
  88. }
  89. } else {
  90. if field.Value != nil {
  91. if len(field.Value) == 0 {
  92. field.Value = []string{""}
  93. }
  94. } else {
  95. field.Value = []string{""}
  96. }
  97. _, err := tx.ContactField.UpdateOneID(f.ID).
  98. SetValue(field.Value).
  99. Save(ctx)
  100. if err != nil {
  101. return err
  102. }
  103. }
  104. }
  105. return nil
  106. }