public void City_Get()
        {
            string jsonStr = string.Empty;
            HttpRequest req = Context.Request;

            Hashtable ht = new Hashtable();
            DB dbi = new DB(SystemConst.DBConnString);
            string sqlBase = @"SELECT * FROM dbo.City";
            try
            {
                string sql = sqlBase + " ORDER BY C_ID";
                ht.Add("rows", dbi.GetDataTable(sql));
            }
            catch (Exception ex)
            {
                ht.Add("err", ex.Message);
                if (!ht.ContainsKey("rows"))
                {
                    ht.Add("rows", dbi.GetDataTable(sqlBase + " WHERE 1=2"));
                }
            }

            jsonStr = JsonConvert.SerializeObject(ht);
            ResponseWrite(jsonStr);
        }
        public void Flight_Get()
        {
            string jsonStr = string.Empty;
            HttpRequest req = Context.Request;
            string strFromCity = req.Form["FromCity"];
            string strToCity = req.Form["ToCity"];
            string strDeparture = req.Form["Departure"];
            string sort = req.Form["sort"];
            int page = Convert.ToInt16(req.Form["page"] ?? "0");
            int rowsCnt = Convert.ToInt16(req.Form["rows"] ?? "20");
            string order = req.Form["order"];

            Hashtable ht = new Hashtable();
            DB dbi = new DB(SystemConst.DBConnString);
            string sqlBase = @"SELECT * FROM dbo.FlightInfo";
            string sqlCnt = @"SELECT COUNT(1) FROM dbo.FlightInfo";
            try
            {
                if (!string.IsNullOrEmpty(strFromCity) && !string.IsNullOrEmpty(strToCity) && !string.IsNullOrEmpty(strDeparture))
                {
                    string sqlCity = @"SELECT * FROM dbo.City WHERE C_CODE=@C_CODE";
                    City fromCity = EntityUtil.Create<City>(dbi.GetDataTable(sqlCity, this.InitSqlParams("C_CODE", strFromCity)).Rows[0]);
                    City toCity = EntityUtil.Create<City>(dbi.GetDataTable(sqlCity, this.InitSqlParams("C_CODE", strToCity)).Rows[0]);

                    string sqlWhere = @" WHERE datediff(MINUTE,C_ADD_TIME,getdate())<15 and C_From=@C_From and C_To=@C_To and datediff(day, C_Departure, @C_Departure)=0";
                    List<SqlParameter> lstParam = new List<SqlParameter>();
                    lstParam.Add(new SqlParameter("@C_From", fromCity.C_NAME));
                    lstParam.Add(new SqlParameter("@C_To", toCity.C_NAME));
                    lstParam.Add(new SqlParameter("@C_Departure", strDeparture));

                    StringBuilder sbdatasoure = new StringBuilder();
                    if (Convert.ToInt16(req.Form["QUNAR"] ?? "0") == 1)
                    {
                        sbdatasoure.Append(" OR C_DateSource='QUNAR'");
                        if (Convert.ToInt16(dbi.ExecScalar(sqlCnt + sqlWhere + " AND C_DateSource='QUNAR'", lstParam.ToArray(), "0")) == 0)
                        {
                            List<Flight> lstFlight_QUNAR = this.QUNAR_Get(fromCity, toCity, strDeparture);
                            dbi.WriteData(lstFlight_QUNAR.ToDataTable(), "FlightInfo");
                        }
                    }

                    if (Convert.ToInt16(req.Form["CEAIR"] ?? "0") == 1)
                    {
                        sbdatasoure.Append(" OR C_DateSource='CE AIR'");
                        if (Convert.ToInt16(dbi.ExecScalar(sqlCnt + sqlWhere + " AND C_DateSource='CE AIR'", lstParam.ToArray(), "0")) == 0)
                        {
                            List<Flight> lstFlight_CEAIR = this.CEAIR_Get(fromCity, toCity, strDeparture);
                            dbi.WriteData(lstFlight_CEAIR.ToDataTable(), "FlightInfo");
                        }
                    }

                    bool needGetCSAIR = false;
                    bool needGetCTRIP = false;
                    bool needGetWS = false;
                    int noOfTask = 0;

                    if (Convert.ToInt16(req.Form["CSAIR"] ?? "0") == 1)
                    {
                        sbdatasoure.Append(" OR C_DateSource='CS AIR'");
                        if (Convert.ToInt16(dbi.ExecScalar(sqlCnt + sqlWhere + " AND C_DateSource='CS AIR'", lstParam.ToArray(), "0")) == 0)
                        {
                            needGetCSAIR = true;
                            noOfTask++;
                            //List<Flight> lstFlight = this.CSAIR_Get(fromCity, toCity, strDeparture);
                            //dbi.WriteData(lstFlight.ToDataTable(), "FlightInfo");
                        }
                    }

                    if (Convert.ToInt16(req.Form["CTRIP"] ?? "0") == 1)
                    {
                        sbdatasoure.Append(" OR C_DateSource='CTRIP API'");
                        if (Convert.ToInt16(dbi.ExecScalar(sqlCnt + sqlWhere + " AND C_DateSource='CTRIP API'", lstParam.ToArray(), "0")) == 0)
                        {
                            needGetCTRIP = true;
                            noOfTask++;
                            //List<Flight> lstFlight = this.CTRIP_Get(fromCity, toCity, strDeparture);
                            //dbi.WriteData(lstFlight.ToDataTable(), "FlightInfo");
                        }
                    }

                    if (Convert.ToInt16(req.Form["WS"] ?? "0") == 1)
                    {
                        sbdatasoure.Append(" OR C_DateSource='webxml'");
                        if (Convert.ToInt16(dbi.ExecScalar(sqlCnt + sqlWhere + " AND C_DateSource='webxml'", lstParam.ToArray(), "0")) == 0)
                        {
                            needGetWS = true;
                            noOfTask++;
                            //List<Flight> lstFlight = this.WS_Get(fromCity, toCity, strDeparture);
                            //dbi.WriteData(lstFlight.ToDataTable(), "FlightInfo");
                        }
                    }

                    Task<List<Flight>>[] tasksList = new Task<List<Flight>>[noOfTask];
                    int taskIndex = 0;
                    if (needGetCSAIR)
                        tasksList[taskIndex++] = Task<List<Flight>>.Factory.StartNew(() => this.CSAIR_Get(fromCity, toCity, strDeparture));

                    if (needGetCTRIP)
                        tasksList[taskIndex++] = Task<List<Flight>>.Factory.StartNew(() => this.CTRIP_Get(fromCity, toCity, strDeparture));

                    if (needGetWS)
                        tasksList[taskIndex++] = Task<List<Flight>>.Factory.StartNew(() => this.WS_Get(fromCity, toCity, strDeparture));
                    if (noOfTask > 0)
                        Task.WaitAll(tasksList, 50 * 1000);
                    List<Flight> lstFlight = new List<Flight>();
                    foreach (var item in tasksList)
                    {
                        lstFlight.AddRange(item.Result);
                    }

                    if (lstFlight.Count > 0)
                        dbi.WriteData(lstFlight.ToDataTable(), "FlightInfo");

                    if (sbdatasoure.Length > 0)
                        sbdatasoure.Remove(0, 3).Insert(0, " AND (").Append(")");

                    string sqlQuery = sqlBase + sqlWhere + sbdatasoure.ToString();
                    if (!string.IsNullOrEmpty(sort))
                        sqlQuery += string.Format(" ORDER BY {0} {1}", sort, order);
                    int total = Convert.ToInt16(dbi.ExecScalar(sqlCnt, lstParam.ToArray(), "0"));
                    DataTable dt = dbi.GetPageData(sqlQuery, page, rowsCnt, lstParam.ToArray());
                    ht.Add("rows", dt);
                    ht.Add("total", total);
                }
                else
                {
                    ht.Add("rows", dbi.GetDataTable(sqlBase + " WHERE 1=2"));
                    ht.Add("total", 0);
                }
            }
            catch (Exception ex)
            {
                Log.LogErr(ex);
                ht.Add("err", ex.Message);
                if (!ht.ContainsKey("rows"))
                {
                    ht.Add("rows", dbi.GetDataTable(sqlBase + " WHERE 1=2"));
                }

                ht.Add("total", 0);
            }

            jsonStr = JsonConvert.SerializeObject(ht);
            ResponseWrite(jsonStr);
        }
