Пример #1
0
        private void UpdateProductButton_Click(object sender, EventArgs e)
        {
            Wait.Show("Đang cập nhật dữ liệu " + shopNameTextEdit.Text);
            try
            {
                long idcompany = Common.Obj2Int64(companyIdWSSSpinEdit.Text);
                QT.Entities.Company company             = new QT.Entities.Company(idcompany);
                string shopname                         = shopNameTextEdit.Text;
                string accesstoken                      = accessTokenTextEdit.Text;
                List <QT.Entities.Product> ListProducts = GetProductFromHaravan(shopname, accesstoken, company);
                labelControlMessage.Text = string.Format("Get {0} product!", ListProducts.Count);
                CompanyFunctions companyFunctions   = new CompanyFunctions();
                var cancelUpdateDataFeedTokenSource = new CancellationTokenSource();
                companyFunctions.UpdateProductsToSql(company, ListProducts, cancelUpdateDataFeedTokenSource);

                #region Public Cửa hàng
                long idharavan = Common.Obj2Int64(companyIdHaravanSpinEdit.Text);
                company_HaravanTableAdapter.UpdateIsPublic(true, idharavan);
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR " + ex.Message);
            }
            Wait.Close();
        }
Пример #2
0
        private void simpleButtonUpdateProduct_Click(object sender, EventArgs e)
        {
            Wait.Show("Đang cập nhật dữ liệu " + shopNameTextEdit.Text);
            try
            {
                long idcompany = Common.Obj2Int64(companyIdWSSTextEdit.Text);
                QT.Entities.Company company                 = new QT.Entities.Company(idcompany);
                string shopname                             = shopNameTextEdit.Text;
                string accesstoken                          = accessTokenTextEdit.Text;
                List <QT.Entities.Product> ListProducts     = GetProductFromBizweb(shopname, accesstoken, company);
                CompanyFunctions           companyFunctions = new CompanyFunctions();
                var cancelUpdateDataFeedTokenSource         = new CancellationTokenSource();
                companyFunctions.UpdateProductsToSql(company, ListProducts, cancelUpdateDataFeedTokenSource);

                #region Public Cửa hàng
                long idbizweb = Common.Obj2Int64(companyIdBizwebTextEdit.Text);
                company_BizwebTableAdapter.UpdateIsPublic(true, idbizweb);
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR " + ex.Message);
            }
            Wait.Close();
        }
Пример #3
0
 private void UpdateProductBizweb(CompanyFunctions companyFunctions, long idWebsosanh)
 {
     DBBizTableAdapters.Company_BizwebTableAdapter bizwebTableAdapter = new DBBizTableAdapters.Company_BizwebTableAdapter();
     bizwebTableAdapter.Connection.ConnectionString = connectionString;
     DBBiz.Company_BizwebDataTable bizwebTable = new DBBiz.Company_BizwebDataTable();
     try
     {
         bizwebTableAdapter.FillBy_IDWSS(bizwebTable, idWebsosanh);
         if (bizwebTable.Rows.Count > 0)
         {
             QT.Entities.Company company = new QT.Entities.Company(idWebsosanh);
             if (company.Name.ToLower() == "not in database")
             {
                 Log.ErrorFormat("BIZWEB : ID received in RabbitMQ not in Company... ID = {0} ", idWebsosanh);
             }
             else
             {
                 string shopname    = bizwebTable.Rows[0]["ShopName"].ToString();
                 string accesstoken = bizwebTable.Rows[0]["AccessToken"].ToString();
                 List <QT.Entities.Product> ListProducts = QT.Moduls.Bizweb.frmSettingBizwebs.GetProductFromBizweb(shopname, accesstoken, company);
                 Log.InfoFormat("Get {0} product of Company {1}, ID = {2}", ListProducts.Count, company.Domain, company.ID);
                 var cancelUpdateDataFeedTokenSource = new CancellationTokenSource();
                 companyFunctions.UpdateProductsToSql(company, ListProducts, cancelUpdateDataFeedTokenSource);
             }
         }
         else
         {
             Log.ErrorFormat("BIZWEB: ID received in RabbitMQ not in Company_Bizweb... ID = {0}", idWebsosanh);
         }
     }
     catch (Exception ex)
     {
         Log.Error("BIZWEB ERROR : ", ex);
     }
 }
Пример #4
0
 private void InitData()
 {
     _company = new QT.Entities.Company(_idCompany);
     // TODO: This line of code loads data into the 'dBProperties.Classification' table. You can move, or remove it, as needed.
     this.classificationTableAdapter.FillByCompanyID(this.dBProperties.Classification, _company.ID);
     //productBindingSource.AddNew();
     this.companyTextEdit.Text = _company.ID.ToString();
     if (_company.Status == QT.Entities.Common.CompanyStatus.WEB_NOMERCHANT)
     {
         detailUrlTextEdit.Properties.ReadOnly = true;
     }
 }
