Ejemplo n.º 1
0
        public async Task <IActionResult> uploadEnterCustom()
        {
            JsonResult <PicMsg> result = new JsonResult <PicMsg>();

            result.code = 1;
            result.msg  = "";
            int      UserID   = Request.Form["UserID"].TryToInt();
            string   UserName = string.Empty;
            UserBll  userBll  = new UserBll();
            UserInfo user     = await userBll.GetByID(UserID);

            if (user != null)
            {
                UserName = user.Name;
            }

            // 获取附带POST参数值
            for (var fileId = 0; fileId < Request.Form.Files.Count; fileId++)
            {
                var curFile = Request.Form.Files[fileId];
                if (curFile.Length < 1)
                {
                    continue;
                }
                var fileExt = Path.GetExtension(curFile.FileName);
                if (String.IsNullOrEmpty(fileExt) || fileExt != ".xlsx")
                {
                    result.msg = "上传文件中包含不支持文件格式!";
                    return(Json(result));
                }
                else
                {
                    // 存储文件名
                    string FileName = DateTime.Now.ToString("yyyyMMddhhmmssff") + CreateRandomCode(8) + fileExt;

                    // 存储路径(绝对路径)
                    string virtualPath = Path.Combine(hostingEnv.WebRootPath, "file");
                    if (!Directory.Exists(virtualPath))
                    {
                        Directory.CreateDirectory(virtualPath);
                    }
                    string filepath = Path.Combine(virtualPath, FileName);
                    try
                    {
                        using (FileStream fs = System.IO.File.Create(filepath))
                        {
                            await curFile.CopyToAsync(fs);

                            fs.Flush();
                        }
                        FileInfo file = new FileInfo(filepath);
                        using (ExcelPackage package = new ExcelPackage(file))
                        {
                            StringBuilder  sb        = new StringBuilder();
                            ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                            int            rowCount  = worksheet.Dimension.Rows;
                            int            ColCount  = worksheet.Dimension.Columns;
                            if (rowCount < 2 || ColCount < 17)
                            {
                                result.msg = "Excel模板不正确";
                                return(Json(result));
                            }
                            StringBuilder            stringBuilder = new StringBuilder();
                            Dictionary <string, int> enteridDIc    = new Dictionary <string, int>();
                            #region 客户信息
                            EnterCustomerBll bll = new EnterCustomerBll();
                            //EnterCustPhaseLogBll logbll = new EnterCustPhaseLogBll();
                            for (int row = 2; row < rowCount; row++)
                            {
                                EnterCustomer enterCustomer = new EnterCustomer();
                                for (int col = 1; col < ColCount; col++)
                                {
                                    string value = worksheet.Cells[row, col].Value.TryToString().Trim();
                                    switch (col)
                                    {
                                    case 1:
                                        enterCustomer.EnterName = value;
                                        break;

                                    case 2:
                                        enterCustomer.Province = value;
                                        break;

                                    case 3:
                                        enterCustomer.City = value;
                                        break;

                                    case 4:
                                        enterCustomer.Telephone = value;
                                        break;

                                    case 5:
                                        enterCustomer.Landline = value;
                                        break;

                                    case 6:
                                        enterCustomer.FaxNumber = value;
                                        break;

                                    case 7:
                                        enterCustomer.ZipCode = value;
                                        break;

                                    case 8:
                                        enterCustomer.Email = value;
                                        break;

                                    case 9:
                                        enterCustomer.WebSit = value;
                                        break;

                                    case 10:
                                        enterCustomer.Address = value;
                                        break;

                                    case 11:
                                        enterCustomer.CustAbstract = value;
                                        break;

                                    case 12:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "代理经销商":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Dealer;
                                            break;

                                        case "普通客户":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Ordinary;
                                            break;

                                        case "集团大客户":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.BigCustomer;
                                            break;

                                        case "业务合作商":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Cooperation;
                                            break;

                                        case "怀疑同行":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Same;
                                            break;

                                        case "其他客户":
                                            enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Other;
                                            break;
                                        }
                                        break;

                                    case 13:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "密切":
                                            enterCustomer.Relationship = Model.Enume.RelationshipEnume.Intimate;
                                            break;

                                        case "较好":
                                            enterCustomer.Relationship = Model.Enume.RelationshipEnume.Better;
                                            break;

                                        case "一般":
                                            enterCustomer.Relationship = Model.Enume.RelationshipEnume.Commonly;
                                            break;

                                        case "较差":
                                            enterCustomer.Relationship = Model.Enume.RelationshipEnume.Poor;
                                            break;
                                        }
                                        break;

                                    case 14:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "高":
                                            enterCustomer.ValueGrade = Model.Enume.ValueGradeEnume.Senior;
                                            break;

                                        case "中":
                                            enterCustomer.ValueGrade = Model.Enume.ValueGradeEnume.Intermediate;
                                            break;

                                        case "低":
                                            enterCustomer.ValueGrade = Model.Enume.ValueGradeEnume.Lower;
                                            break;
                                        }
                                        break;

                                    case 15:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "客户来电":
                                            enterCustomer.Source = Model.Enume.CustSource.CustTelephone;
                                            break;

                                        case "主动挖掘":
                                            enterCustomer.Source = Model.Enume.CustSource.Excavate;
                                            break;

                                        case "网站咨询":
                                            enterCustomer.Source = Model.Enume.CustSource.WebConsulting;
                                            break;

                                        case "客户介绍":
                                            enterCustomer.Source = Model.Enume.CustSource.Introduction;
                                            break;

                                        case "其他来源":
                                            enterCustomer.Source = Model.Enume.CustSource.Other;
                                            break;
                                        }
                                        break;

                                    case 16:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "售前跟踪":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Pre_sale;
                                            break;

                                        case "需求确定":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Demand_Confirmation;
                                            break;

                                        case "售中跟单":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.In_Sales;
                                            break;

                                        case "签约洽谈":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Sign_Contract;
                                            break;

                                        case "成交售后":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.After_Sale;
                                            break;

                                        case "跟单失败":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Invalid;
                                            break;

                                        case "暂且搁置":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Shelve;
                                            break;

                                        case "其他阶段":
                                            enterCustomer.Phase = Model.Enume.PhaseEnume.Other;
                                            break;
                                        }
                                        break;

                                    case 17:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "是":
                                            enterCustomer.IsHeat = true;
                                            break;

                                        case "否":
                                            enterCustomer.IsHeat = false;
                                            break;
                                        }
                                        break;

                                    case 18:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "低热":
                                            enterCustomer.DegreeOfHeat = Model.Enume.DegreeOfHeatEnume.Lower;
                                            break;

                                        case "中热":
                                            enterCustomer.DegreeOfHeat = Model.Enume.DegreeOfHeatEnume.Intermediate;
                                            break;

                                        case "高热":
                                            enterCustomer.DegreeOfHeat = Model.Enume.DegreeOfHeatEnume.Senior;
                                            break;
                                        }
                                        break;

                                    case 19:
                                        if (value.IsNull())
                                        {
                                            break;
                                        }
                                        switch (value)
                                        {
                                        case "高意向客户":
                                            enterCustomer.HeatTYPE = Model.Enume.HeatTypeEnum.Intentional;
                                            break;

                                        case "重点跟踪客户":
                                            enterCustomer.HeatTYPE = Model.Enume.HeatTypeEnum.Key_Account;
                                            break;

                                        case "有望签单客户":
                                            enterCustomer.HeatTYPE = Model.Enume.HeatTypeEnum.Hopeful;
                                            break;
                                        }
                                        break;

                                    case 20:
                                        enterCustomer.CreateTime = value.TryToDateTime();
                                        break;
                                    }
                                }
                                enterCustomer.CreateUserID = UserID;
                                enterCustomer.UpdateTime   = DateTime.Now;

                                enterCustomer.UserID = UserID;

                                bool flag = await bll.ExistsEnterNameAsync(0, enterCustomer.EnterName);

                                if (flag)
                                {
                                    stringBuilder.AppendLine(string.Format("{0}已存在", enterCustomer.EnterName));
                                    continue;
                                }
                                int id = await bll.AddEnterCustomer(enterCustomer);

                                if (id > 0)
                                {
                                    enteridDIc[enterCustomer.EnterName] = id;
                                }
                                else
                                {
                                    stringBuilder.AppendLine(string.Format("企业【{0}】|添加失败", enterCustomer.EnterName));
                                }
                            }
                            #endregion

                            #region 联系人信息
                            EnterCustContactsBll enterCustContactsBll = new EnterCustContactsBll();
                            ExcelWorksheet       worksheet1           = package.Workbook.Worksheets[2];
                            int rowCount1 = worksheet1.Dimension.Rows;
                            int colCount1 = worksheet1.Dimension.Columns;
                            if (rowCount1 < 2 || colCount1 < 11)
                            {
                                result.msg = "客户联系人信息格式不正确";
                                return(Json(result));
                            }

                            for (int row = 2; row < rowCount1; row++)
                            {
                                EnterCustContacts enterCustContacts = new EnterCustContacts();
                                for (int col = 1; col < colCount1; col++)
                                {
                                    string value = worksheet1.Cells[row, col].Value.TryToString().Trim();
                                    switch (col)
                                    {
                                    case 1:
                                        if (enteridDIc.ContainsKey(value))
                                        {
                                            enterCustContacts.EnterCustID = enteridDIc[value];
                                        }
                                        break;

                                    case 2:
                                        enterCustContacts.Name = value;
                                        break;

                                    case 3:
                                        if (value == "男")
                                        {
                                            enterCustContacts.Sex = Model.Enume.SexEnum.Man;
                                        }
                                        else if (value == "女")
                                        {
                                            enterCustContacts.Sex = Model.Enume.SexEnum.Woman;
                                        }
                                        break;

                                    case 4:
                                        enterCustContacts.Business = value;
                                        break;

                                    case 5:
                                        enterCustContacts.Department = value;
                                        break;

                                    case 6:
                                        enterCustContacts.Duties = value;
                                        break;

                                    case 7:
                                        enterCustContacts.Landline = value;
                                        break;

                                    case 8:
                                        enterCustContacts.Telephone = value;
                                        break;

                                    case 9:
                                        enterCustContacts.Email = value;
                                        break;

                                    case 10:
                                        enterCustContacts.QQ = value;
                                        break;

                                    case 11:
                                        enterCustContacts.WeChart = value;
                                        break;

                                    case 12:
                                        enterCustContacts.Address = value;
                                        break;

                                    case 13:
                                        enterCustContacts.Rem = value;
                                        break;
                                    }
                                }
                                if (enterCustContacts.EnterCustID > 0)
                                {
                                    int id = await enterCustContactsBll.Add(enterCustContacts);

                                    if (id <= 0)
                                    {
                                        stringBuilder.AppendLine(string.Format("企业联系人【{0}】信息保存失败", enterCustContacts.Name));
                                    }
                                }
                            }
                            #endregion

                            if (stringBuilder.Length > 0)
                            {
                                string errorfile     = DateTime.Now.ToString("yyyyMMddhhmmssff") + UserID + ".txt";
                                string errorfilePath = Path.Combine(virtualPath, errorfile);
                                try
                                {
                                    FileStream   filestream = new FileStream(errorfilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                                    StreamWriter writer     = new StreamWriter(filestream, System.Text.Encoding.UTF8);
                                    writer.BaseStream.Seek(0, SeekOrigin.End);
                                    await writer.WriteAsync(stringBuilder.ToString());

                                    writer.Flush();
                                    writer.Close();
                                    filestream.Close();
                                    result.data       = new PicMsg();
                                    result.data.src   = "/file/" + errorfile;
                                    result.data.title = errorfile;
                                }
                                catch
                                {
                                }
                            }
                        }
                        result.code = 0;
                        result.msg  = "OK";
                    }
                    catch (Exception exception)
                    {
                        result.msg = "上传失败:" + exception.Message;
                    }
                }
            }

            return(Json(result));
        }
