package chat import ( "context" "errors" "wechat-api/ent" "wechat-api/ent/predicate" "wechat-api/ent/wx" "wechat-api/internal/service/addfriend" "wechat-api/internal/svc" "wechat-api/internal/types" "wechat-api/internal/utils/typekit" "github.com/suyuan32/simple-admin-common/enum/errorcode" "github.com/suyuan32/simple-admin-common/msg/errormsg" "github.com/zeromicro/go-zero/core/logx" ) type AddFriendByPhoneLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewAddFriendByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendByPhoneLogic { return &AddFriendByPhoneLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } func GetRandWxid(idList []string) string { id := "" if len(idList) > 0 { typekit.ShuffleList(idList) id = idList[0] } return id } func (l *AddFriendByPhoneLogic) getWxInfoList(preds ...predicate.Wx) ([]*ent.Wx, error) { preds = append(preds, wx.StatusEQ(1)) return l.svcCtx.DB.Wx.Query().Where(preds...).All(l.ctx) } func (l *AddFriendByPhoneLogic) getWxIdListByOid(Oid uint64, Type uint64) ([]string, error) { preds := []predicate.Wx{} preds = append(preds, wx.OrganizationIDEQ(Oid)) if Type == 1 || Type == 3 { preds = append(preds, wx.CtypeEQ(Type)) } wxIdList := []string{} wxInfoList, err := l.getWxInfoList(preds...) if err == nil { for _, wxInfo := range wxInfoList { wxIdList = append(wxIdList, wxInfo.Wxid) } } return wxIdList, err } type AddFriendByPhoneInfo struct { Type int `json:"type"` WeChatId string `json:"wechat_id"` Phone string `json:"phone"` Message string `json:"message"` CallbackURL string `json:"callback_url"` } func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFriendByPhoneReq, Oid uint64) ([]AddFriendByPhoneInfo, error) { var err error _, req.WeChatIds, req.Phones, err = l.alignWxidsAndPhones(Oid, uint64(req.Type), req.WeChatIds, req.Phones) if err != nil { return nil, err } infos := []AddFriendByPhoneInfo{} for idx, phone := range req.Phones { info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: req.WeChatIds[idx], Phone: phone, Message: req.Message, CallbackURL: req.CallbackURL} infos = append(infos, info) } return infos, nil } func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64, weChatIds []string, phones []string) (bool, []string, []string, error) { lenWechatIds := len(weChatIds) lenPhones := len(phones) //case1: 输入的wxids和phones大小一样,不需要对齐 if lenWechatIds == lenPhones { return true, weChatIds, phones, nil } //case2: 输入的wxids比phones小或者空 if lenWechatIds < lenPhones { allWxIds, err := l.getWxIdListByOid(Oid, CType) if err != nil { return false, weChatIds, phones, err } if len(allWxIds) == 0 { if len(weChatIds) == 0 { err = errors.New("db wxIds empty") } return false, weChatIds, phones, err } for i := 0; i < lenPhones-lenWechatIds; i++ { weChatIds = append(weChatIds, GetRandWxid(allWxIds)) } } else { //case 3: 输入的wxids比phones大 /* for i := 0; i < lenWechatIds-lenPhones; i++ { phones = append(phones, GetRandWxid(phones)) } */ } return true, weChatIds, phones, nil } func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) { // todo: add your logic here and delete this line if len(req.Phones) == 0 { return nil, errors.New("miss phone") } orgID, ok := l.ctx.Value("organizationId").(uint64) if !ok || orgID == 0 { return nil, errors.New("content get oid info err") } //fmt.Println("before weChatIds:", req.WeChatIds) //fmt.Println("before phones:", req.Phones) //apiKeyObj.OrganizationID = 1 if req.Type != 3 { req.Type = 1 } infos, err := l.rebuildAddFriendByPhoneInfos(req, orgID) if err != nil { return nil, err } //fmt.Println("after weChatIds:", req.WeChatIds) //fmt.Println("after phones:", req.Phones) //fmt.Println(typekit.PrettyPrint(infos)) var id int64 for _, info := range infos { id, err = l.AppendWechaFriendAddInfo(&info) if err != nil { logx.Errorf("chat.AppendWechaFriendAddLog err : %s", err) return nil, err } logx.Infof("chat.AppendWechaFriendAddLog succ,get id:%d", id) if req.Type != 1 { continue //企微忽略后面 } ret := addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx). FindFriendByContent(info.WeChatId, info.Phone) retStr := "fail" if ret { retStr = "succ" } logx.Infof("call addfriend.NewAddWechatFriendService.FindFriendByContent('%s','%s') maybe %s", info.WeChatId, info.Phone, retStr) } return &types.BaseMsgResp{Msg: errormsg.Success, Code: errorcode.OK}, nil } func (l *AddFriendByPhoneLogic) AppendWechaFriendAddInfo(addInfo *AddFriendByPhoneInfo) (int64, error) { isCanAdd := 0 if addInfo.Type != 1 { isCanAdd = 1 } res, err := l.svcCtx.DB.AddWechatFriendLog.Create(). SetNotNilOwnerWxID(&addInfo.WeChatId). SetNotNilOwnerWxType(&addInfo.Type). SetNotNilFindContent(&addInfo.Phone). SetNotNilMessage(&addInfo.Message). SetIsCanAdd(isCanAdd). Save(l.ctx) id := int64(0) if err == nil { id = res.ID } return id, err }