Exemplo n.º 3
0
        public void Flight_Get()
        {
            string jsonStr = string.Empty;
            HttpRequest req = Context.Request;
            string strFromCity = req.Form["FromCity"];
            string strToCity = req.Form["ToCity"];
            string strDeparture = req.Form["Departure"];
            string sort = req.Form["sort"];
            int page = Convert.ToInt16(req.Form["page"] ?? "1") - 1;
            int rowsCnt = Convert.ToInt16(req.Form["rows"] ?? "100");
            string order = req.Form["order"];
            Hashtable ht = new Hashtable();
            List<Flight> lstFlight = new List<Flight>();
            try
            {
                List<Flight> rsltLstFlight = new List<Flight>();
                if (!string.IsNullOrEmpty(strFromCity) && !string.IsNullOrEmpty(strToCity) && !string.IsNullOrEmpty(strDeparture))
                {
                    DB dbi = new DB(SystemConst.DBConnString);
                    string sqlCity = @"SELECT * FROM dbo.City WHERE C_CODE=@C_CODE";
                    City fromCity = EntityUtil.Create<City>(dbi.GetDataTable(sqlCity, this.InitSqlParams("C_CODE", strFromCity)).Rows[0]);
                    City toCity = EntityUtil.Create<City>(dbi.GetDataTable(sqlCity, this.InitSqlParams("C_CODE", strToCity)).Rows[0]);

                    //lstFlight.AddRange(this.CSAIR_Get(fromCity, toCity, strDeparture));
                    //lstFlight.AddRange(this.WS_Get(fromCity, toCity, strDeparture));
                    //lstFlight.AddRange(this.CTRIP_Get(fromCity, toCity, strDeparture));
                    lstFlight.AddRange(this.CEAIR_Get(fromCity, toCity, strDeparture));
                    //todo: This function is not finished, you can try.
                    //lstFlight.AddRange(this.QUNAR_Get(fromCity, toCity, strDeparture));

                    if (!string.IsNullOrEmpty(sort) && !string.IsNullOrEmpty(order))
                        lstFlight = EntityUtil.SortList(lstFlight, sort, order.ToEnum<EntityUtil.SortOrder>());

                    Flight[] tmpFlights = new Flight[rowsCnt];
                    if (lstFlight.Count > (page * rowsCnt + rowsCnt))
                        lstFlight.CopyTo(page * rowsCnt, tmpFlights, 0, rowsCnt);
                    else if (lstFlight.Count > page * rowsCnt)
                    {
                        tmpFlights = new Flight[lstFlight.Count - page * rowsCnt];
                        lstFlight.CopyTo(page * rowsCnt, tmpFlights, 0, lstFlight.Count - page * rowsCnt);
                    }
                    else
                    {
                        tmpFlights = new Flight[lstFlight.Count];
                        lstFlight.CopyTo(0, tmpFlights, 0, lstFlight.Count);
                    }

                    rsltLstFlight.AddRange(tmpFlights);
                }

                int total = lstFlight.Count;
                ht.Add("rows", EntityUtil.Create(rsltLstFlight));
                ht.Add("total", total);
            }
            catch (Exception ex)
            {
                ht.Add("err", ex.Message);
                if (!ht.ContainsKey("rows"))
                {
                    ht.Add("rows", EntityUtil.Create(lstFlight));
                }

                ht.Add("total", 0);
            }

            jsonStr = JsonConvert.SerializeObject(ht);
            ResponseWrite(jsonStr);
        }