public void ImportSAPService()
        {
            while (bIsRunning)
            {
                try
                {
                    ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService sd = new ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService();

                    NetworkCredential c = new NetworkCredential();
                    c.UserName = ConfigHelper.GetConfigString("SAP_ZSD_User");
                    c.Password = ConfigHelper.GetConfigString("SAP_ZSD_Password");
                    sd.Credentials = c;

                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results1 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results2 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    sd.ZSdSdSh("1", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("1", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results2);

                    List<ImportExcelTDP.SAP_ZSD.Ztdplog> results = new List<ImportExcelTDP.SAP_ZSD.Ztdplog>();
                    results.AddRange(results1);
                    results.AddRange(results2);
                    SendMessage("ImportSAPService", "获取到" + results.Count().ToString() + "条发货数量!");

                    if (results.Count() == 0) { Thread.Sleep(3600 * 1000); continue; }

                    PBM_DeliveryBLL bll = new PBM_DeliveryBLL();
                    decimal totalamount = 0;
                    foreach (ImportExcelTDP.SAP_ZSD.Ztdplog item in results.OrderBy(p => p.Vgbel))
                    {
                        if (item.Vgbel != bll.Model.SheetCode)
                        {
                            #region 更新发货单
                            if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                            {
                                //计算折扣金额
                                bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                                    Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                                //明细合计金额
                                decimal actamount = Math.Round(bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor), 2);

                                //优惠金额(明细合计金额与实际采购金额的差额)
                                bll.Model.WipeAmount = actamount - totalamount;

                                //计算实际销售金额
                                bll.Model.ActAmount = (bll.Model.Classify == 12 ? -1 : 1) * (actamount - bll.Model.WipeAmount);

                                int ret = bll.Add();
                                if (ret > 0)
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",正在提交,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                                else
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",提交失败!Ret=" + ret.ToString());

                                bll = null;
                                totalamount = 0;
                            }
                            #endregion

                            #region 新增采购单据

                            //判断单号有没有导入过
                            if (PBM_DeliveryBLL.GetModelList("SheetCode='" + item.Vgbel + "' AND Supplier=1000 AND Classify IN (11,12)").Count > 0)
                            {
                                SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",之前已存在!");
                                continue;
                            }

                            bll = new PBM_DeliveryBLL();
                            bll.Model.Classify = 11;

                            #region 判断TDP编码Kunage
                            string tdpcode = item.Kunag.Substring(3);
                            IList<CM_ClientManufactInfo> _clients = CM_ClientManufactInfoBLL.GetModelList("Code='" + tdpcode + "' AND Manufacturer=1000 AND State = 1");
                            if (_clients.Count == 0)
                            {
                                SendMessage("ImportSAPService", "门店编码无效,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.SheetCode = item.Vgbel;
                            bll.Model.Supplier = 1000;
                            bll.Model.Client = _clients[0].Client;

                            #region 获取经销商主仓库
                            IList<CM_WareHouse> _warehouses = CM_WareHouseBLL.GetEnbaledByClient(bll.Model.Client).Where(p => p.Classify == 1).OrderBy(p => p.ID).ToList();
                            if (_warehouses.Count == 0)
                            {
                                SendMessage("ImportSAPService", "未找到客户主仓库,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.ClientWareHouse = _warehouses[0].ID;
                            bll.Model.PrepareMode = 1;
                            bll.Model.State = 1;        //默认制单状态
                            bll.Model.WipeAmount = 0;
                            bll.Model.Remark = "SAP导入";
                            bll.Model.ApproveFlag = 2;
                            bll.Model.InsertStaff = 1;
                            DateTime _prearrivaldate = DateTime.Today;
                            DateTime.TryParse(item.WadatIst, out _prearrivaldate);
                            bll.Model.PreArrivalDate = _prearrivaldate;
                            #endregion
                        }

                        #region 新增商品明细
                        string productcode = item.Matnr.Substring(item.Matnr.Length - 6);
                        IList<PDT_Product> _products = PDT_ProductBLL.GetModelList("FactoryCode='" + productcode + "' AND State=1");
                        if (_products.Count == 0)
                        {
                            SendMessage("ImportSAPService", "商品编码无效,单据号" + item.Vgbel + ",商品编号:" + productcode);
                            continue;
                        }
                        int productid = _products[0].ID;
                        PDT_ProductBLL productbll = new PDT_ProductBLL(productid);

                        PBM_DeliveryDetail d = new PBM_DeliveryDetail();
                        d.Product = productid;
                        d.ConvertFactor = _products[0].ConvertFactor == 0 ? 1 : _products[0].ConvertFactor;
                        d.SalesMode = 1;
                        d.DeliveryQuantity = (int)Math.Round(item.Fkimg * d.ConvertFactor, 0);

                        if (d.DeliveryQuantity < 0)
                        {
                            bll.Model.Classify = 12;        //采购退库单
                            d.DeliveryQuantity = 0 - d.DeliveryQuantity;
                        }

                        d.SignInQuantity = d.DeliveryQuantity;
                        d.CostPrice = Math.Round(item.Kzwi / d.DeliveryQuantity, 4);
                        d.Price = d.CostPrice;
                        d.DiscountRate = Math.Round(item.Kzwi5 / item.Kzwi, 4);
                        d.LotNumber = item.Charg;
                        totalamount += item.Kzwi5;

                        DateTime _productdate = new DateTime(1900, 1, 1);
                        DateTime.TryParse(item.Hsdat, out _productdate);

                        if (_productdate.Year < 1900) _productdate = new DateTime(1900, 1, 1);
                        d.ProductDate = _productdate;
                        d.Remark = item.Fkart;

                        bll.Items.Add(d);
                        #endregion

                        #region 判断是否在默认价表中
                        PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Client);
                        if (extinfo == null)
                        {
                            extinfo = new PDT_ProductExtInfo();
                            extinfo.Supplier = bll.Model.Client;
                            extinfo.Product = productid;
                            extinfo.Category = productbll.Model.Category;
                            extinfo.Code = productbll.Model.FactoryCode;
                            extinfo.SalesState = 1;
                            extinfo.BuyPrice = d.Price;
                            extinfo.SalesPrice = d.Price;
                            new PDT_ProductBLL(productid).SetProductExtInfo(extinfo);
                        }
                        #endregion
                    }

                    #region 更新发货单
                    if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                    {
                        //计算折扣金额
                        bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                            Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                        //计算实际销售金额
                        bll.Model.ActAmount = Math.Round((bll.Model.Classify == 12 ? -1 : 1) *
            bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
            - bll.Model.WipeAmount, 2);

                        int ret = bll.Add();
                        if (ret > 0)
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交成功,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                        else
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交失败!Ret=" + ret.ToString());
                    }
                    #endregion

                    Thread.Sleep(3600 * 1000);
                }
                catch (ThreadAbortException e)
                {
                    this.SendMessage("ImportSAPService.ThreadAbortException", e.Message);
                    Thread.Sleep(10000);
                    continue;
                }
                catch (Exception ex)
                {
                    this.SendMessage("ImportSAPService.Exception", ex.Message);
                    Thread.Sleep(10000);
                    continue;
                }
            }
        }
        public void ImportSAPService()
        {
            while (bIsRunning)
            {
                try
                {
                    ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService sd = new ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService();

                    NetworkCredential c = new NetworkCredential();
                    c.UserName     = ConfigHelper.GetConfigString("SAP_ZSD_User");
                    c.Password     = ConfigHelper.GetConfigString("SAP_ZSD_Password");
                    sd.Credentials = c;

                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results1 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results2 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    sd.ZSdSdSh("1", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("1", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results2);

                    List <ImportExcelTDP.SAP_ZSD.Ztdplog> results = new List <ImportExcelTDP.SAP_ZSD.Ztdplog>();
                    results.AddRange(results1);
                    results.AddRange(results2);
                    SendMessage("ImportSAPService", "获取到" + results.Count().ToString() + "条发货数量!");

                    if (results.Count() == 0)
                    {
                        Thread.Sleep(3600 * 1000); continue;
                    }

                    PBM_DeliveryBLL bll         = new PBM_DeliveryBLL();
                    decimal         totalamount = 0;
                    foreach (ImportExcelTDP.SAP_ZSD.Ztdplog item in results.OrderBy(p => p.Vgbel))
                    {
                        if (item.Vgbel != bll.Model.SheetCode)
                        {
                            #region 更新发货单
                            if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                            {
                                //计算折扣金额
                                bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                                                                         Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                                //明细合计金额
                                decimal actamount = Math.Round(bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor), 2);

                                //优惠金额(明细合计金额与实际采购金额的差额)
                                bll.Model.WipeAmount = actamount - totalamount;

                                //计算实际销售金额
                                bll.Model.ActAmount = (bll.Model.Classify == 12 ? -1 : 1) * (actamount - bll.Model.WipeAmount);



                                int ret = bll.Add();
                                if (ret > 0)
                                {
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",正在提交,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                                }
                                else
                                {
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",提交失败!Ret=" + ret.ToString());
                                }

                                bll         = null;
                                totalamount = 0;
                            }
                            #endregion

                            #region 新增采购单据

                            //判断单号有没有导入过
                            if (PBM_DeliveryBLL.GetModelList("SheetCode='" + item.Vgbel + "' AND Supplier=1000 AND Classify IN (11,12)").Count > 0)
                            {
                                SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",之前已存在!");
                                continue;
                            }

                            bll = new PBM_DeliveryBLL();
                            bll.Model.Classify = 11;

                            #region 判断TDP编码Kunage
                            string tdpcode = item.Kunag.Substring(3);
                            IList <CM_ClientManufactInfo> _clients = CM_ClientManufactInfoBLL.GetModelList("Code='" + tdpcode + "' AND Manufacturer=1000 AND State = 1");
                            if (_clients.Count == 0)
                            {
                                SendMessage("ImportSAPService", "门店编码无效,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.SheetCode = item.Vgbel;
                            bll.Model.Supplier  = 1000;
                            bll.Model.Client    = _clients[0].Client;

                            #region 获取经销商主仓库
                            IList <CM_WareHouse> _warehouses = CM_WareHouseBLL.GetEnbaledByClient(bll.Model.Client).Where(p => p.Classify == 1).OrderBy(p => p.ID).ToList();
                            if (_warehouses.Count == 0)
                            {
                                SendMessage("ImportSAPService", "未找到客户主仓库,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.ClientWareHouse = _warehouses[0].ID;
                            bll.Model.PrepareMode     = 1;
                            bll.Model.State           = 1; //默认制单状态
                            bll.Model.WipeAmount      = 0;
                            bll.Model.Remark          = "SAP导入";
                            bll.Model.ApproveFlag     = 2;
                            bll.Model.InsertStaff     = 1;
                            DateTime _prearrivaldate = DateTime.Today;
                            DateTime.TryParse(item.WadatIst, out _prearrivaldate);
                            bll.Model.PreArrivalDate = _prearrivaldate;
                            #endregion
                        }

                        #region 新增商品明细
                        string productcode            = item.Matnr.Substring(item.Matnr.Length - 6);
                        IList <PDT_Product> _products = PDT_ProductBLL.GetModelList("FactoryCode='" + productcode + "' AND State=1");
                        if (_products.Count == 0)
                        {
                            SendMessage("ImportSAPService", "商品编码无效,单据号" + item.Vgbel + ",商品编号:" + productcode);
                            continue;
                        }
                        int            productid  = _products[0].ID;
                        PDT_ProductBLL productbll = new PDT_ProductBLL(productid);


                        PBM_DeliveryDetail d = new PBM_DeliveryDetail();
                        d.Product          = productid;
                        d.ConvertFactor    = _products[0].ConvertFactor == 0 ? 1 : _products[0].ConvertFactor;
                        d.SalesMode        = 1;
                        d.DeliveryQuantity = (int)Math.Round(item.Fkimg * d.ConvertFactor, 0);

                        if (d.DeliveryQuantity < 0)
                        {
                            bll.Model.Classify = 12;        //采购退库单
                            d.DeliveryQuantity = 0 - d.DeliveryQuantity;
                        }

                        d.SignInQuantity = d.DeliveryQuantity;
                        d.CostPrice      = Math.Round(item.Kzwi / d.DeliveryQuantity, 4);
                        d.Price          = d.CostPrice;
                        d.DiscountRate   = Math.Round(item.Kzwi5 / item.Kzwi, 4);
                        d.LotNumber      = item.Charg;
                        totalamount     += item.Kzwi5;

                        DateTime _productdate = new DateTime(1900, 1, 1);
                        DateTime.TryParse(item.Hsdat, out _productdate);

                        if (_productdate.Year < 1900)
                        {
                            _productdate = new DateTime(1900, 1, 1);
                        }
                        d.ProductDate = _productdate;
                        d.Remark      = item.Fkart;

                        bll.Items.Add(d);
                        #endregion

                        #region 判断是否在默认价表中
                        PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Client);
                        if (extinfo == null)
                        {
                            extinfo            = new PDT_ProductExtInfo();
                            extinfo.Supplier   = bll.Model.Client;
                            extinfo.Product    = productid;
                            extinfo.Category   = productbll.Model.Category;
                            extinfo.Code       = productbll.Model.FactoryCode;
                            extinfo.SalesState = 1;
                            extinfo.BuyPrice   = d.Price;
                            extinfo.SalesPrice = d.Price;
                            new PDT_ProductBLL(productid).SetProductExtInfo(extinfo);
                        }
                        #endregion
                    }

                    #region 更新发货单
                    if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                    {
                        //计算折扣金额
                        bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                                                                 Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                        //计算实际销售金额
                        bll.Model.ActAmount = Math.Round((bll.Model.Classify == 12 ? -1 : 1) *
                                                         bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
                                                         - bll.Model.WipeAmount, 2);

                        int ret = bll.Add();
                        if (ret > 0)
                        {
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交成功,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                        }
                        else
                        {
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交失败!Ret=" + ret.ToString());
                        }
                    }
                    #endregion

                    Thread.Sleep(3600 * 1000);
                }
                catch (ThreadAbortException e)
                {
                    this.SendMessage("ImportSAPService.ThreadAbortException", e.Message);
                    Thread.Sleep(10000);
                    continue;
                }
                catch (Exception ex)
                {
                    this.SendMessage("ImportSAPService.Exception", ex.Message);
                    Thread.Sleep(10000);
                    continue;
                }
            }
        }