private string ImportToTag()
        {
            Dictionary <string, Stream>    dic  = new Dictionary <string, Stream>();
            Dictionary <string, TagImport> dics = new Dictionary <string, TagImport>();
            //获取accesstoken
            string accessToken = GetToken();
            string invalidList = string.Empty;

            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase objFile = Request.Files[i];
                var f = new byte[objFile.InputStream.Length];
                //将csv文件一次性读出来
                objFile.InputStream.Read(f, 0, (int)objFile.InputStream.Length);
                //定义数据格式UTF-8
                string str = System.Text.Encoding.GetEncoding("UTF-8").GetString(f);
                //获取第一行数据然后取两列数据WeChatUserID,TagID
                string[] sArray = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                string[] rowOne = sArray[0].Split(',');
                int      TagidIndex = 0, WeChatUserIDIndex = 0;
                //循环判断返回数组对应列下标
                for (int s = 0; s < rowOne.Length; s++)
                {
                    if (string.Equals(rowOne[s], "TagID", StringComparison.OrdinalIgnoreCase))
                    {
                        TagidIndex = s;
                    }
                    else if (string.Equals(rowOne[s], "WeChatUserID", StringComparison.OrdinalIgnoreCase))
                    {
                        WeChatUserIDIndex = s;
                    }
                }

                //循环其他行去把对应下标的数据封装成TagImport对象
                for (int x = 1; x < sArray.Length; x++)
                {
                    string[]  rowX         = sArray[x].Split(',');
                    string    tagid        = rowX[TagidIndex];
                    string    WeChatUserID = rowX[WeChatUserIDIndex];
                    TagImport tagimport    = null;
                    //确认dics是否有tagid 有add,没有就新建对象
                    if (dics.ContainsKey(tagid))
                    {
                        tagimport = dics[tagid];
                        tagimport.userlist.Add(WeChatUserID);
                    }
                    else
                    {
                        tagimport          = new TagImport();
                        tagimport.tagid    = tagid;
                        tagimport.userlist = new List <string>();
                        tagimport.userlist.Add(WeChatUserID);
                        tagimport.partylist = new List <int>();
                        dics.Add(tagid, tagimport);
                    }
                }

                //返回invalidlist
                AddTagMemberResult addResult = new AddTagMemberResult();

                //先根据tagid去取当前tagid的userlist再删除掉成员
                foreach (var b in dics.Keys)
                {
                    var lstTagUser = MailListApi.GetTagMember(accessToken, int.Parse(b)).userlist;
                    if (lstTagUser == null || lstTagUser.Count <= 0)
                    {
                        break;
                    }

                    List <string> userList = new List <string>();
                    lstTagUser.ForEach(a =>
                    {
                        userList.Add(a.userid);
                    });
                    MailListApi.DelTagMember(accessToken, int.Parse(b), userList.ToArray(), null);
                }

                //封装完后循环dics,序列化后调用API的接口Post过去
                foreach (var a in dics)
                {
                    var           strs = string.Join(",", a.Value.userlist.ToArray());
                    List <string> lst  = new List <string>();
                    GetStr(strs, lst);

                    foreach (var b in lst)
                    {
                        addResult = MailListApi.AddTagMember(
                            accessToken,
                            int.Parse(a.Key),
                            b.Split(',')
                            );
                        if (!string.IsNullOrEmpty(addResult.invalidlist))
                        {
                            invalidList = invalidList + '|' + addResult.invalidlist;
                        }
                    }
                }
                dic.Add(objFile.FileName, objFile.InputStream);
            }

            return(invalidList);
        }
