import_whatsapp_contact_logic.go 3.6 KB

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