busChange

来源:互联网 发布:苹果mac如何复制粘贴 编辑:程序博客网 时间:2024/06/16 21:46

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Xml;
namespace Ticket.Web.Application.Bus
{
    public partial class BusChange : System.Web.UI.Page
    {
        public string City = "上海";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.txtEnd.Visible = false;
                this.LabTo.Visible = false;
                // 在此处放置用户代码以初始化页面
                string CurrentPath = this.Server.MapPath(".");


                if (!Page.IsPostBack)
                {
                    if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
                    {
                        this.DropDownListProvince.Items.Clear();
                        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                        doc.Load(CurrentPath + "//Province.xml");

                        XmlNodeList nodes = doc.DocumentElement.ChildNodes;
                        XmlNode node1 = doc.DocumentElement.SelectSingleNode(@"Province/City[@Name='" + this.City + "']");

                        foreach (XmlNode node in nodes)
                        {
                            this.DropDownListProvince.Items.Add(node.Attributes["Name"].Value);
                            int n = this.DropDownListProvince.Items.Count - 1;

                            if (node1 != null && node == node1.ParentNode)
                                this.DropDownListProvince.SelectedIndex = n;
                        }
                        DropDownListProvince_SelectedIndexChanged(sender, e);
                    }
                    else
                    {
                        AppCode.MessageBox.Show("地市信息文件丢失!");
                    }
                }
            }
        }
        protected void DropDownListProvince_SelectedIndexChanged(object sender, EventArgs e)
        {

            string CurrentPath = this.Server.MapPath(".");

            if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
            {
                this.DropDownListCity.Items.Clear();
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(CurrentPath + "//Province.xml");

                XmlNodeList nodes = doc.DocumentElement.ChildNodes[this.DropDownListProvince.SelectedIndex].ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    this.DropDownListCity.Items.Add(node.Attributes["Name"].Value);
                    int n = this.DropDownListCity.Items.Count - 1;
                    if (node.Attributes["Name"].Value == this.City)
                    {
                        this.DropDownListCity.SelectedIndex = n;
                    }
                }

                if (this.DropDownListCity.SelectedIndex == -1)
                    this.DropDownListCity.SelectedIndex = 0;
            }
            else
            {
                AppCode.MessageBox.Show("地市信息文件丢失!");
            }
            //this.tab.Style.Add("display", "none");
        }
        protected void radChange_CheckedChanged(object sender, EventArgs e)
        {
            if (radChange.Checked == true)
            {
                this.txtEnd.Visible = true;
                this.LabTo.Visible = true;
            }
            else
            {
                this.txtEnd.Visible = false;
                this.LabTo.Visible = false;
            }
        }

        protected void radStation_CheckedChanged(object sender, EventArgs e)
        {
            this.txtEnd.Visible = false;
            this.LabTo.Visible = false;
        }
        public DataTable NewTable(string busStation,string endStation)
        {
            string acity = this.DropDownListCity.SelectedValue.ToString();
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("Bus", typeof(string));
            dtNew.Columns.Add("DownBuslines", typeof(string));
            dtNew.Columns.Add("UpBuslines", typeof(string));
            dtNew.Columns.Add("Runtime", typeof(string));
            dtNew.Columns.Add("Tickets", typeof(string));
            dtNew.Columns.Add("StarSite", typeof(string));
            dtNew.Columns.Add("StopSite", typeof(string));
            dtNew.Columns.Add("Countsite", typeof(string));
            ArrayList altemp = new ArrayList();
            DataSet ds = new DataSet();
            if (endStation.ToString() != "")
            {
                 ds = Ticket.WebService.Bus.BusService.GetObject().GetChange(busStation, endStation, acity);
            }
            else
            {
                ds = Ticket.WebService.Bus.BusService.GetObject().GetBusTable(busStation, acity);
            }

           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (altemp.Contains(ds.Tables[0].Rows[i]["Bus"].ToString()))
                {
                }
                else
                {
                    DataTable dt = ds.Tables[0];
                    DataRow row1 = dtNew.NewRow();
                    row1["Bus"] = dt.Rows[i]["Bus"].ToString();
                    row1["DownBuslines"] = dt.Rows[i]["DownBuslines"].ToString();
                    row1["UpBuslines"] = dt.Rows[i]["UpBuslines"].ToString();
                    row1["Runtime"] = dt.Rows[i]["Runtime"].ToString();
                    row1["Tickets"] = dt.Rows[i]["Tickets"].ToString();
                    row1["StarSite"] = dt.Rows[i]["StarSite"].ToString();
                    row1["StopSite"] = dt.Rows[i]["StopSite"].ToString();
                    row1["Countsite"] = dt.Rows[i]["Countsite"].ToString();
                    dtNew.Rows.Add(row1);
                    altemp.Add(ds.Tables[0].Rows[i]["Bus"].ToString());
                }
            }
            return dtNew;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.RptChange.DataSource = null;
            this.RptChange.DataBind();
            this.rptBus.DataSource = null;
            this.rptBus.DataBind();
            this.lblMsg.Text = "";
            BindBus();
        }
       
        public void BindBus()
        {
            string star = this.txtXianlu.Text;
            string end = this.txtEnd.Text;
            if (this.radStation.Checked == true)
            {
                this.rptBus.DataSource = NewTable(this.txtXianlu.Text,"");
                this.rptBus.DataBind();
            }
            else
            {
                //如果有直达的
                DataTable dtNew = new DataTable();
                dtNew.Columns.Add("Bus", typeof(string));
                dtNew.Columns.Add("UpBuslines", typeof(string));
                dtNew.Columns.Add("Runtime", typeof(string));
                dtNew.Columns.Add("Tickets", typeof(string));
                dtNew.Columns.Add("StarSite", typeof(string));
                dtNew.Columns.Add("StopSite", typeof(string));
                dtNew.Columns.Add("Countsite", typeof(string));

                DataTable dt = NewTable(txtXianlu.Text,"");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string path = dt.Rows[i]["UpBuslines"].ToString();
                    if (path.Contains(txtXianlu.Text) && path.Contains(this.txtEnd.Text))
                    {//上行
                        DataRow row1 = dtNew.NewRow();
                        row1["Bus"] = dt.Rows[i]["Bus"].ToString();
                        row1["Runtime"] = dt.Rows[i]["Runtime"].ToString();
                        row1["Tickets"] = dt.Rows[i]["Tickets"].ToString();
                        row1["StarSite"] = dt.Rows[i]["StarSite"].ToString();
                        row1["StopSite"] = dt.Rows[i]["StopSite"].ToString();
                        row1["Countsite"] = dt.Rows[i]["Countsite"].ToString();
                        if (path.IndexOf(this.txtEnd.Text) - path.IndexOf(txtXianlu.Text) > 0)
                        {
                            row1["UpBuslines"] = dt.Rows[i]["UpBuslines"].ToString();
                        }
                        else
                        {
                            row1["UpBuslines"] = dt.Rows[i]["DownBuslines"].ToString();
                        }
                        dtNew.Rows.Add(row1);
                    }
                }
                this.lblMsg.Text = "乘下列任意车直达";
                this.RptChange.DataSource = dtNew;
                this.RptChange.DataBind();
                if (dtNew.Rows.Count == 0)
                {

                    //如果没有公共的就要换乘的
                    this.lblMsg.Text = "";
                    DataTable dtChange = NewTable(star, end);
                    DataTable dtFrom = dtChange.Clone();//创建存放所有经过起点的公交线路datable
                    DataTable dtEnd = dtChange.Clone();//创建存放所有经过终点的公交线路datable

                    DataRow[] drFroms = dtChange.Select("DownBuslines like '%" + star + "%'");//获取经过起点的公交线路的记录行的集合
                    DataRow[] drEnds = dtChange.Select("DownBuslines like '%" + end + "%'");//获取经过终点的公交线路的记录行的集合

                    foreach (DataRow dr in drFroms)
                    {
                        dtFrom.Rows.Add(dr.ItemArray);//复制数据
                    }

                    foreach (DataRow dr in drEnds)
                    {
                        dtEnd.Rows.Add(dr.ItemArray);//复制数据
                    }
                    GetSameSite(dtFrom, dtEnd);
                }
            }
        }
        /// <summary>
        /// 获取相交的路线      
        /// </summary>
        /// <param name="dtFrom">经过起始站的datatable</param>
        /// <param name="dtEnd">经过终点站的datatable</param>
        /// <returns></returns>
        private void GetSameSite(DataTable dtFrom, DataTable dtEnd)
        {
            DataTable dtPath = new DataTable();
            dtPath.Columns.Add("Car", typeof(string));//两个公交
            dtPath.Columns.Add("StarBus", typeof(string));//开始做那个
            dtPath.Columns.Add("scount", typeof(int));//起点到交点
            dtPath.Columns.Add("Jiaodian", typeof(string));//交点在那个地方
            dtPath.Columns.Add("ecount", typeof(int));//交点到终点
            dtPath.Columns.Add("EndBus", typeof(string));//最后做哪个
            dtPath.Columns.Add("count", typeof(int));//共做基站
            string path = "";
            foreach (DataRow dr in dtFrom.Rows)
            {
                string[] sites = dr["DownBuslines"].ToString().Split('-');//分段获取某条线路经过的所有站
                for (int i = 0; i < sites.Length; i++)
                {
                    DataRow[] drSes = dtEnd.Select("DownBuslines like '%" + sites[i].ToString() + "%'");
                    if (drSes.Length > 0)
                    {
                        for (int j=0;j<drSes.Length;j++)
                        {
                            string[] endSites = drSes[j]["DownBuslines"].ToString().Split('-');
                            int iPath = 0;
                            for (int x = 0; x < endSites.Length; x++)
                            {
                                if (endSites[x] == sites[i])
                                {
                                    iPath = x;
                                    break;
                                }
                            }
                          int snum = GetStation(this.txtXianlu.Text, sites[i].ToString(), sites);//起点到换乘点共几站
                          int enuma=  GetStation(this.txtEnd.Text, sites[i].ToString(), endSites);//换乘点到目的地共几站
                          int count = snum + enuma;//总共要做多少站
                          DataRow row1 = dtPath.NewRow();
                          row1["Car"] = dr["Bus"].ToString() + drSes[j]["Bus"].ToString();
                          row1["StarBus"] = dr["Bus"].ToString();
                          row1["scount"] = snum;
                          row1["Jiaodian"] = sites[i].ToString();
                          row1["EndBus"] = drSes[j]["Bus"].ToString();
                          row1["ecount"] =enuma;
                          row1["Count"] =count;
                          dtPath.Rows.Add(row1);
                          //path = path + "</br>" + "乘坐" + dr["Bus"].ToString() + "车 " + snum.ToString() + "站到" + sites[i].ToString() + "处换乘" + drSes[j]["Bus"] + "路车 坐" + enuma + "站下车";

                        }
                        continue;
                    }
                }
            }

            DataTable newtb = dtPath.Clone();

            for (int pc = 0; pc < dtPath.Rows.Count; pc++)
            {
                if (dtPath.Select("Car='" + dtPath.Rows[pc]["Car"] + "' and count <'" + dtPath.Rows[pc]["count"] + "'").Length == 0)
                {
                    DataRow row1 = newtb.NewRow();
                    row1["Car"] = dtPath.Rows[pc]["Car"];
                    row1["StarBus"] = dtPath.Rows[pc]["StarBus"];
                    row1["EndBus"] = dtPath.Rows[pc]["EndBus"];
                    row1["Count"] = dtPath.Rows[pc]["count"];
                    row1["scount"] = dtPath.Rows[pc]["scount"];
                    row1["ecount"] = dtPath.Rows[pc]["ecount"];
                    row1["Jiaodian"] = dtPath.Rows[pc]["Jiaodian"];
                    newtb.Rows.Add(row1);
                }
            }
            this.GridView1.DataSource = newtb;
            this.GridView1.DataBind();
        }
        /// <summary>
        ///  查询起点或目的点距交点的站数
        /// </summary>
        /// <param name="starStation">起点或重点</param>
        /// <param name="Station">交点</param>
        /// <param name="path">路线</param>
        /// <returns>站数</returns>
        public int GetStation(string starStation, string Station, string[] path)//  
        {
            int qjnum = 0;//起点到交点的站数  起点到  北京西路泰兴路
            int qznum = 0;//起点到所经过的站  起点到澳门路
            for (int i = 0; i < path.Length; i++)
            {
                if (path[i].ToString().Contains(starStation))
                {
                    qznum = i;
                }
                if (path[i].ToString().Contains(Station))
                {
                    qjnum = i;
                }
            }
            return Math.Abs(qznum - qjnum);

            //起点到交点的站数 起点到澳门路


        }
    }
}

原创粉丝点击