Ejemplo n.º 2
0
        private string ImportToTag()
        {
            var dic  = new Dictionary <string, Stream>();
            var dics = new Dictionary <string, TagImport>();
            //获取accesstoken
            string accessToken = GetToken();
            string invalidList = string.Empty;

            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase objFile = Request.Files[i];
                if (objFile != null)
                {
                    var f = new byte[objFile.InputStream.Length];
                    //将csv文件一次性读出来
                    objFile.InputStream.Read(f, 0, (int)objFile.InputStream.Length);
                    //定义数据格式UTF-8
                    string str = System.Text.Encoding.GetEncoding("UTF-8").GetString(f);
                    //获取第一行数据然后取两列数据lillyID,TagID
                    string[] sArray = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] rowOne = sArray[0].Split(',');
                    int      tagidIndex = 0, lillyIdIndex = 0;
                    //循环判断返回数组对应列下标
                    for (int s = 0; s < rowOne.Length; s++)
                    {
                        if (string.Equals(rowOne[s], "TagID", StringComparison.OrdinalIgnoreCase))
                        {
                            tagidIndex = s;
                        }
                        else if (string.Equals(rowOne[s], "LillyID", StringComparison.OrdinalIgnoreCase))
                        {
                            lillyIdIndex = s;
                        }
                    }

                    //循环其他行去把对应下标的数据封装成TagImport对象
                    for (int x = 1; x < sArray.Length; x++)
                    {
                        string[]  rowX      = sArray[x].Split(',');
                        string    tagid     = rowX[tagidIndex];
                        string    lillyid   = rowX[lillyIdIndex] == null ? rowX[lillyIdIndex] : rowX[lillyIdIndex].Trim();
                        TagImport tagimport = null;
                        //确认dics是否有tagid 有add,没有就新建对象
                        if (dics.ContainsKey(tagid))
                        {
                            tagimport = dics[tagid];
                            tagimport.userlist.Add(lillyid);
                        }
                        else
                        {
                            tagimport = new TagImport
                            {
                                tagid    = tagid,
                                userlist = new List <string> {
                                    lillyid
                                },
                                partylist = new List <int>()
                            };
                            dics.Add(tagid, tagimport);
                        }
                    }
                }

                //返回invalidlist
                var addResult = new AddTagMemberResult();

                //先根据tagid去取当前tagid的userlist再删除掉成员
                foreach (var b in dics.Keys)
                {
                    var lstTagUser = MailListApi.GetTagMember(accessToken, int.Parse(b)).userlist;
                    if (lstTagUser == null || lstTagUser.Count <= 0)
                    {
                        break;
                    }

                    var userList = new List <string>();
                    lstTagUser.ForEach(a => userList.Add(a.userid));
                    MailListApi.DelTagMember(accessToken, int.Parse(b), userList.ToArray());
                }

                //封装完后循环dics,序列化后调用API的接口Post过去
                foreach (var a in dics)
                {
                    var strs = string.Join(",", a.Value.userlist.ToArray());
                    var lst  = new List <string>();
                    GetStr(strs, lst);

                    foreach (var b in lst)
                    {
                        try
                        {
                            addResult = MailListApi.AddTagMember(
                                accessToken,
                                int.Parse(a.Key),
                                b.Split(',')
                                );
                        }
                        catch (ErrorJsonResultException e)
                        {
                            //WeChatCommonService.lstUser.
                            var invalidUsers = b.Split(',').Distinct().Where(x => WeChatCommonService.lstUser.All(y => y.userid != x)).ToList();
                            invalidUsers.Add(invalidList);
                            invalidList = string.Join("|", invalidUsers);
                        }

                        if (!string.IsNullOrEmpty(addResult.invalidlist))
                        {
                            invalidList = invalidList + '|' + addResult.invalidlist;
                        }
                    }
                }
                if (objFile != null)
                {
                    dic.Add(objFile.FileName, objFile.InputStream);
                }
            }

            if (invalidList.StartsWith("|"))
            {
                invalidList.Remove(0, 1);
            }
            if (invalidList.EndsWith("|"))
            {
                invalidList.Remove(invalidList.Length - 1, 1);
            }
            return(invalidList);
        }