using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;

public partial class WTOSYSadmin_news_advertisementdo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        string pic1 = "", pic2 = "", pic3 = "", pic4 = "", pic5 = "", pic6 = "", spic1 = "", spic2 = "", spic3 = "", spic4 = "", spic5 = "", spic6 = "",dlpic1="",dlpic2="",pdpic1="";
        string id = Request.QueryString["id"].ToString().Trim();
        string doing = Request.QueryString["doing"].ToString().Trim();
        string type = Request.QueryString["type"].ToString().Trim();
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        #region 开通与关闭
        switch (doing)
        { 
            case "open":
                switch (type)
                {
                    case "0":
                        #region 轮显广告
                        SqlCommand smd = new SqlCommand("select * from Advertisement where type='0' and id=" + id, conn);
                        SqlDataReader sdr = smd.ExecuteReader();
                        sdr.Read();
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/xml/bcastr.xml"));
                        XmlNodeList nodeList = xmlDoc.SelectSingleNode("bcaster").ChildNodes;//获取bookstore节点的所有子节点
                        foreach (XmlNode xn in nodeList)//遍历所有子节点
                        {
                            XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                            XmlElement xe2 = (XmlElement)xn;
                            switch (xe2.GetAttribute("item_url"))
                            {
                                case "images/01.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(13));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(20));
                                    FileInfo p1 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/01.jpg"));
                                    pic1 = DateTime.Now.ToString("yyyyMMddHHmmssffffMMyyyy") + ".jpg";
                                    p1.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic1);
                                    FileInfo pp1 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(7)));
                                    pp1.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/01.jpg"));
                                    break;
                                case "images/02.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(14));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(13));
                                    FileInfo p2 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/02.jpg"));
                                    pic2 = DateTime.Now.ToString("yyyyMMddHHmmssffffMMssss") + ".jpg";
                                    p2.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic2);
                                    FileInfo pp2 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(8)));
                                    pp2.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/02.jpg"));
                                    break;
                                case "images/03.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(15));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(21));
                                    FileInfo p3 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/03.jpg"));
                                    pic3 = DateTime.Now.ToString("yyyyMMddHHmmssffffMM") + ".jpg";
                                    p3.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic3);
                                    FileInfo pp3 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(9)));
                                    pp3.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/03.jpg"));
                                    break;
                                case "images/04.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(16));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(22));
                                    FileInfo p4 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/04.jpg"));
                                    pic4 = DateTime.Now.ToString("yyyyMMddHHmmssffffMMffff") + ".jpg";
                                    p4.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic4);
                                    FileInfo pp4 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(10)));
                                    pp4.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/04.jpg"));
                                    break;
                                case "images/05.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(17));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(23));
                                    FileInfo p5 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/05.jpg"));
                                    pic5 = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                                    p5.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic5);
                                    FileInfo pp5 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(11)));
                                    pp5.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/05.jpg"));
                                    break;
                                case "images/06.jpg":
                                    xe2.SetAttribute("link", sdr.GetString(18));
                                    xe2.SetAttribute("itemtitle", sdr.GetString(24));
                                    FileInfo p6 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/06.jpg"));
                                    pic6 = DateTime.Now.ToString("yyyyMMddHHmmssssyyyy") + ".jpg";
                                    p6.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/") + pic6);
                                    FileInfo pp6 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/" + sdr.GetString(12)));
                                    pp6.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/images/06.jpg"));
                                    break;
                            }

                        }

                        xmlDoc.Save(Server.MapPath("~/WebMain/news/Advertisement/lxAdvertisement/xml/bcastr.xml"));//保存
                        sdr.Close();
                        Doadmin.Exec("update  Advertisement set Condition='n',picurl1='" + pic1 + "',picurl2='" + pic2 + "',picurl3='" + pic3 + "',picurl4='" + pic4 + "',picurl5='" + pic5 + "',picurl6='" + pic6 + "' where Condition='y' and type='0'");
                        if (Doadmin.Exec("update Advertisement set Condition='y',picurl1='01.jpg',picurl2='02.jpg',picurl3='03.jpg',picurl4='04.jpg',picurl5='05.jpg',picurl6='06.jpg' where id=" + id))
                        {

                            Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');location.href('../news/Advertisement.aspx')</script>");
                        }
                        #endregion
                        break;
                    case "1":

                        #region 对联广告
                        SqlCommand  smd5=new SqlCommand ("select count(*) from Advertisement where type='1'and Condition='y'", conn);
                        int countdl=Convert.ToInt32(smd5.ExecuteScalar());
                        if(countdl!=0)
                        {
                        SqlCommand smd2 = new SqlCommand("select * from Advertisement where type='1' and id=" + id, conn);
                        SqlDataReader sdr2 = smd2.ExecuteReader();
                        sdr2.Read();
                        FileInfo dl1 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/left.jpg"));
                        dlpic1 = DateTime.Now.ToString("yyyyMMddHHmmssffffMMyyyy") + ".jpg";
                        dl1.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/") + dlpic1);
                        FileInfo pp8 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/" + sdr2.GetString(7)));
                        pp8.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/left.jpg"));
                        FileInfo dl2 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/right.jpg"));
                        dlpic2 = DateTime.Now.ToString("yyyyMMssssddHHmmssffffMMyyyy") + ".jpg";
                        dl2.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/") + dlpic2);
                        FileInfo pp9 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/" + sdr2.GetString(8)));
                        pp9.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/right.jpg"));
                        sdr2.Close();
                        Doadmin.Exec("update  Advertisement set Condition='n',picurl1='" + dlpic1 + "',picurl2='" + dlpic2 + "' where Condition='y' and type='1'");
                        if (Doadmin.Exec("update Advertisement set Condition='y',picurl1='left.jpg',picurl2='right.jpg' where id=" + id))
                        {

                            Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');location.href('../news/Advertisement.aspx')</script>");
                        }
                        }
                        else
                        {
                            SqlCommand cmd911 = new SqlCommand("select * from Advertisement where type='1' and id="+id, conn);
                            SqlDataReader sdr911 = cmd911.ExecuteReader();
                            sdr911.Read();
                            FileInfo p911 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/" + sdr911.GetString(7)));
                            p911.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/left.jpg"));
                            FileInfo p912 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/" + sdr911.GetString(8)));
                            p912.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/right.jpg"));
                            sdr911.Close();
                            Doadmin.Exec("update Advertisement set Condition='y',picurl1='left.jpg',picurl2='right.jpg' where id=" + id);
                            Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                         }
                        #endregion
                        break;
                    case "2":
                        #region 漂动广告
                        SqlCommand smd6 = new SqlCommand("select count(*) from Advertisement where type='2'and Condition='y'", conn);
                        int countpd = Convert.ToInt32(smd6.ExecuteScalar());
                        if (countpd != 0)
                        {
                            SqlCommand smd7 = new SqlCommand("select * from Advertisement where type='2' and id=" + id, conn);
                            SqlDataReader sdr7 = smd7.ExecuteReader();
                            sdr7.Read();

                            FileInfo pd1 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/pic.jpg"));
                            pdpic1 = DateTime.Now.ToString("yyyyMMddHHmmssffffMMssss") + ".jpg";
                            pd1.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/") + pdpic1);

                            FileInfo pd8 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/" + sdr7.GetString(7)));
                            pd8.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/pic.jpg"));

                            Doadmin.Exec("update  Advertisement set Condition='n',picurl1='" + pdpic1 + "' where Condition='y' and type='2'");
                            if (Doadmin.Exec("update Advertisement set Condition='y',picurl1='pic.jpg' where id=" + id))
                            {

                                Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                            }
                            else
                            {
                                Response.Write("<script>alert('此次操作失败');location.href('../news/Advertisement.aspx')</script>");
                            }
                            sdr7.Close();
                        }
                        else
                        {
                            SqlCommand cmdpd = new SqlCommand("select * from Advertisement where type='2' and id=" + id, conn);
                            SqlDataReader sdrpd = cmdpd.ExecuteReader();
                            sdrpd.Read();
                            FileInfo pd911 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/" + sdrpd.GetString(7)));
                            pd911.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/pic.jpg"));
                            sdrpd.Close();
                            Doadmin.Exec("update Advertisement set Condition='y',picurl1='pic.jpg' where id=" + id);
                            Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        #endregion
                        break;
                    case "3":
                        #region 弹出广告
                        if (Doadmin.Exec("update Advertisement set Condition='y' where id=" + id))
                        {

                            Response.Write("<script>alert('发布成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次操作失败');location.href('../news/Advertisement.aspx')</script>");
                        }
                        #endregion
                        break;
                }

                conn.Close();
                break;
            case "close":
                  switch(type)
                  {
                      case "0":
                       Response.Write("<script>alert('数据不完整，请保留一个轮显广告方案。');location.href('../news/Advertisement.aspx')</script>");
                       break;
                      case "1":

                          FileInfo dl11 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/left.jpg"));
                          string  dlpic11 = DateTime.Now.ToString("yyyyMMssssddHHmmssffffMMyyyy") + ".jpg";
                          dl11.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/") + dlpic11);
                          FileInfo dl12= new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/right.jpg"));
                          string   dlpic12 = DateTime.Now.ToString("yyyyMMssssddssssmmffffMM") + ".jpg";
                          dl12.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/dlAdvertisement/ad/") + dlpic12);
                        if(Doadmin.Exec("update Advertisement set Condition='n',picurl1='"+dlpic11+"',picurl2='"+dlpic12+"' where Condition='y' and type='1'"))
                        {
                          Response.Write("<script>alert('操作完成');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                         Response.Write("<script>alert('此次提交失败');location.href('../news/Advertisement.aspx')</script>");
                        }
                          break;
                      case "2":
                          FileInfo pd11 = new FileInfo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/pic.jpg"));
                          string pd191 = DateTime.Now.ToString("yyyyMMssssddHHmmssffffMMyyyy") + ".jpg";
                          pd11.MoveTo(Server.MapPath("~/WebMain/news/Advertisement/pdAdvertisement/images/") + pd191);
                          if (Doadmin.Exec("update Advertisement set Condition='n',picurl1='" + pd191 + "' where Condition='y' and type='2'"))
                          {
                              Response.Write("<script>alert('操作完成');location.href('../news/Advertisement.aspx')</script>");
                          }
                          else
                          {
                              Response.Write("<script>alert('此次提交失败');location.href('../news/Advertisement.aspx')</script>");
                          }
                          break;
                      case "3":
                          if (Doadmin.Exec("update Advertisement set Condition='n' where type='3' and id="+id))
                          {
                              Response.Write("<script>alert('操作完成');location.href('../news/Advertisement.aspx')</script>");
                          }
                          else
                          {
                              Response.Write("<script>alert('此次提交失败');location.href('../news/Advertisement.aspx')</script>");
                          }
                          break;
                   
                   }
                break;
            case "del":
                switch(type)
                {
                case "0":
                SqlCommand smddel = new SqlCommand("select Condition from Advertisement where id="+Request.QueryString["id"].ToString().Trim(),conn);
                string isopen =Convert.ToString(smddel.ExecuteScalar());
                if (isopen == "n")
                {
                    Doadmin.Exec("delete from Advertisement where id=" + Request.QueryString["id"]);
                    Response.Write("<script>alert('删除成功');location.href('../news/Advertisement.aspx')</script>");
                }
                else
                {
                    Response.Write("<script>alert('数据不完整，请保留一个轮显广告方案。');location.href('../news/Advertisement.aspx')</script>");
                }
                break;
                    case "1":
                        
                        if(Doadmin.Exec("delete from Advertisement where id=" + Request.QueryString["id"]))
                        {
                        Response.Write("<script>alert('删除成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                        Response.Write("<script>alert('此次提交不成功');location.href('../news/Advertisement.aspx')</script>"); 
                        }
                        break;
                    case "2":

                        if (Doadmin.Exec("delete from Advertisement where id=" + Request.QueryString["id"]))
                        {
                            Response.Write("<script>alert('删除成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次提交不成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        break;
                    case "3":
                        if (Doadmin.Exec("delete from Advertisement where id=" + Request.QueryString["id"]))
                        {
                            Response.Write("<script>alert('删除成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('此次提交不成功');location.href('../news/Advertisement.aspx')</script>");
                        }
                        break;



                }
                    break;
            }
            #endregion


        }
}