Пример #5
0
        private void simpleButtonTestProduct_Click(object sender, EventArgs e)
        {
            Wait.Show("Đang tải dữ liệu...");
            long idcompany = Common.Obj2Int64(companyIdWSSTextEdit.Text);

            QT.Entities.Company company = new QT.Entities.Company(idcompany);
            if (company.Name.ToLower() == "not in database")
            {
                MessageBox.Show("ERROR! Không tồn tại công ty này trong database!");
            }
            else
            {
                string shopname    = shopNameTextEdit.Text;
                string accesstoken = accessTokenTextEdit.Text;
                gridControlProduct.DataSource = GetProductFromBizweb(shopname, accesstoken, company);
            }
            Wait.Close();
        }
Пример #6
0
        private void TestProductButton_Click(object sender, EventArgs e)
        {
            Wait.Show("Đang tải sản phẩm từ Haravan");
            long idcompany = Common.Obj2Int64(companyIdWSSSpinEdit.Text);

            QT.Entities.Company company = new QT.Entities.Company(idcompany);
            if (company.Name.ToLower() == "not in database")
            {
                MessageBox.Show("Không tồn tại công ty này trong database! Ấn Check Company để kiểm tra");
            }
            else
            {
                string shopname    = shopNameTextEdit.Text;
                string accesstoken = accessTokenTextEdit.Text;
                gridControlProduct.DataSource = GetProductFromHaravan(shopname, accesstoken, company);
            }
            Wait.Close();
        }
Пример #7
0
 private void UpdateProductHaravan(CompanyFunctions companyFunctions, long idWebsosanh)
 {
     //Log.Info(string.Format("Nhan message {0}", idWebsosanh));
     DBHarTableAdapters.Company_HaravanTableAdapter haravanAdapter = new DBHarTableAdapters.Company_HaravanTableAdapter();
     haravanAdapter.Connection.ConnectionString = connectionString;
     DBHar.Company_HaravanDataTable haravanTable = new DBHar.Company_HaravanDataTable();
     try
     {
         haravanAdapter.FillBy_IDWSS(haravanTable, idWebsosanh);
         if (haravanTable.Rows.Count > 0)
         {
             QT.Entities.Company company = new QT.Entities.Company(idWebsosanh);
             if (company.Name.ToLower() == "not in database")
             {
                 Log.ErrorFormat("HARAVAN : ID received in RabbitMQ not in Company... ID = {0} ", idWebsosanh);
             }
             else
             {
                 string shopname    = haravanTable.Rows[0]["ShopName"].ToString();
                 string accesstoken = haravanTable.Rows[0]["AccessToken"].ToString();
                 List <QT.Entities.Product> ListProducts = QT.Moduls.WebPartner.frmSettingHaravan.GetProductFromHaravan(shopname, accesstoken, company);
                 Log.InfoFormat("Get {0} product of Company {1}, ID = {2}", ListProducts.Count,
                                company.Domain, company.ID);
                 var cancelUpdateDataFeedTokenSource = new CancellationTokenSource();
                 companyFunctions.UpdateProductsToSql(company, ListProducts,
                                                      cancelUpdateDataFeedTokenSource);
             }
         }
         else
         {
             Log.ErrorFormat("HARAVAN: ID received in RabbitMQ not in Company_Haravan... ID = {0}", idWebsosanh);
         }
     }
     catch (Exception exx)
     {
         Log.Error("HARAVAN ERROR: ", exx);
     }
 }
Пример #8
0
        public List <StructCompanyXPath> ExportXPathData()
        {
            QT.Entities.Server.ConnectionString = _sqlDb.connection.ConnectionString;
            List <StructCompanyXPath> lstStructCompany = new List <StructCompanyXPath>();
            DataTable tblCompany = _sqlDb.GetTblData("select  id from company where status = 1 and datafeedtype = 0");

            int i = 0;

            foreach (DataRow rowInfo in tblCompany.Rows)
            {
                log.Info(string.Format("{0}/{1}", i++, tblCompany.Rows.Count));

                long CompanyID = QT.Entities.Common.Obj2Int64(rowInfo["ID"]);
                QT.Entities.Company company = new QT.Entities.Company(CompanyID);
                Configuration       config  = new Configuration(CompanyID);
                lstStructCompany.Add(new StructCompanyXPath()
                {
                    CompanyID   = CompanyID,
                    Website     = company.Website,
                    structXpath = new StructXPath()
                    {
                        ProductNameXPath   = config.ProductNameXPath,
                        ImageXPath         = config.ImageXPath,
                        PriceXPath         = config.PriceXPath,
                        StatusXPath        = config.StatusXPath,
                        VATInfoXPath       = config.VATInfoXPath,
                        PromotionInfoXPath = config.PromotionInfoXPath,
                        OriginPriceXPath   = config.OriginPriceXPath,
                        CategoryXPath      = config.CategoryXPath,
                        StartDealXPath     = config.StartDealXPath,
                        EndDealXPath       = config.EndDealXPath,
                    }
                });
            }
            return(lstStructCompany);
        }