Ejemplo n.º 2
0
        private void SpiderList(string url, string price, string city)
        {
            EnterCustomerBll bll = new EnterCustomerBll();
            int       totalPage  = 1;
            HttpUtils httpUtils  = new HttpUtils();

            for (int j = 1; j < 6; j++)
            {
                for (int i = 1; i <= totalPage; i++)
                {
                    Thread.Sleep(2000);
                    string     lurl       = $"{url}&t={j}&p={i}";
                    string     html       = httpUtils.Get(lurl);
                    HtmlParser htmlParser = new HtmlParser();
                    var        dom        = htmlParser.ParseDocument(html);
                    if (dom != null)
                    {
                        var slist = dom.QuerySelectorAll("div.sk");
                        if (slist != null)
                        {
                            foreach (var s in slist)
                            {
                                EnterCustomer enterCustomer = new EnterCustomer();
                                var           h4            = s.QuerySelector("h4");
                                if (h4 != null)
                                {
                                    enterCustomer.Abbreviation = h4.TextContent;
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.MiddleSchool;
                                    enterCustomer.EnterName    = h4.TextContent;

                                    s.RemoveChild(h4);
                                }
                                var div = s.QuerySelector("div.kw");
                                if (div != null)
                                {
                                    s.RemoveChild(div);
                                }
                                string context = s.TextContent;
                                var    param   = context.Split("地址");
                                if (context.Contains("地址") && context.Contains("电话") && param != null && param.Length >= 2)
                                {
                                    string p1 = param[0];
                                    string p2 = param[1];//地址
                                    if (p1.Contains("邮编"))
                                    {
                                        var pr = p1.Split("邮编");
                                        if (pr != null && pr.Length >= 2)
                                        {
                                            enterCustomer.Landline = pr[0].Replace("电话:", "");
                                        }
                                    }
                                    else
                                    {
                                        enterCustomer.Landline = p1.Replace("电话:", "");
                                    }
                                }
                                enterCustomer.Province = price;
                                enterCustomer.City     = city;
                                enterCustomer.Source   = Model.Enume.CustSource.Other;
                                enterCustomer.State    = Model.Enume.StateEnum.Invalid;
                                switch (j)
                                {
                                case 1:
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.PrimarySchool;
                                    break;

                                case 2:
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.PrimarySchool;
                                    break;

                                case 3:
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.MiddleSchool;
                                    break;

                                case 4:
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Colleges;
                                    break;

                                case 5:
                                    enterCustomer.CustomerType = Model.Enume.CustomerTypeEnum.Colleges;
                                    break;
                                }
                                if (!bll.ExistsEnterNameAsync(0, enterCustomer.EnterName).Result)
                                {
                                    bll.AddEnterCustomer(enterCustomer);
                                }
                                ClassLoger.Info("成功抓取学校:", enterCustomer.EnterName);
                                Console.WriteLine(enterCustomer.EnterName);
                            }
                        }
                        var fenyeDiv = dom.QuerySelector("div.fy");
                        if (fenyeDiv != null)
                        {
                            var zysspan = fenyeDiv.QuerySelector("span.zys");
                            if (zysspan != null)
                            {
                                totalPage = zysspan.TextContent.TryToInt(1);
                            }
                        }
                    }
                }
            }
        }