Ejemplo n.º 1
0
        void doDowload(Object id)
        {
            DBToolTableAdapters.View_SanPhamImageTableAdapter adt = new DBToolTableAdapters.View_SanPhamImageTableAdapter();
            DBTool.View_SanPhamImageDataTable dt = new DBTool.View_SanPhamImageDataTable();
            adt.Connection.ConnectionString = QT.Entities.Server.ConnectionString;
            if (adt.Connection.State == ConnectionState.Closed)
            {
                adt.Connection.Open();
            }
            long idCompany           = 0;
            bool isdownloadImageNhap = false;

            this.Invoke((MethodInvoker) delegate
            {
                this.laMess1.Text   = String.Format("Start download data");
                idCompany           = QT.Entities.Common.Obj2Int64(id);
                isdownloadImageNhap = chkImageAdmin.Checked;
            });
            switch (cboCommand.SelectedIndex)
            {
            case 0:     //Current Not Valid
                adt.FillBy_ChuaKiemTraTheoCongTy(dt, idCompany);
                break;

            case 1:     //Current All
                adt.FillBy_AllByCompany(dt, idCompany);
                break;

            case 2:     //All Check not valid
                adt.FillBy_ChuaKiemTraTheoCongTy(dt, idCompany);
                //DBTool.View_SanPhamImageDataTable dt1 = new DBTool.View_SanPhamImageDataTable();
                //foreach (DBTool.CompanyRow dr in this.dBTool.Company)
                //{
                //    if (dr.CheckD == true)
                //    {
                //        this.Invoke((MethodInvoker)delegate
                //        {
                //            this.laMess1.Text = String.Format("start download {0}", dr.Domain);
                //        });
                //        adt.FillBy_ChuaKiemTraTheoCongTy(dt1, dr.ID);
                //        dt.Merge(dt1);
                //    }
                //}
                //dt.AcceptChanges();
                break;

            case 3:     //All Check
                adt.FillBy_AllByCompany(dt, idCompany);
                //DBTool.View_SanPhamImageDataTable dt2 = new DBTool.View_SanPhamImageDataTable();
                //foreach (DBTool.CompanyRow dr in this.dBTool.Company)
                //{
                //    if (dr.CheckD == true)
                //    {
                //        this.Invoke((MethodInvoker)delegate
                //        {
                //            this.laMess1.Text = String.Format("start download {0}", dr.Domain);
                //        });
                //        adt.FillBy_AllByCompany(dt2, dr.ID);
                //        dt.Merge(dt2);
                //    }
                //}
                //dt.AcceptChanges();
                break;

            case 4:     //Config ananytic
                DBTool.View_SanPhamImageDataTable dt3 = new DBTool.View_SanPhamImageDataTable();
                this.Invoke((MethodInvoker) delegate
                {
                    this.laMess1.Text = String.Format("start download {0}", "quangtrung.vn");
                });
                if (isdownloadImageNhap)
                {
                    // Update ảnh ở bảng ProductImage sang

                    adt.FillBy_SanPhamGocCoAnhNhap(dt3, QT.Entities.Common.GetIDCompany("quangtrung.vn"));
                }
                else
                {
                    adt.FillBy_SPConfigAnanytic(dt3, QT.Entities.Common.GetIDCompany("quangtrung.vn"));
                }
                dt.Merge(dt3);
                dt.AcceptChanges();
                break;
            }
            this.Invoke((MethodInvoker) delegate
            {
                this.laMess1.Text = String.Format("download finish");
            });
            String path = "";

            path = "D:\\Beta\\img\\";
            path = QT.Entities.Common.FolderImage;
            DBToolTableAdapters.ProductImagePathTableAdapter adtPro = new DBToolTableAdapters.ProductImagePathTableAdapter();
            adtPro.Connection.ConnectionString = QT.Entities.Server.ConnectionString;
            if (adtPro.Connection.State == ConnectionState.Closed)
            {
                adtPro.Connection.Open();
            }

            int count = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                try
                {
                    String url      = dt.Rows[i]["DetailUrl"].ToString();
                    Uri    root     = new Uri(url);
                    String namepath = root.DnsSafeHost.Replace("www.", "").Replace('.', '_');
                    //namepath = Common.UnicodeToKoDauAndGach(namepath.Replace("  ", " ").Replace("  ", " ").Trim());
                    if (cboCommand.SelectedIndex == 4)
                    {
                        namepath = "images";
                    }
                    string ss       = namepath + "\\";
                    string filename = QT.Entities.Common.UnicodeToKoDauAndGach(dt.Rows[i]["Name"].ToString());
                    string tempf    = filename.Replace("-", "");
                    string s3       = tempf.Substring(0, 3);
                    if (s3 == "bin")
                    {
                        s3 = "bin1";
                    }
                    ss += s3 + "\\";
                    if (filename.Length > 100)
                    {
                        filename = filename.Substring(0, 99);
                    }

                    String fullfile = path + ss + filename + ".jpg";

                    if (chkRedownload.Checked == true)
                    {
                        //Bitmap thumbBitmap = null;
                        //try
                        //{
                        //    thumbBitmap = new Bitmap(fullfile);
                        //    string pathsave = ss + filename + ".jpg";
                        //    pathsave = "Store/" + pathsave.Replace('\\', '/');
                        //    adtPro.UpdateQuery(pathsave, thumbBitmap.Width, thumbBitmap.Height, QT.Entities.Common.Obj2Int64(dt.Rows[i]["ID"].ToString()));
                        //}
                        //catch (Exception ex)
                        //{
                        //    this.Invoke((MethodInvoker)delegate
                        //    {
                        //        this.laMess1.Text = String.Format("Error {0}/{1}{2}", i, dt.Rows.Count, ex);
                        //    });
                        //}
                    }
                    else
                    {
                        if (!File.Exists(fullfile))
                        {
                            /// chưa có file --> tải lại dữ liệu
                            int    w = 0, h = 0;
                            string fileSave = QT.Entities.Common.SaveFile(dt.Rows[i]["ImageUrls"].ToString(), path + ss, filename + ".jpg", out w, out h, false);
                            count++;
                            if (fileSave != "")
                            {
                                string pathsave = ss + filename.Replace('\\', '/') + ".jpg";
                                pathsave = "Store/" + pathsave.Replace('\\', '/');
                                adtPro.UpdateQuery(pathsave, w, h, QT.Entities.Common.Obj2Int64(dt.Rows[i]["ID"].ToString()));
                                count++;
                            }
                        }
                        else
                        {
                            if (dt.Rows[i]["ImagePath"].ToString() == "")
                            {
                                Bitmap thumbBitmap = null;
                                try
                                {
                                    thumbBitmap = new Bitmap(fullfile);
                                    string pathsave = ss + filename + ".jpg";
                                    pathsave = "Store/" + pathsave.Replace('\\', '/');
                                    adtPro.UpdateQuery(pathsave, thumbBitmap.Width, thumbBitmap.Height, QT.Entities.Common.Obj2Int64(dt.Rows[i]["ID"].ToString()));
                                }
                                catch (Exception ex)
                                {
                                    this.Invoke((MethodInvoker) delegate
                                    {
                                        this.laMess1.Text = String.Format("Error {0}/{1}{2}", i, dt.Rows.Count, ex);
                                    });
                                }
                            }
                        }
                    }
                    this.Invoke((MethodInvoker) delegate
                    {
                        this.laMess1.Text = String.Format("{0}/{1} download:{2} - {3}\n{4}", i, dt.Rows.Count, count, dt.Rows[i]["Name"].ToString(), dt.Rows[i]["DetailUrl"].ToString());
                    });
                }
                catch (Exception ex)
                {
                    this.Invoke((MethodInvoker) delegate
                    {
                        this.laMess1.Text = String.Format("{0}/{1} download:{2} - {3}\n{4}\n{5}", i, dt.Rows.Count, count, dt.Rows[i]["Name"].ToString(), dt.Rows[i]["DetailUrl"].ToString(), ex.ToString());
                        Thread.Sleep(5000);
                    });
                }
            }
            adtPro.Dispose();
            dt.Clear();
            dt.Dispose();
            adt.Dispose();
        }
