123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package contact
- import (
- "context"
- "encoding/csv"
- "fmt"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/suyuan32/simple-admin-common/utils/uuidx"
- "mime/multipart"
- "strconv"
- "strings"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type ImportWhatsappContactLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewImportWhatsappContactLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImportWhatsappContactLogic {
- return &ImportWhatsappContactLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *ImportWhatsappContactLogic) ImportWhatsappContact(req *types.ImportWhatsappContactReq, file multipart.File) (*types.BaseDataInfo, error) {
- organizationId := l.ctx.Value("organizationId").(uint64)
- resp := types.BaseDataInfo{}
- reader := csv.NewReader(file)
- records, err := reader.ReadAll()
- if err != nil {
- return nil, err
- }
- var total, success, fail, add_labels int
- var conditions = "{}"
- for idx, record := range records {
- if idx == 0 {
- continue
- }
- total++
- uuidstr := uuidx.NewUUID().String()
- sex, _ := strconv.Atoi(trim(record[5]))
- cage, _ := strconv.Atoi(trim(record[7]))
- newContact, err := l.svcCtx.DB.Contact.Create().SetCtype(2).
- SetCc(record[0]).
- SetPhone(record[1]).
- SetType(5).
- SetWxWxid(uuidstr).
- SetWxid(uuidstr).
- SetCname(trim(record[2])).
- SetMarkname(trim(record[3])).
- SetSex(sex).
- SetCage(cage).
- SetCtitle(trim(record[6])).
- SetCarea(trim(record[9])).
- SetCbirthday(trim(record[8])).
- SetCbirtharea(trim(record[10])).
- SetCidcardNo(trim(record[11])).
- SetOrganizationID(organizationId).
- Save(l.ctx)
- if err != nil {
- l.Logger.Error("insert to whatsapp contact failed. err=%v\n", err)
- fail++
- } else {
- success++
- }
- labels := trim(record[4])
- if labels != "" {
- for _, value := range strings.Split(labels, "+") {
- newLabel, err := l.svcCtx.DB.Label.Create().SetCtype(2).
- SetName(value).
- SetType(5).
- SetNotNilConditions(&conditions).
- SetOrganizationID(organizationId).
- Save(l.ctx)
- if err != nil {
- l.Logger.Errorf("insert into whatsapp label failed.err=%v\n", err)
- }
- _, err = l.svcCtx.DB.LabelRelationship.Create().
- SetLabelID(newLabel.ID).
- SetContactID(newContact.ID).
- SetOrganizationID(organizationId).
- Save(l.ctx)
- if err != nil {
- l.Logger.Errorf("insert into whatsapp label-relationship failed.err=%v\n", err)
- } else {
- add_labels++
- }
- }
- }
- }
- resp.Code = 0
- resp.Msg = errormsg.Success
- resp.Data = fmt.Sprintf("upload finished. tota:%d success:%d failed:%d add_labels:%d\n", total, success, fail, add_labels)
- return &resp, nil
- }
- func trim(s string) string {
- return strings.Trim(s, " \r\n\t")
- }
|