Пример #9
0
 public void CrawlForCompany(long CompanyID)
 {
     try
     {
         this._companyId = CompanyID;
         if ((!_bCheckOtherRunning || !CheckOtherRunning()) && _productAdapter.AllowCrawlFindNew(_companyId))
         {
             if (!_redisWaitCrawler.CheckHaveItemFindNew(_companyId))
             {
                 _redisWaitCrawler.SetNexFindNew(_companyId, 1);
                 var company = new QT.Entities.Company(this._companyId);
                 _redisCacheCompanyCrawler.SetCompanyInfo(_companyId, company.Domain, 24, 24);
             }
             Crawl();
         }
         else
         {
             _redisWaitCrawler.SetNexFindNew(_companyId, 1);
         }
     }
     catch (System.OperationCanceledException)
     {
         if (this._company != null)
         {
             this._redisQueueFindNew.SaveQueue(this._companyId, this._linkQueue.ToArray());
             this._redisWaitCrawler.SetRemoveRunningCrawler(_companyId);
             this._redisWaitCrawler.SetNexFindNew(this._companyId, 1);
             this._redisCrcVisited.SetForCompany(_companyId, new List <long>(this._visitedCrc.Keys));
         }
         throw;
     }
     catch (Exception ex02)
     {
         _log.Error(ex02);
     }
 }