Ejemplo n.º 2
0
        void doDowload()
        {
            QT.Moduls.Tool.DBToolTableAdapters.View_SanPhamImageTableAdapter adt = new QT.Moduls.Tool.DBToolTableAdapters.View_SanPhamImageTableAdapter();
            DBTool.View_SanPhamImageDataTable dt = new DBTool.View_SanPhamImageDataTable();
            adt.Connection.ConnectionString = QT.Entities.Server.ConnectionString;
            if (adt.Connection.State == ConnectionState.Closed)
            {
                adt.Connection.Open();
            }

            this.Invoke((MethodInvoker) delegate
            {
                this.laMess1.Text = String.Format("Start download du lieu tu sql");
            });

            if (Common.DowloadFullImage)
            {
                //fix riêng cho lazada : download toàn bộ ảnh cho sản phẩm valid = 1
                //if (_idCompany == 3502170206813664485)
                //{
                //    adt.FillBy_CompanyValid1(dt, _idCompany);
                //}
                //else
                if (isDatafeed)
                {
                    adt.FillBy_CompanyValid1(dt, _idCompany);
                }
                else
                {
                    adt.FillBy_AllByCompany(dt, _idCompany);
                }
            }
            else
            {
                //15.10.2015 - Hải
                //fix riêng cho lazada chỉ load những ảnh sản phẩm valid = 1 và imagepath is null or ''
                //if (_idCompany == 3502170206813664485)
                //{
                //    adt.FillBy_CompanyValid1ImagePathNull(dt, _idCompany);
                //}
                //else
                if (isDatafeed)
                {
                    adt.FillBy_CompanyValid1ImagePathNull(dt, _idCompany);
                }
                else
                {
                    adt.FillBy_ChuaKiemTraTheoCongTy(dt, _idCompany);
                }
            }

            this.Invoke((MethodInvoker) delegate
            {
                this.laMess1.Text = String.Format("download du lieu san pham tu sql finish");
            });
            String path = "D:\\Image\\";

            path = Common.FolderImage;
            //if (_bDownloadF)
            //{
            //    path = path.Replace("D:", "F:") + "big\\";
            //}

            QT.Moduls.Tool.DBToolTableAdapters.ProductImagePathTableAdapter adtPro = new QT.Moduls.Tool.DBToolTableAdapters.ProductImagePathTableAdapter();
            adtPro.Connection.ConnectionString = QT.Entities.Server.ConnectionString;
            if (adtPro.Connection.State == ConnectionState.Closed)
            {
                adtPro.Connection.Open();
            }

            int count = 0;

            Log.InfoFormat("path = ", path);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string url = string.Empty;
                try
                {
                    url = dt.Rows[i]["DetailUrl"].ToString();
                    Uri    root     = new Uri(url);
                    string domain   = root.DnsSafeHost.Replace("www.", "");
                    string char1    = domain.Substring(0, 1);
                    String namepath = domain.Substring(0, 1) + "\\" + root.DnsSafeHost.Replace("www.", "").Replace('.', '_');
                    string ss       = namepath + "\\";
                    string filename = Common.UnicodeToKoDauAndGach(dt.Rows[i]["Name"].ToString());
                    string tempf    = filename.Replace("-", "");
                    string sf       = tempf.Substring(0, 3);
                    if (sf == "bin")
                    {
                        sf = "bin1";
                    }
                    if (sf == "con")
                    {
                        sf = "con1";
                    }
                    ss += sf + "\\";
                    if (filename.Length > 100)
                    {
                        filename = filename.Substring(0, 99);
                    }
                    String fullfile = path + ss + filename + ".jpg";

                    if (chkRedownload.Checked == true)
                    {
                        Bitmap thumbBitmap = null;
                        try
                        {
                            thumbBitmap = new Bitmap(fullfile);
                            string pathsave = ss + filename + ".jpg";

                            pathsave = "Store/" + pathsave.Replace('\\', '/');
                        }
                        catch (Exception ex)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                this.laMess1.Text = String.Format("Error {0}/{1}{2}", i, dt.Rows.Count, ex);
                                //Thread.Sleep(5000);
                            });
                        }
                    }
                    else
                    {
                        if (!File.Exists(fullfile))
                        {
                            Log.InfoFormat("/// chưa có file --> tải lại dữ liệu");
                            /// chưa có file --> tải lại dữ liệu
                            int    w = 0, h = 0;
                            string fileSave = Common.SaveFile(dt.Rows[i]["ImageUrls"].ToString(), path + ss, filename + ".jpg", out w, out h, false);
                            if (fileSave != "")
                            {
                                string pathsave = ss + filename.Replace('\\', '/') + ".jpg";
                                pathsave = "Store/" + pathsave.Replace('\\', '/');
                                adtPro.UpdateQuery(pathsave, w, h, Common.Obj2Int64(dt.Rows[i]["ID"].ToString()));
                                count++;
                            }
                        }
                        else
                        {
                            /// Nếu chưa update ảnh vào hệ thống  thì update

                            Bitmap thumbBitmap = null;
                            try
                            {
                                thumbBitmap = new Bitmap(fullfile);
                                string pathsave = ss + filename + ".jpg";
                                pathsave = "Store/" + pathsave.Replace('\\', '/');

                                adtPro.UpdateQuery(pathsave, thumbBitmap.Width, thumbBitmap.Height, Common.Obj2Int64(dt.Rows[i]["ID"].ToString()));
                            }
                            catch (Exception ex)
                            {
                                this.Invoke((MethodInvoker) delegate
                                {
                                    this.laMess1.Text = String.Format("Error {0}/{1}{2}", i, dt.Rows.Count, ex);
                                });
                            }
                        }
                    }
                    this.Invoke((MethodInvoker) delegate
                    {
                        this.laMess1.Text = String.Format("{0}/{1} download:{2} - {3}\n{4}", i, dt.Rows.Count, count, dt.Rows[i]["Name"].ToString(), dt.Rows[i]["DetailUrl"].ToString());
                    });
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Links : {0} ERROR : {1}.  ", url, ex);
                    this.Invoke((MethodInvoker) delegate
                    {
                        this.laMess1.Text = ex.ToString();
                        //Thread.Sleep(1000);
                    });
                }
            }
            adtPro.Dispose();
            dt.Clear();
            dt.Dispose();
            adt.Dispose();
            this.Invoke((MethodInvoker) delegate
            {
                this.laMess1.Text = "Finish";
                this.WindowState  = FormWindowState.Minimized;
                if (FinishClose)
                {
                    this.Close();
                }
            });
        }