211 lines
6.3 KiB
C#
211 lines
6.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web.Mvc;
|
|
using FSLib.MvcWeb;
|
|
using Newtonsoft.Json;
|
|
using TrainInfomationProviderService.StationInfo;
|
|
using TrainInfomationProviderService.TrainInfo;
|
|
using TrainInfomationProviderService.TrainInfo.Entities;
|
|
|
|
namespace TrainInfomationProviderService.Web.Controllers
|
|
{
|
|
[AllowOriginFilter(@"(https?://.*?\.(fishlee\.net|liebao\.cn)|chrome-extension://.*)")]
|
|
public class TransitController : Controller
|
|
{
|
|
#region Overrides of Controller
|
|
|
|
/// <summary>
|
|
/// Called when authorization occurs.
|
|
/// </summary>
|
|
/// <param name="filterContext">Information about the current request and action.</param>
|
|
protected override void OnAuthorization(AuthorizationContext filterContext)
|
|
{
|
|
base.OnAuthorization(filterContext);
|
|
|
|
if (!TrainInfoSearchProvider.Instance.IsReady)
|
|
filterContext.Result = Json(new { ret = false, msg = "service not ready." }, JsonRequestBehavior.AllowGet);
|
|
}
|
|
|
|
#endregion
|
|
|
|
[ActionName("ls2")]
|
|
public ActionResult LogSameStation2(string data)
|
|
{
|
|
if (string.IsNullOrEmpty(data))
|
|
return null;
|
|
|
|
return LogSameStation(Encoding.UTF8.GetString(Convert.FromBase64String(data)));
|
|
}
|
|
|
|
[ActionName("ls")]
|
|
public ActionResult LogSameStation(string data)
|
|
{
|
|
var list = JsonConvert.DeserializeObject<List<HashSet<string>>>(data);
|
|
var map = SameStationManager.SameStationMap;
|
|
var count = map.Count;
|
|
|
|
foreach (var hashSet in list)
|
|
{
|
|
var curSet = hashSet.Select(map.GetValue).FirstOrDefault(s => s != null) ?? new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var s1 in hashSet)
|
|
{
|
|
curSet.SafeAdd(s1);
|
|
}
|
|
|
|
foreach (var s in hashSet)
|
|
{
|
|
map.AddOrUpdate(s, curSet);
|
|
}
|
|
}
|
|
if (map.Count != count)
|
|
{
|
|
//有变化
|
|
SameStationManager.Save();
|
|
}
|
|
|
|
return Json(new { ret = true });
|
|
}
|
|
|
|
[OutputCache(Duration = 3600)]
|
|
[ActionName("ss")]
|
|
public ActionResult GetSameStation(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id))
|
|
return null;
|
|
|
|
return Json(new
|
|
{
|
|
ret = true,
|
|
data = SameStationManager.SameStationMap.GetValue(id),
|
|
time = DateTime.Now.ToString()
|
|
}, JsonRequestBehavior.AllowGet);
|
|
}
|
|
|
|
public ActionResult KeepAlive()
|
|
{
|
|
return Json(new { ret = true, time = DateTime.Now.ToString() }, JsonRequestBehavior.AllowGet);
|
|
}
|
|
|
|
[OutputCache(Duration = 3600)]
|
|
[ActionName("tor")]
|
|
public ActionResult TransitOnceRouting(string from, string to, DateTime date, int? maxAge)
|
|
{
|
|
if (String.IsNullOrEmpty(from))
|
|
throw new ArgumentException("from is null or empty.", "from");
|
|
if (String.IsNullOrEmpty(to))
|
|
throw new ArgumentException("to is null or empty.", "to");
|
|
|
|
var maxMinute = maxAge ?? 99999;
|
|
var opt = new TrainTransitSearchOptions();
|
|
opt.InitLimit(maxMinute);
|
|
|
|
var provider = TrainInfoSearchProvider.Instance;
|
|
var result = provider.FindOnceTransitTrains(date, from, to, opt);
|
|
if (result == null)
|
|
return Json(new { ret = false, msg = "no routing found." });
|
|
|
|
var cities = result.Select(s => s.First.ToStation).Distinct().ToDictionary(s => s.Code, s => new
|
|
{
|
|
name = s.Name,
|
|
ncount = result.Count(x => x.NotRecommand && x.First.ToStation == s),
|
|
rcount = result.Count(x => !x.NotRecommand && x.First.ToStation == s)
|
|
});
|
|
var lines = result.Select(s => new
|
|
{
|
|
ne = s.NotRecommand,
|
|
first = new
|
|
{
|
|
from = s.First.FromStation.Code,
|
|
to = s.First.ToStation.Code,
|
|
train = new
|
|
{
|
|
id = s.First.Train.Id,
|
|
code = s.First.Train.Code
|
|
},
|
|
elapsed = (int)s.FirstElapsedTime.TotalMinutes,
|
|
left = s.FirstTrainLeftDate.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
arrive = s.FirstTrainArriveDate.ToString("yyyy-MM-dd HH:mm:ss")
|
|
},
|
|
second = new
|
|
{
|
|
from = s.Second.FromStation.Code,
|
|
to = s.Second.ToStation.Code,
|
|
train = new
|
|
{
|
|
id = s.Second.Train.Id,
|
|
code = s.Second.Train.Code
|
|
},
|
|
elapsed = (int)s.SecondElapsedTime.TotalMinutes,
|
|
left = s.SecondTrainLeftDate.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
arrive = s.SecondTrainArriveDate.ToString("yyyy-MM-dd HH:mm:ss")
|
|
},
|
|
wait = (int)s.WaitElaspsedTime.TotalMinutes,
|
|
total = (int)s.TotalElapsedTime.TotalMinutes
|
|
}).ToArray();
|
|
var toplines = result.TopLinesIndices;
|
|
|
|
var data = new
|
|
{
|
|
cities,
|
|
corder = result.CityOrder,
|
|
lines,
|
|
toplines,
|
|
count = new
|
|
{
|
|
notRecommand = lines.Count(s => s.ne),
|
|
regular = lines.Count(s => !s.ne)
|
|
}
|
|
};
|
|
|
|
return Json(new { ret = true, time = DateTime.Now.ToString(), data }, JsonRequestBehavior.AllowGet);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 直达线路
|
|
/// </summary>
|
|
/// <param name="from"></param>
|
|
/// <param name="to"></param>
|
|
/// <param name="date"></param>
|
|
/// <returns></returns>
|
|
[OutputCache(Duration = 3600)]
|
|
[ActionName("dt")]
|
|
public ActionResult DirectRouting(string from, string to, DateTime date)
|
|
{
|
|
var lines = TrainInfoSearchProvider.Instance.FindDirectTrains(ref date, from, to);
|
|
|
|
return Json(new
|
|
{
|
|
ret = true,
|
|
time = DateTime.Now.ToString(),
|
|
data = new
|
|
{
|
|
date = date.ToString("yyyy-MM-dd"),
|
|
lines = lines.Select(s => new
|
|
{
|
|
from = new
|
|
{
|
|
name = s.FromStation.Name,
|
|
code = s.FromStation.Code,
|
|
leftTime = s.LeftTime.ToString("yyyy-MM-dd HH:mm:ss")
|
|
},
|
|
to = new
|
|
{
|
|
name = s.ToStation.Name,
|
|
code = s.ToStation.Code,
|
|
leftTime = s.ArriveTime.ToString("yyyy-MM-dd HH:mm:ss")
|
|
},
|
|
elapsed = (int)s.ElapsedTime.TotalMinutes,
|
|
train = new
|
|
{
|
|
id = s.Train.Id,
|
|
code = s.Train.Code
|
|
}
|
|
}).ToArray()
|
|
}
|
|
}, JsonRequestBehavior.AllowGet);
|
|
}
|
|
}
|
|
}
|