Пример #10
0
        public static List <QT.Entities.Product> GetProductFromBizweb(string shopname, string accesstoken, QT.Entities.Company company)
        {
            List <QT.Entities.Product> ListProducts = new List <QT.Entities.Product>();
            var authorizeState = new BizwebAuthorizationState()
            {
                ShopName = shopname, AccessToken = accesstoken
            };
            var client = new BizwebAPIClient(authorizeState);
            ProductBizwebInfo listproductBizweb = new ProductBizwebInfo()
            {
                products = new List <Product>()
            };
            bool check = true;
            int  page  = 1;

            while (check)
            {
                Object obj = null;
                try
                {
                    //Mỗi lần lấy về 50 product của bizweb
                    string querry = "admin/products.json?page=" + page.ToString() + "&limit=250";
                    //string querry = "/admin/products.json";
                    obj = client.Get(querry);
                    //Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    check = false;
                    Log.ErrorFormat("BIZWEB : Get/admin/products.json FAILS. ERROR : {0}", ex);
                }
                if (obj != null)
                {
                    ProductBizwebInfo listproduct = JsonConvert.DeserializeObject <ProductBizwebInfo>(obj.ToString());
                    if (listproduct.products.Count > 0)
                    {
                        listproductBizweb.products.AddRange(listproduct.products);
                        page++;
                    }
                    else
                    {
                        check = false;
                    }
                }
                else
                {
                    check = false;
                }
            }
            if (listproductBizweb.products.Count > 0)
            {
                #region Chuyển từ ListProducts of Bizweb sang ListProductWebsosanh
                for (int i = 0; i < listproductBizweb.products.Count; i++)
                {
                    //Check số lượng phiên bản variant
                    int variant = listproductBizweb.products[i].variants.Count;
                    //TH variant = 1 (1 phiên bản)
                    if (variant == 1)
                    {
                        #region Product
                        var tmpProduct = new QT.Entities.Product();
                        tmpProduct.Domain = company.Domain;
                        string parameter = "?";
                        if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?"))
                        {
                            parameter = "&";
                        }
                        tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + parameter + "utm=websosanh";

                        tmpProduct.ID = Common.GetIDProduct(tmpProduct.DetailUrl);

                        if (listproductBizweb.products[i].variants[0].sku != null)
                        {
                            tmpProduct.MerchantSku = listproductBizweb.products[i].variants[0].sku.ToString();
                        }

                        if (listproductBizweb.products[i].variants[0].inventory_policy == "continue" || listproductBizweb.products[i].variants[0].inventory_management == "")
                        {
                            tmpProduct.Status = Common.ProductStatus.Available;
                        }
                        else if (listproductBizweb.products[i].variants[0].inventory_policy == "deny")
                        {
                            var inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[0].inventory_quantity);
                            if (inventory_quantity > 0)
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.Clear;
                            }
                        }
                        else
                        {
                            tmpProduct.Status = Common.ProductStatus.LienHe;
                        }
                        tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                        tmpProduct.Manufacture = listproductBizweb.products[i].vendor;

                        tmpProduct.Name     = listproductBizweb.products[i].name;
                        tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                        tmpProduct.ProductContent = listproductBizweb.products[i].meta_description;

                        CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                        int         originPrice = 0;
                        int         price       = 0;
                        string      compare_at_price;
                        string      price_temp;
                        if ((listproductBizweb.products[i].variants[0].compare_at_price == null || listproductBizweb.products[i].variants[0].compare_at_price == 0) && listproductBizweb.products[i].variants[0].price == 0)
                        {
                            Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                            continue;
                        }
                        else
                        {
                            compare_at_price = listproductBizweb.products[i].variants[0].compare_at_price.ToString();
                            compare_at_price = (compare_at_price.Contains('.')) ? compare_at_price.Remove(compare_at_price.IndexOf('.')) : compare_at_price;
                            price_temp       = listproductBizweb.products[i].variants[0].price.ToString();
                            price_temp       = (price_temp.Contains('.')) ? price_temp.Remove(price_temp.IndexOf('.')) : price_temp;
                        }
                        try
                        {
                            originPrice = (int)Decimal.Parse(compare_at_price, cultureInfo);
                        }
                        catch (FormatException)
                        {
                        }
                        catch (Exception exception)
                        {
                            Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception);
                        }
                        try
                        {
                            price = (int)Decimal.Parse(price_temp, cultureInfo);
                        }
                        catch (FormatException ex)
                        {
                            Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex);
                        }
                        catch (Exception exception)
                        {
                            Log.Error(string.Format("BIZWEB : Parse discounted_price Error. {0}", tmpProduct.DetailUrl), exception);
                        }

                        if (price <= 0)
                        {
                            Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                            continue;
                        }
                        if (originPrice > price)
                        {
                            tmpProduct.OriginPrice = originPrice;
                            tmpProduct.Price       = price;
                        }
                        else
                        {
                            tmpProduct.OriginPrice = tmpProduct.Price = price;
                        }
                        //Categories
                        tmpProduct.Categories = new List <string>();
                        tmpProduct.Categories.Add(company.Domain);
                        tmpProduct.Categories.Add(listproductBizweb.products[i].product_type);
                        tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                        // list Url Images
                        if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0)
                        {
                            tmpProduct.ImageUrls = new List <string>()
                            {
                                listproductBizweb.products[i].images[0].src
                            };
                        }
                        else
                        {
                            tmpProduct.ImageUrls = new List <string>();
                        }
                        if (listproductBizweb.products[i].variants[0] != null)
                        {
                            tmpProduct.VATStatus = listproductBizweb.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT;
                        }
                        else
                        {
                            tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                        }
                        ListProducts.Add(tmpProduct);
                        #endregion
                    }
                    // TH có nhiều phiên bản
                    else if (variant > 1)
                    {
                        //check image nếu có ảnh thì = true, ko có = false
                        bool checkImage = false;
                        Dictionary <int, string> dicImage = new Dictionary <int, string>();
                        if (listproductBizweb.products[i].images != null && listproductBizweb.products[i].images.Count != 0)
                        {
                            checkImage = true;
                            for (int k = 0; k < listproductBizweb.products[i].images.Count; k++)
                            {
                                dicImage.Add(listproductBizweb.products[i].images[k].id, listproductBizweb.products[i].images[k].src);
                            }
                        }
                        else
                        {
                            checkImage = false;
                        }

                        for (int j = 0; j < variant; j++)
                        {
                            #region Product
                            var tmpProduct = new QT.Entities.Product();
                            tmpProduct.Domain = company.Domain;
                            string parameter = "?";
                            if (!string.IsNullOrEmpty(listproductBizweb.products[i].alias) && listproductBizweb.products[i].alias.Contains("?"))
                            {
                                parameter = "&";
                            }
                            tmpProduct.DetailUrl = company.Website + "products/" + listproductBizweb.products[i].alias + "#" + (j + 1).ToString() + parameter + "utm=websosanh";
                            tmpProduct.ID        = Common.GetIDProduct(tmpProduct.DetailUrl);

                            if (listproductBizweb.products[i].variants[j].sku != null)
                            {
                                tmpProduct.MerchantSku = listproductBizweb.products[i].variants[j].sku.ToString();
                            }

                            if (listproductBizweb.products[i].variants[j].inventory_policy == "continue" || listproductBizweb.products[i].variants[j].inventory_management == "")
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                            else if (listproductBizweb.products[i].variants[j].inventory_policy == "deny")
                            {
                                int inventory_quantity = Common.Obj2Int(listproductBizweb.products[i].variants[j].inventory_quantity);
                                if (inventory_quantity > 0)
                                {
                                    tmpProduct.Status = Common.ProductStatus.Available;
                                }
                                else
                                {
                                    tmpProduct.Status = Common.ProductStatus.Clear;
                                }
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.LienHe;
                            }
                            tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                            tmpProduct.Manufacture = listproductBizweb.products[i].vendor;
                            string nameproduct = listproductBizweb.products[i].name;
                            if (listproductBizweb.products[i].variants[j].option1 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].options[0].name + " " + listproductBizweb.products[i].variants[j].option1;
                            }
                            if (listproductBizweb.products[i].variants[j].option2 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option2;
                            }
                            if (listproductBizweb.products[i].variants[j].option3 != null)
                            {
                                nameproduct = nameproduct + " " + listproductBizweb.products[i].variants[j].option3;
                            }
                            tmpProduct.Name     = nameproduct;
                            tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                            tmpProduct.ProductContent = listproductBizweb.products[i].meta_description;

                            CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                            int         originPrice = 0;
                            int         price       = 0;
                            try
                            {
                                originPrice = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].compare_at_price.ToString(), cultureInfo);
                            }
                            catch (FormatException)
                            {
                            }
                            try
                            {
                                price = (int)Decimal.Parse(listproductBizweb.products[i].variants[0].price.ToString(), cultureInfo);
                            }
                            catch (FormatException ex)
                            {
                                Log.Error("BIZWEB : Parse discounted_price Error. " + listproductBizweb.products[i].variants[0].price + "\r\n" + ex);
                                continue;
                            }
                            if (price <= 0)
                            {
                                Log.Error("BIZWEB : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                                continue;
                            }
                            if (originPrice > price)
                            {
                                tmpProduct.OriginPrice = originPrice;
                                tmpProduct.Price       = price;
                            }
                            else
                            {
                                tmpProduct.OriginPrice = tmpProduct.Price = price;
                            }

                            //Categories
                            tmpProduct.Categories = new List <string>();
                            tmpProduct.Categories.Add(company.Domain);
                            tmpProduct.Categories.Add(listproductBizweb.products[i].product_type);
                            tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                            // list Url Images
                            if (checkImage)
                            {
                                int image_id = Common.Obj2Int(listproductBizweb.products[i].variants[j].image_id);
                                //nếu != 0 thì tìm values trong dicImage tương ứng
                                if (image_id != 0)
                                {
                                    string imageurl = dicImage.Where(kvp => kvp.Key == image_id).Select(kvp => kvp.Value).FirstOrDefault();
                                    if (!string.IsNullOrEmpty(imageurl))
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            imageurl
                                        }
                                    }
                                    ;
                                    else
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            listproductBizweb.products[i].images[0].src
                                        }
                                    };
                                }
                                // nếu = 0 (null) thì lấy ảnh mặc định images[0].src
                                else
                                {
                                    tmpProduct.ImageUrls = new List <string>()
                                    {
                                        listproductBizweb.products[i].images[0].src
                                    }
                                };
                            }
                            else
                            {
                                tmpProduct.ImageUrls = new List <string>();
                            }
                            if (listproductBizweb.products[i].variants[j] != null)
                            {
                                tmpProduct.VATStatus = listproductBizweb.products[i].variants[j].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT;
                            }
                            else
                            {
                                tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                            }
                            ListProducts.Add(tmpProduct);
                            #endregion
                        }
                    }
                    else if (variant < 1)
                    {
                        Log.ErrorFormat("Error : Variant of {0}  = 0", listproductBizweb.products[i].name);
                    }
                    #endregion
                }
            }
            return(ListProducts);
        }
