import_whatsapp_contact_logic.go 3.1 KB

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