import_whatsapp_contact_logic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "time"
  16. "wechat-api/ent"
  17. "wechat-api/ent/contact"
  18. "wechat-api/ent/label"
  19. "wechat-api/internal/svc"
  20. "wechat-api/internal/types"
  21. "github.com/zeromicro/go-zero/core/logx"
  22. )
  23. type ImportWhatsappContactLogic struct {
  24. logx.Logger
  25. ctx context.Context
  26. svcCtx *svc.ServiceContext
  27. }
  28. func NewImportWhatsappContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImportWhatsappContactLogic {
  29. return &ImportWhatsappContactLogic{
  30. Logger: logx.WithContext(ctx),
  31. ctx: ctx,
  32. svcCtx: svcCtx}
  33. }
  34. func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhatsappContactReq, file multipart.File) (*types.BaseDataInfo, error) {
  35. organizationId := l.ctx.Value("organizationId").(uint64)
  36. resp := types.BaseDataInfo{}
  37. reader := csv.NewReader(file)
  38. records, err := reader.ReadAll()
  39. if err != nil {
  40. return nil, err
  41. }
  42. var total, success, fail, add_labels int
  43. var conditions = "{}"
  44. for idx, record := range records {
  45. l.Logger.Infof("record=%v\n", record)
  46. if idx == 0 {
  47. continue
  48. }
  49. total++
  50. _, err := l.svcCtx.DB.Contact.Query().Where(
  51. contact.OrganizationID(organizationId),
  52. contact.Ctype(2),
  53. contact.Cc(trim(record[0])),
  54. contact.Phone(trim(record[1])),
  55. ).First(l.ctx)
  56. if err == nil || !ent.IsNotFound(err) {
  57. fail++
  58. continue
  59. }
  60. uuidstr := uuidx.NewUUID().String()
  61. sex, _ := strconv.Atoi(trim(record[5]))
  62. cage, _ := strconv.Atoi(trim(record[7]))
  63. var cbirthday string
  64. t, err := time.Parse("2006/01/02", trim(record[8]))
  65. if err == nil {
  66. cbirthday = t.Format("2006-01-02")
  67. }
  68. newContact, err := l.svcCtx.DB.Contact.Create().SetCtype(2).
  69. SetCc(trim(record[0])).
  70. SetPhone(trim(record[1])).
  71. SetType(5).
  72. SetWxWxid(uuidstr).
  73. SetWxid(uuidstr).
  74. SetCname(transCharset(record[2])).
  75. SetMarkname(transCharset(record[3])).
  76. SetSex(sex).
  77. SetCage(cage).
  78. SetCtitle(transCharset(record[6])).
  79. SetCarea(transCharset(record[9])).
  80. SetCbirthday(cbirthday).
  81. SetCbirtharea(transCharset(record[10])).
  82. SetCidcardNo(trim(record[11])).
  83. SetOrganizationID(organizationId).
  84. Save(l.ctx)
  85. if err != nil {
  86. l.Logger.Errorf("insert to whatsapp contact failed. err=%v\n", err)
  87. fail++
  88. continue
  89. } else {
  90. success++
  91. }
  92. labels := trim(record[4])
  93. if labels != "" {
  94. for _, value := range strings.Split(labels, "+") {
  95. _, err = l.svcCtx.DB.Label.Query().Where(
  96. label.OrganizationID(organizationId),
  97. label.Name(value),
  98. ).First(l.ctx)
  99. if err == nil || !ent.IsNotFound(err) {
  100. continue
  101. }
  102. newLabel, err := l.svcCtx.DB.Label.Create().
  103. SetName(value).
  104. SetType(1).
  105. SetNotNilConditions(&conditions).
  106. SetOrganizationID(organizationId).
  107. Save(l.ctx)
  108. if err != nil {
  109. l.Logger.Errorf("insert into whatsapp label failed.err=%v\n", err)
  110. }
  111. _, err = l.svcCtx.DB.LabelRelationship.Create().
  112. SetLabelID(newLabel.ID).
  113. SetContactID(newContact.ID).
  114. SetOrganizationID(organizationId).
  115. Save(l.ctx)
  116. if err != nil {
  117. l.Logger.Errorf("insert into whatsapp label-relationship failed.err=%v\n", err)
  118. } else {
  119. add_labels++
  120. }
  121. }
  122. }
  123. }
  124. resp.Code = 0
  125. resp.Msg = errormsg.Success
  126. resp.Data = fmt.Sprintf("upload finished. tota:%d success:%d failed:%d add_labels:%d\n", total, success, fail, add_labels)
  127. return &resp, nil
  128. }
  129. func trim(s string) string {
  130. return strings.Trim(s, " \r\n\t")
  131. }
  132. func transCharset(s string) string {
  133. s = trim(s)
  134. return s
  135. rd := transform.NewReader(bytes.NewReader([]byte(s)), simplifiedchinese.GBK.NewDecoder())
  136. bytes, err := io.ReadAll(rd)
  137. fmt.Printf("bytes=%s err=%v\n", bytes, err)
  138. return string(bytes)
  139. }