Пример #11
0
        public List <Product> GetAllProduct(string csvPath, QT.Entities.Company company, DatafeedConfig datafeedConfig, Action <string> eventLog = null)
        {
            List <Product> lstPt       = new List <Product>();
            CultureInfo    cultureInfo = (datafeedConfig.CurrencyFormat == CurrencyFormat.Vietnamese) ? CultureInfo.GetCultureInfo("vi-VN") : CultureInfo.GetCultureInfo("en-US");
            var            csv         = new Wss.Lib.CsvHelper.CsvFile(csvPath, new Action <string>((string str) =>
            {
                _log.ErrorFormat("Can't parser csv line: {0}", str);
            }));

            while (csv.Read())
            {
                try
                {
                    var tmpProduct = new Product
                    {
                        Domain   = company.Domain,
                        IDCongTy = company.ID,
                        Status   = Common.ProductStatus.Available,
                        Instock  = Common.GetProductInstockFormStatus(Common.ProductStatus.Available)
                    };
                    string        strTemp         = "";
                    long          discountedPrice = 0;
                    long          originPrice     = 0;
                    List <string> lstCat          = new ArrayOfString();

                    if (csv.TryGetField(datafeedConfig.ProductNameNode, ref strTemp))
                    {
                        tmpProduct.Name = strTemp;
                    }

                    if (csv.TryGetField(datafeedConfig.UrlNode, ref strTemp))
                    {
                        tmpProduct.DetailUrl = strTemp;
                        tmpProduct.ID        = Wss.Lib.Utilities.UtilIdProduct.GenId(tmpProduct.DetailUrl, datafeedConfig.RegexConfigUrl);
                    }

                    if (csv.TryGetField(datafeedConfig.SkuNode, ref strTemp))
                    {
                        tmpProduct.MerchantSku = strTemp;
                    }

                    if (csv.TryGetField(datafeedConfig.BrandNode, ref strTemp))
                    {
                        tmpProduct.Manufacture = strTemp;
                    }
                    tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                    if (csv.TryGetField(datafeedConfig.DiscountedPriceNode, ref strTemp))
                    {
                        discountedPrice = (int)Decimal.Parse(Regex.Replace(strTemp.ToLower().Trim(), strNotMeanPrice, ""), cultureInfo);
                    }

                    if (csv.TryGetField(datafeedConfig.PriceNode, ref strTemp))
                    {
                        originPrice = (int)Decimal.Parse(Regex.Replace(strTemp.ToLower().Trim(), strNotMeanPrice, ""), cultureInfo);
                    }

                    #region {xuligia}

                    if (discountedPrice > 0)
                    {
                        if (originPrice > discountedPrice)
                        {
                            tmpProduct.Price       = (int)discountedPrice;
                            tmpProduct.OriginPrice = (int)originPrice;
                            //tmpProduct.IsDeal = true;
                        }
                        else
                        {
                            tmpProduct.Price = tmpProduct.OriginPrice = (int)discountedPrice;
                            //tmpProduct.IsDeal = false;
                        }
                    }
                    //
                    else if (originPrice > 0)
                    {
                        int discountPrice = 0;
                        //DiscountPrice (số tiền giảm)
                        try
                        {
                            if (csv.TryGetField(datafeedConfig.DiscountNode, ref strTemp))
                            {
                                discountPrice = (int)Decimal.Parse(Regex.Replace(strTemp.ToLower().Trim(), strNotMeanPrice, ""), cultureInfo);
                            }
                        }
                        catch (Exception)
                        {
                        }
                        if (discountPrice > 0)
                        {
                            tmpProduct.Price       = (int)originPrice - (int)discountPrice;
                            tmpProduct.OriginPrice = (int)originPrice;
                            //tmpProduct.IsDeal = true;
                        }
                        else
                        {
                            tmpProduct.Price = tmpProduct.OriginPrice = (int)originPrice;
                            //tmpProduct.IsDeal = false;
                        }
                    }

                    #endregion

                    if (csv.TryGetField(datafeedConfig.Category1Node, ref strTemp))
                    {
                        lstCat.Add(strTemp);
                    }

                    if (csv.TryGetField(datafeedConfig.Category2Node, ref strTemp))
                    {
                        lstCat.Add(strTemp);
                    }
                    tmpProduct.Categories   = lstCat;
                    tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                    if (csv.TryGetField(datafeedConfig.PictureUrl1Node, ref strTemp))
                    {
                        tmpProduct.ImageUrls = new ArrayOfString()
                        {
                            strTemp
                        };
                    }

                    tmpProduct.VATStatus = 1;


                    eventLog(string.Format("NumbItem: {0} CheckInfoProduct: {1}", lstPt.Count, CheckWarnInfo(tmpProduct)));
                    lstPt.Add(tmpProduct);
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                    throw ex;
                }
            }
            return(lstPt);
        }
