update_contact_logic.go 2.8 KB

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