using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Maticsoft.DBUtility;
using AndroidSevice.BLL;

public partial class Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DbHelperSQL stock = new DbHelperSQL();
        stock.connectionString = PubConstant.ConnectionString + " ;database=" + Session["DbName"].ToString();
        string basePath = Server.MapPath("~/PtypeImages/");
        string name = string.Empty;
        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        //如果目录不存在，则创建目录
        if (files != null)
        {
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            if (Session["ImagesPageType"].ToString() == "ptypeimages")//商品图片上传
            {
                for (int i = 0; i < files.Count; i++)
                {
                    files[i].SaveAs(basePath + files[i].FileName);
                    int imagenum = int.Parse(Session["imagecountnum"].ToString());
                    Session["imageindexnum"] = int.Parse(Session["imageindexnum"].ToString()) + 1;
                    for (int j = int.Parse(Session["imageindexnum"].ToString()); j <= imagenum; j++)
                    {
                        //保存图片到数据库中
                        Woolinte_PtypeImage wpi = new Woolinte_PtypeImage();
                        if (wpi.IfExists(stock, Session["uploadtypeid_"].ToString(), Session["imageindexnum"].ToString()))
                        {
                            FileStream fs = new FileStream(basePath + files[i].FileName, FileMode.Open);
                            byte[] imageBytes = new byte[fs.Length];
                            BinaryReader br = new BinaryReader(fs);
                            imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
                            // int count = dal.UpdateSeal(ptypeid, "1", imageBytes, pictureBox1.ImageLocation.Trim());
                            int count = wpi.UpdateSeal(stock, Session["uploadtypeid_"].ToString(), Session["imageindexnum"].ToString(), imageBytes);
                            if (count > 0)
                            {
                                // MessageBox.Show("保存成功！");
                            }
                            else
                            {
                                // MessageBox.Show("图片一保存失败！");
                            }
                            fs.Dispose();
                        }
                        else
                        {
                            FileStream fs = new FileStream(basePath + files[i].FileName, FileMode.Open);
                            byte[] imageBytes = new byte[fs.Length];
                            BinaryReader br = new BinaryReader(fs);
                            imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
                            int count = wpi.AddSeal(stock, Session["uploadtypeid_"].ToString(), Session["imageindexnum"].ToString(), imageBytes);
                            if (count > 0)
                            {
                                //  MessageBox.Show("保存成功！");
                            }
                            else
                            {
                                // MessageBox.Show("图片一保存失败！");
                            }
                            fs.Dispose();
                        }
                    }
                }
                //
            }//商品图片
            else if (Session["ImagesPageType"].ToString() == "Sealimages")//公章上传
            {
                files[0].SaveAs(basePath + files[0].FileName);
                FileStream fs = new FileStream(basePath + files[0].FileName, FileMode.Open);
                byte[] imageBytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
                string strSql = string.Format("delete from Woolinte_Seal; insert into Woolinte_Seal (ImagesAddress) values (@image)");
                int count = stock.Write(strSql, imageBytes);
                if (count > 0)
                {

                }
                else
                {

                }
            }
            else if (Session["ImagesPageType"].ToString() == "UploadMainImage")     //用户配置 上传首页图片 经销商版
            {
                for (int i = 0; i < files.Count; i++)
                {
                    files[i].SaveAs(basePath + files[i].FileName);
                    int imagenum = int.Parse(Session["imagecountnum"].ToString());
                    Session["imageindexnum"] = int.Parse(Session["imageindexnum"].ToString()) + 1;
                    for (int j = int.Parse(Session["imageindexnum"].ToString()); j <= imagenum; j++)
                    {
                        //保存图片到数据库中
                        AndroidSevice.BLL.Woolinte_UserPowerBLL dal = new AndroidSevice.BLL.Woolinte_UserPowerBLL();
                        FileStream fs = new FileStream(basePath + files[i].FileName, FileMode.Open);
                        byte[] imageBytes = new byte[fs.Length];
                        BinaryReader br = new BinaryReader(fs);
                        imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
                        int count = dal.UpdateSeal(stock, imageBytes, Session["imageindexnum"].ToString(), imagenum);
                        if (count > 0)
                        {
                            // MessageBox.Show("保存成功！");
                        }
                        else
                        {
                            // MessageBox.Show("图片一保存失败！");
                        }
                        fs.Dispose();
                    }
                }
                //
            }
            else if (Session["ImagesPageType"].ToString() == "UploadMainLog")     //用户配置 上传LOGO图片 经销商版
            {
                files[0].SaveAs(basePath + files[0].FileName);
                FileStream fs = new FileStream(basePath + files[0].FileName, FileMode.Open);
                byte[] imageBytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
                string strSql = string.Format(" update  Woolinte_DealersSysdata set TypeImage=@TypeImage  where TypeName='UploadMainLog'");
                int count = stock.WriteImage(strSql, imageBytes);
            }
             
        }
    }
}