Пример #12
0
        public static List <QT.Entities.Product> GetProductFromHaravan(string shopname, string accesstoken, QT.Entities.Company company)
        {
            List <QT.Entities.Product> ListProducts = new List <QT.Entities.Product>();
            var authorizeState = new HaravanAuthorizationState()
            {
                ShopName = shopname, AccessToken = accesstoken
            };
            var client = new HaravanAPIClient(authorizeState);

            ProductHaravanInfo listproductHaravan = new ProductHaravanInfo()
            {
                products = new List <Product>()
            };
            bool check = true;
            int  page  = 1;

            while (check)
            {
                Object obj = null;
                try
                {
                    //Mỗi lần lấy về 50 product của haravan
                    string querry = "/admin/products.json?page=" + page.ToString();
                    obj = client.Get(querry);
                }
                catch (Exception ex)
                {
                    check = false;
                    Log.ErrorFormat("HARAVAN : Get/admin/products.json FAILS. ERROR : {0}", ex);
                }
                if (obj != null)
                {
                    ProductHaravanInfo listproduct = JsonConvert.DeserializeObject <ProductHaravanInfo>(obj.ToString());
                    if (listproduct.products.Count > 0)
                    {
                        listproductHaravan.products.AddRange(listproduct.products);
                        page++;
                    }
                    else
                    {
                        check = false;
                    }
                }
                else
                {
                    check = false;
                }
            }


            if (listproductHaravan.products.Count > 0)
            {
                #region Chuyển từ ListProducts of Haravan sang ListProductWebsosanh
                for (int i = 0; i < listproductHaravan.products.Count; i++)
                {
                    if (string.IsNullOrEmpty(listproductHaravan.products[i].published_at) || Common.ObjectToDataTime(listproductHaravan.products[i].published_at) > DateTime.Now)
                    {
                        continue;
                    }
                    //Check số lượng phiên bản variant
                    int variant = listproductHaravan.products[i].variants.Count;
                    //TH variant = 1 (1 phiên bản)
                    if (variant == 1)
                    {
                        #region Product
                        var tmpProduct = new QT.Entities.Product();
                        tmpProduct.Domain = company.Domain;
                        var originalurl = string.Empty;
                        originalurl = company.Website + "products/" + listproductHaravan.products[i].handle;
                        if (company.ID == 7605281528637856297)
                        {
                            tmpProduct.DetailUrl = originalurl + "?ref=10066";
                        }
                        else
                        {
                            tmpProduct.DetailUrl = originalurl + "?utm=websosanh";
                        }

                        tmpProduct.ID = Common.GetIDProduct(originalurl);

                        if (listproductHaravan.products[i].variants[0].sku != null)
                        {
                            tmpProduct.MerchantSku = listproductHaravan.products[i].variants[0].sku;
                        }

                        if (listproductHaravan.products[i].variants[0].inventory_policy == "deny")
                        {
                            //check số lượng hàng còn trong kho
                            if (listproductHaravan.products[i].variants[0].inventory_quantity <= 0)
                            {
                                //17.09.2015 trạng thái là deny thì để là liên hệ
                                tmpProduct.Status = Common.ProductStatus.LienHe;
                                //tmpProduct.Status = Common.ProductStatus.Clear;
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                        }
                        else if (listproductHaravan.products[i].variants[0].inventory_policy == "continue")
                        {
                            int inventory_quantity = Common.Obj2Int(listproductHaravan.products[i].variants[0].inventory_quantity);
                            if (inventory_quantity > 0)
                            {
                                tmpProduct.Status = Common.ProductStatus.Available;
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.Clear;
                            }
                        }
                        else
                        {
                            tmpProduct.Status = Common.ProductStatus.LienHe;
                        }
                        tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                        tmpProduct.Manufacture = listproductHaravan.products[i].vendor;

                        tmpProduct.Name     = listproductHaravan.products[i].title;
                        tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                        tmpProduct.ProductContent = listproductHaravan.products[i].body_html;

                        CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                        int         originPrice = 0;
                        int         price       = 0;
                        try
                        {
                            originPrice = (int)Decimal.Parse(listproductHaravan.products[i].variants[0].compare_at_price.ToString(), cultureInfo);
                        }
                        catch (FormatException)
                        {
                        }
                        try
                        {
                            price = (int)Decimal.Parse(listproductHaravan.products[i].variants[0].price.ToString(), cultureInfo);
                        }
                        catch (FormatException ex)
                        {
                            Log.Error("HARAVAN : Parse discounted_price Error. " + listproductHaravan.products[i].variants[0].price + "/r/n" + ex);
                            continue;
                        }
                        if (price <= 0)
                        {
                            Log.Error("HARAVAN : Product price equal = 0" + "Product: ID" + tmpProduct.ID + " - " + tmpProduct.Name);
                            continue;
                        }
                        if (originPrice > price)
                        {
                            tmpProduct.OriginPrice = originPrice;
                            tmpProduct.Price       = price;
                        }
                        else
                        {
                            tmpProduct.OriginPrice = tmpProduct.Price = price;
                        }
                        //Categories
                        tmpProduct.Categories = new List <string>();
                        tmpProduct.Categories.Add(company.Domain);
                        tmpProduct.Categories.Add(listproductHaravan.products[i].product_type);
                        tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                        // list Url Images
                        if (listproductHaravan.products[i].images != null && listproductHaravan.products[i].images.Count != 0)
                        {
                            tmpProduct.ImageUrls = new List <string>()
                            {
                                listproductHaravan.products[i].images[0].src
                            };
                        }
                        else
                        {
                            tmpProduct.ImageUrls = new List <string>();
                        }
                        if (listproductHaravan.products[i].variants[0] != null)
                        {
                            tmpProduct.VATStatus = listproductHaravan.products[i].variants[0].taxable == false ? Common.VATStatus.NotVAT : Common.VATStatus.HaveVAT;
                        }
                        else
                        {
                            tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                        }
                        ListProducts.Add(tmpProduct);
                        #endregion
                    }
                    // TH có nhiều phiên bản
                    else if (variant > 1)
                    {
                        //check image nếu có ảnh thì = true, ko có = false
                        bool checkImage = false;
                        Dictionary <int, string> dicImage = new Dictionary <int, string>();
                        if (listproductHaravan.products[i].images != null && listproductHaravan.products[i].images.Count != 0)
                        {
                            checkImage = true;
                            for (int k = 0; k < listproductHaravan.products[i].images.Count; k++)
                            {
                                dicImage.Add(listproductHaravan.products[i].images[k].id, listproductHaravan.products[i].images[k].src);
                            }
                        }
                        else
                        {
                            checkImage = false;
                        }
                        for (int j = 0; j < variant; j++)
                        {
                            #region Product
                            var tmpProduct = new QT.Entities.Product();
                            tmpProduct.Domain = company.Domain;
                            string originalurl = string.Empty;
                            originalurl = company.Website + "products/" + listproductHaravan.products[i].handle + "?index=" + (j + 1).ToString();
                            //guphukien.com
                            if (company.ID == 7605281528637856297)
                            {
                                tmpProduct.DetailUrl = originalurl + "&ref=10066";
                            }
                            else
                            {
                                tmpProduct.DetailUrl = originalurl + "&utm=websosanh";
                            }
                            tmpProduct.ID = Common.GetIDProduct(originalurl);
                            if (listproductHaravan.products[i].variants[j].sku != null)
                            {
                                tmpProduct.MerchantSku = listproductHaravan.products[i].variants[0].sku;
                            }
                            //check status inventory_policy
                            if (listproductHaravan.products[i].variants[j].inventory_policy == "deny")
                            {
                                //check số lượng hàng còn trong kho
                                if (listproductHaravan.products[i].variants[j].inventory_quantity <= 0)
                                {
                                    //17.09.2015 trạng thái là deny thì để là liên hệ
                                    tmpProduct.Status = Common.ProductStatus.LienHe;
                                    //tmpProduct.Status = Common.ProductStatus.Clear;
                                }
                                else
                                {
                                    tmpProduct.Status = Common.ProductStatus.Available;
                                }
                            }
                            else if (listproductHaravan.products[i].variants[j].inventory_policy == "continue")
                            {
                                if (listproductHaravan.products[i].variants[j].inventory_quantity <= 0)
                                {
                                    tmpProduct.Status = Common.ProductStatus.Clear;
                                }
                                else
                                {
                                    tmpProduct.Status = Common.ProductStatus.Available;
                                }
                            }
                            else
                            {
                                tmpProduct.Status = Common.ProductStatus.LienHe;
                            }
                            tmpProduct.Instock     = QT.Entities.Common.GetProductInstockFormStatus(tmpProduct.Status);
                            tmpProduct.Manufacture = listproductHaravan.products[i].vendor;
                            string nameproduct = listproductHaravan.products[i].title;
                            if (listproductHaravan.products[i].variants[j].option1 != null)
                            {
                                nameproduct += " " + listproductHaravan.products[i].options[0].name + " " + listproductHaravan.products[i].variants[j].option1;
                            }
                            if (listproductHaravan.products[i].variants[j].option2 != null)
                            {
                                nameproduct += " " + listproductHaravan.products[i].options[1].name + " " + listproductHaravan.products[i].variants[j].option2;
                            }
                            if (listproductHaravan.products[i].variants[j].option3 != null)
                            {
                                nameproduct += " " + listproductHaravan.products[i].options[2].name + " " + listproductHaravan.products[i].variants[j].option3;
                            }
                            tmpProduct.Name     = nameproduct;
                            tmpProduct.HashName = Common.GetHashNameProduct(company.Domain, tmpProduct.Name.Trim());

                            tmpProduct.ProductContent = listproductHaravan.products[i].body_html;

                            CultureInfo cultureInfo = CultureInfo.GetCultureInfo("vi-VN");
                            int         originPrice = 0;
                            int         price       = 0;
                            try
                            {
                                originPrice = (int)Decimal.Parse(listproductHaravan.products[i].variants[j].compare_at_price.ToString(), cultureInfo);
                            }
                            catch (FormatException)
                            {
                            }
                            try
                            {
                                price = (int)Decimal.Parse(listproductHaravan.products[i].variants[j].price.ToString(), cultureInfo);
                            }
                            catch (FormatException ex)
                            {
                                Log.Error("HARAVAN : Parse discounted_price Error. " + listproductHaravan.products[i].variants[0].price + "/r/n" + ex);
                                continue;
                            }
                            if (price <= 0)
                            {
                                Log.Error("HARAVAN : Product price equal = 0" + "Product: ID " + tmpProduct.ID + " - " + tmpProduct.Name);
                                continue;
                            }
                            if (originPrice > price)
                            {
                                tmpProduct.OriginPrice = originPrice;
                                tmpProduct.Price       = price;
                            }
                            else
                            {
                                tmpProduct.OriginPrice = tmpProduct.Price = price;
                            }

                            //Categories
                            tmpProduct.Categories = new List <string>();
                            tmpProduct.Categories.Add(company.Domain);
                            tmpProduct.Categories.Add(listproductHaravan.products[i].product_type);
                            tmpProduct.IDCategories = Common.GetIDClassification(Common.ConvertToString(tmpProduct.Categories, " -> "));

                            // list Url Images
                            if (checkImage)
                            {
                                //check image_id của variant

                                int image_id = Common.Obj2Int(listproductHaravan.products[i].variants[j].image_id);
                                //nếu != 0 thì tìm values trong dicImage tương ứng
                                if (image_id != 0)
                                {
                                    string imageurl = dicImage.Where(kvp => kvp.Key == image_id).Select(kvp => kvp.Value).FirstOrDefault();
                                    if (!string.IsNullOrEmpty(imageurl))
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            imageurl
                                        }
                                    }
                                    ;
                                    else
                                    {
                                        tmpProduct.ImageUrls = new List <string>()
                                        {
                                            listproductHaravan.products[i].images[0].src
                                        }
                                    };
                                }
                                // nếu = 0 (null) thì lấy ảnh mặc định images[0].src
                                else
                                {
                                    tmpProduct.ImageUrls = new List <string>()
                                    {
                                        listproductHaravan.products[i].images[0].src
                                    }
                                };
                            }
                            else
                            {
                                tmpProduct.ImageUrls = new List <string>();
                            }
                            if (listproductHaravan.products[i].variants[0] != null)
                            {
                                tmpProduct.VATStatus = listproductHaravan.products[i].variants[0].taxable == false
                                    ? Common.VATStatus.NotVAT
                                    : Common.VATStatus.HaveVAT;
                            }
                            else
                            {
                                tmpProduct.VATStatus = Common.VATStatus.UndefinedVAT;
                            }

                            ListProducts.Add(tmpProduct);
                            #endregion
                        }
                    }
                    else if (variant < 1)
                    {
                        Log.ErrorFormat("HARAVAN : Error : Variant of {0}  = 0", listproductHaravan.products[i].title);
                    }
                }
                #endregion
            }
            return(ListProducts);
        }