import_whatsapp_contact_logic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package contact
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "fmt"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "github.com/suyuan32/simple-admin-common/utils/uuidx"
  8. "mime/multipart"
  9. "strconv"
  10. "strings"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type ImportWhatsappContactLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewImportWhatsappContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImportWhatsappContactLogic {
  21. return &ImportWhatsappContactLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhatsappContactReq, file multipart.File) (*types.BaseDataInfo, error) {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. resp := types.BaseDataInfo{}
  29. reader := csv.NewReader(file)
  30. records, err := reader.ReadAll()
  31. if err != nil {
  32. return nil, err
  33. }
  34. var total, success, fail, add_labels int
  35. var conditions = "{}"
  36. for idx, record := range records {
  37. if idx == 0 {
  38. continue
  39. }
  40. total++
  41. uuidstr := uuidx.NewUUID().String()
  42. sex, _ := strconv.Atoi(trim(record[5]))
  43. cage, _ := strconv.Atoi(trim(record[7]))
  44. newContact, err := l.svcCtx.DB.Contact.Create().SetCtype(2).
  45. SetCc(record[0]).
  46. SetPhone(record[1]).
  47. SetType(5).
  48. SetWxWxid(uuidstr).
  49. SetWxid(uuidstr).
  50. SetCname(trim(record[2])).
  51. SetMarkname(trim(record[3])).
  52. SetSex(sex).
  53. SetCage(cage).
  54. SetCtitle(trim(record[6])).
  55. SetCarea(trim(record[9])).
  56. SetCbirthday(trim(record[8])).
  57. SetCbirtharea(trim(record[10])).
  58. SetCidcardNo(trim(record[11])).
  59. SetOrganizationID(organizationId).
  60. Save(l.ctx)
  61. if err != nil {
  62. l.Logger.Error("insert to whatsapp contact failed. err=%v\n", err)
  63. fail++
  64. } else {
  65. success++
  66. }
  67. labels := trim(record[4])
  68. if labels != "" {
  69. for _, value := range strings.Split(labels, "+") {
  70. newLabel, err := l.svcCtx.DB.Label.Create().SetCtype(2).
  71. SetName(value).
  72. SetType(5).
  73. SetNotNilConditions(&conditions).
  74. SetOrganizationID(organizationId).
  75. Save(l.ctx)
  76. if err != nil {
  77. l.Logger.Errorf("insert into whatsapp label failed.err=%v\n", err)
  78. }
  79. _, err = l.svcCtx.DB.LabelRelationship.Create().
  80. SetLabelID(newLabel.ID).
  81. SetContactID(newContact.ID).
  82. SetOrganizationID(organizationId).
  83. Save(l.ctx)
  84. if err != nil {
  85. l.Logger.Errorf("insert into whatsapp label-relationship failed.err=%v\n", err)
  86. } else {
  87. add_labels++
  88. }
  89. }
  90. }
  91. }
  92. resp.Code = 0
  93. resp.Msg = errormsg.Success
  94. resp.Data = fmt.Sprintf("upload finished. tota:%d success:%d failed:%d add_labels:%d\n", total, success, fail, add_labels)
  95. return &resp, nil
  96. }
  97. func trim(s string) string {
  98. return strings.Trim(s, " \r\n\t")
  99. }